note:
- 10.11:
if(tr[x].mark&&tr[x].l<tr[x].r)
不要写成 ≤ bt(lch,l,mid);
bt(rch,mid+1,r);
这两句不要加括号
code:
#include<cstdio>
#define lch ((x)*2)
#define rch ((x)*2+1)
using namespace std;
struct ben
{
long long l,r,val,mark;
}tr[400005];
long long a[100005];
void bt(long long x,long long l,long long r)
{
tr[x].l=l;
tr[x].r=r;
if(tr[x].l==tr[x].r)
{
tr[x].val=a[l];
return ;
}
long long mid=(tr[x].l+tr[x].r)/2;
bt(lch,l,mid);
bt(rch,mid+1,r);
tr[x].val=tr[lch].val+tr[rch].val;
}
void down(long long x)
{
if(tr[x].mark&&tr[x].l<tr[x].r)
{
tr[lch].val+=tr[x].mark*(tr[lch].r-tr[lch].l+1);
tr[lch].mark+=tr[x].mark;
tr[rch].val+=tr[x].mark*(tr[rch].r-tr[rch].l+1);
tr[rch].mark+=tr[x].mark;
}
tr[x].mark=0;
}
void plus(long long x,long long l,long long r,long long k)
{
down(x);
if(l<=tr[x].l&&tr[x].r<=r)
{
tr[x].val+=k*(tr[x].r-tr[x].l+1);
tr[x].mark+=k;
return ;
}
long long mid=(tr[x].l+tr[x].r)/2;
if(l<=mid)
{
plus(lch,l,r,k);
}
if(r>mid)
{
plus(rch,l,r,k);
}
tr[x].val=tr[lch].val+tr[rch].val;
}
long long sum(long long x,long long l,long long r)
{
down(x);
long long ans=0;
if(l<=tr[x].l&&tr[x].r<=r)
{
return tr[x].val;
}
long long mid=(tr[x].l+tr[x].r)/2;
if(l<=mid)
{
ans+=sum(lch,l,r);
}
if(r>mid)
{
ans+=sum(rch,l,r);
}
return ans;
}
int main()
{
long long n,m;
scanf("%lld%lld",&n,&m);
for(long long i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
bt(1,1,n);
for(long long i=1;i<=m;i++)
{
long long pd,x,y;
scanf("%lld%lld%lld",&pd,&x,&y);
if(pd==1)
{
long long k;
scanf("%lld",&k);
plus(1,x,y,k);
}
else
{
printf("%lld\n",sum(1,x,y));
}
}
return 0;
}