Neven @ 2023-07-25 12:28:43
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, num[N], op, x, y, k;
struct node{
int lft, rgt, sum, lazy;
}tree[N * 4];
void build(int pos, int l, int r){
tree[pos].lft = l, tree[pos].rgt = r;
if(l == r){
tree[pos].sum = num[l];
return;
}
int mid = l + r >> 1;
build(pos << 1, l, mid);
build((pos << 1) + 1, mid + 1, r);
tree[pos].sum = tree[pos << 1].sum + tree[(pos << 1) + 1].sum;
}
void push_down(int pos){
if(!tree[pos].lazy) return;
tree[pos << 1].lazy = tree[(pos << 1) + 1].lazy = tree[pos].lazy;
int mid = tree[pos].lft + tree[pos].rgt >> 1;
tree[pos << 1].sum += tree[pos].lazy * (mid - tree[pos << 1].lft + 1);
tree[(pos << 1) + 1].sum = tree[pos].lazy * (tree[(pos << 1) + 1].rgt - mid);
tree[pos].lazy = 0;
}
void add(int pos, int l, int r, int num){
if(tree[pos].lft >= l && tree[pos].rgt <= r){
tree[pos].sum += num * (tree[pos].rgt - tree[pos].lft + 1);
tree[pos].lazy += num;
return;
}
push_down(pos);
int mid = tree[pos].lft + tree[pos].rgt >> 1;
if(l <= mid) add(pos << 1, l, r, k);
if(r > mid) add((pos << 1) | 1, l, r, k);
}
int search(int pos, int l, int r){
if(tree[pos].lft >= l && tree[pos].rgt <= r){
return tree[pos].sum;
}
if(tree[pos].lft > r || tree[pos].rgt < l) return 0;
push_down(pos);
return search(pos << 1, l, r) + search((pos << 1) + 1, l, r);
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> num[i];
}
build(1, 1, n);
while(m--){
cin >> op >> x >> y;
if(op == 1){
cin >> k;
add(1, x, y, k);
}else{
cout << search(1, x, y + 1) << endl;
}
}
return 0;
}
by hjqhs @ 2023-07-25 12:43:04
给您看一下我写的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100005;
int n,q,a[maxn];
struct SegmentTree{
int l,r;
ll dat_sum;
ll tag_add;
}tree[maxn*4];
int ls(int p){return p*2;}
int rs(int p){return p*2+1;}
int len(int p){return tree[p].r-tree[p].l+1;}
void push_up(int p){
tree[p].dat_sum=tree[ls(p)].dat_sum+tree[rs(p)].dat_sum;
}
void build_tree(int p,int l,int r){
tree[p].l=l;
tree[p].r=r;
if(l==r){
tree[p].dat_sum=a[l];
return;
}
int mid=(l+r)/2;
build_tree(ls(p),l,mid);
build_tree(rs(p),mid+1,r);
push_up(p);
}
void push_down(int p){
tree[ls(p)].dat_sum+=tree[p].tag_add*(len(ls(p)));
tree[rs(p)].dat_sum+=tree[p].tag_add*(len(rs(p)));
tree[ls(p)].tag_add+=tree[p].tag_add;
tree[rs(p)].tag_add+=tree[p].tag_add;
tree[p].tag_add=0;
}
void change(int p,int x,int y,int k){
if(x<=tree[p].l&&tree[p].r<=y){
tree[p].dat_sum+=len(p)*k;
tree[p].tag_add+=k;
return;
}
push_down(p);
int mid=(tree[p].l+tree[p].r)/2;
if(x<=mid)change(ls(p),x,y,k);
if(y>mid)change(rs(p),x,y,k);
push_up(p);
}
ll ask(int p,int x,int y){
if(x<=tree[p].l&&tree[p].r<=y){
return tree[p].dat_sum;
}
push_down(p);
ll res=0;
int mid=(tree[p].l+tree[p].r)/2;
if(x<=mid)res+=ask(ls(p),x,y);
if(y>mid)res+=ask(rs(p),x,y);
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);cout.tie(nullptr);
cin>>n>>q;
for(int i=1;i<=n;++i)cin>>a[i];
build_tree(1,1,n);
for(int i=1;i<=q;++i){
int op;
cin>>op;
if(op==1){
int x,y;ll k;
cin>>x>>y>>k;
change(1,x,y,k);
}else if(op==2){
int x,y;
cin>>x>>y;
cout<<ask(1,x,y)<<endl;
}
}
return 0;
}
by Neven @ 2023-07-25 13:05:35
@hjqhs 感谢大佬