如题,感谢!
#include <iostream>
using namespace std;
int n;
int a[114514];
struct dot{
int l;
int r;
int sum;
int lazy;
};
dot t[114514*4];
void pushup(int p){
t[p].sum=t[p<<1].sum+t[(p<<1)+1].sum;
}
void pushdown(int p){
if(t[p].lazy){
t[p<<1].sum+=t[p].lazy*(t[p<<1].r-t[p<<1].l+1);
t[(p<<1)+1].sum+=t[p].lazy*(t[(p<<1)+1].r-t[(p<<1)+1].l+1);
t[p<<1].lazy+=t[p].lazy;
t[(p<<1)+1].lazy+=t[p].lazy;
t[p].lazy=0;
return;
}
}
void build(int p,int l,int r){
t[p].l=l;
t[p].r=r;
if(l==r){
t[p].sum=a[l];
return;
}
int mid=(t[p].l+t[p].r)/2;
build(p<<1,l,mid);
build((p<<1)+1,mid+1,r);
pushup(p);
}
void add(int p,int L,int R,int d){
if(t[p].l>L&&t[p].r<R){
t[p].sum+=(t[p].r-t[p].l+1)*d;
t[p].lazy+=d;
return;
}
pushdown(p);
int mid=(t[p].l+t[p].r)/2;
if(L<=mid)
add(2*p,L,R,d);
if(mid<R)
add(2*p+1,L,R,d);
pushup(p);
}
int getsum(int p,int l,int r){
if(l<=t[p].l&&t[p].r<=r)
return t[p].sum;
pushdown(p);
int mid=(t[p].l+t[p].r)/2;
int ans=0;
if(l<=mid)
ans+=getsum(p<<1,l,r);
if(mid<r)
ans+=getsum((p<<1)+1,l,r);
return ans;
}
int main(){
int T;
cin>>n>>T;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
while(T--){
int op;
cin>>op;
if(op==1){
int l,r,k;
cin>>l>>r>>k;
add(1,l,r,k);
}else{
int x,y;
cin>>x>>y;
cout<<getsum(1,x,y)<<endl;
}
}
}