线段树,求和版模板
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
ll seg[maxn<<2],lazy[maxn<<2],g[maxn];
int n,m;
inline void push_down(int p,int l,int r){
if(!lazy[p])return;
int mid=l+r>>1;
lazy[p<<1]+=lazy[p],seg[p<<1]+=(mid-l+1)*lazy[p];
lazy[p<<1|1]+=lazy[p],seg[p<<1|1]+=(r-mid)*lazy[p];
lazy[p]=0;
}
inline void add(int p,int l,int r,int L,int R,ll d){
if(L<=l&&r<=R){seg[p]+=(r-l+1)*d,lazy[p]+=d;return;}
push_down(p,l,r);
int mid=l+r>>1;
if(L<=mid)add(p<<1,l,mid,L,R,d);
if(R>mid)add(p<<1|1,mid+1,r,L,R,d);
seg[p]=seg[p<<1]+seg[p<<1|1];
}
inline ll query(int p,int l,int r,int L,int R){
ll sum=0;
if(L<=l&&r<=R)return seg[p];
push_down(p,l,r);
int mid=l+r>>1;
if(L<=mid)sum+=query(p<<1,l,mid,L,R);
if(R>mid)sum+=query(p<<1|1,mid+1,r,L,R);
return sum;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&g[i]);
for(int i=1;i<=n;i++)add(1,1,n,i,i,g[i]);
ll op,a,b,c;
while(m--){
scanf("%lld",&op);
if(op==1){
scanf("%lld%lld%lld",&a,&b,&c);add(1,1,n,a,b,c);
}else{
scanf("%lld%lld",&a,&b);
printf("%lld\n",query(1,1,n,a,b));
}
}
return 0;
}