照着一篇题解打的。但是0分。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+100;
int a[N];
struct tree{
int l,r;
long long pre,add;
}t[4*N];
void build(int p,int l,int r)//编号 区间l,r 建树
{
t[p].l=l,t[p].r=r;
if(l==r)
{
t[p].pre=a[l]; return ;
}
int mid=l+2>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
t[p].pre=t[p*2].pre+t[p*2+1].pre;
}
void spread(int p)//懒标记
{
if(t[p].add!=0)
{
t[p*2].pre+=t[p].add*(t[p*2].r-t[p*2].l+1);
t[p*2+1].pre+=t[p].add*(t[p*2+1].r-t[p*2+1].l+1);
t[p*2].add+=t[p].add; t[p*2+1].add+=t[p].add;
t[p].add=0;
}
}
void change(int p,int x,int y,int z)//修改
{
if(x<=t[p].l && y>=t[p].r)
{
t[p].pre+=z*(t[p].r-t[p].l+1);
t[p].add+=z; return ;
}
spread(p);
int mid=t[p].l+t[p].r>>1;
if(x<=mid) change(p*2,x,y,z);
if(y>mid) change(p*2+1,x,y,z);
t[p].pre=t[p*2].pre+t[p*2+1].pre;
}
int ask(int p,int x,int y)//查询
{
if(x<=t[p].l && y>=t[p].r) return t[p].pre;
spread(p);
int mid=t[p].l+t[p].r>>1;
int ans=0;
if(x<=mid) ans+=ask(p*2,x,y);
if(y>mid) ans+=ask(p*2+1,x,y);
return ans;
}
signed main( )
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
build(1,1,n);
for(int i=1;i<=m;i++)
{
int q,x,y,z; cin>>q;
if(q==1)
{
cin>>x>>y>>z;
change(1,x,y,z);
}
else
{
cin>>x>>y;
cout<<ask(1,x,y)<<"\n";
}
}
return 0;
}