没错,这是一个裸的线段树。
#include<stdio.h>
using namespace std;
typedef long long ll;
const int Maxn=1e5+5;
ll c[Maxn];
struct sgt{
ll sum[Maxn*8];
ll addv[Maxn*8];
void build(int o,int l,int r){
addv[o]=0;
if(l==r)sum[o]=c[l];
else{
int mid=(l+r)>>1,lson=o<<1,rson=lson|1;
build(lson,l,mid);
build(rson,mid+1,r);
sum[o]=sum[lson]+sum[rson];
}
}
void pushdown(int o,int l,int r,int mid,int lson,int rson){
addv[lson]+=addv[o];
addv[rson]+=addv[o];
sum[lson]+=(mid-l+1)*addv[o];
sum[rson]+=(r-mid)*addv[o];
addv[o]=0;
}
void update(int o,int l,int r,int a,int b,int x){
if(l>=a && r<=b){
addv[o]+=x;
sum[o]+=(r-l+1)*x;
return;
}
else{
int mid=(l+r)>>1,lson=o<<1,rson=lson|1;
if(addv[o])pushdown(o,l,r,mid,lson,rson);
if(a<=mid)update(lson,l,mid,a,b,x);
if(b>mid)update(rson,mid+1,r,a,b,x);
sum[o]=sum[lson]+sum[rson];
}
}
ll query(int o,int l,int r,int a,int b){
if(l>=a && r<=b) return sum[o];
else{
int mid=(l+r)>>1,lson=o<<1,rson=lson|1;
ll ans=0;
if(addv[o]) pushdown(o,l,r,mid,lson,rson);
if(a<=mid)ans+=query(lson,l,mid,a,b);
if(b>mid) ans+=query(rson,mid+1,r,a,b);
return ans;
}
}
};
sgt tree;
int n,m,opt;
int x,y,k;
int main(){
scanf("%d%d",&n,&m);
for(register int i=1;i<=n;i++)scanf("%d",&c[i]);
tree.build(1,1,n);
for(register int i=1;i<=m;i++){
scanf("%d",&opt);
if(opt&1){
scanf("%d%d%d",&x,&y,&k);
tree.update(1,1,n,x,y,k);
}
else{
scanf("%d%d",&x,&y);
printf("%lld\n",tree.query(1,1,n,x,y));
}
}
return 0;
}