区间增减懒惰标记
#include <iostream>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
#define MAXN 111111
ll stree[MAXN<<2];
ll cont[MAXN<<2];
void pushup(int rt) {
stree[rt]=stree[rt<<1]+stree[rt<<1|1];
}
void build(int l,int r,int rt)
{
cont[rt]=0;
if(l==r)
{
scanf("%lld",&stree[rt]);
return;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int l,int r,int rt)
{
if(cont[rt])
{
cont[rt<<1]+=cont[rt];
cont[rt<<1|1]+=cont[rt];
int mid=(l+r)>>1;
stree[rt<<1]=stree[rt<<1]+(mid-l+1)*cont[rt];
stree[rt<<1|1]=stree[rt<<1|1]+(r-(mid+1)+1)*cont[rt];
cont[rt]=0;
}
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return stree[rt];
}
int mid=(l+r)>>1;
pushdown(l,r,rt);
ll ans=0;
if(L<=mid) ans+=query(L,R,lson);
if(R>mid) ans+=query(L,R,rson);
return ans;
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
cont[rt]+=c;
stree[rt]=stree[rt]+c*(r-l+1);
return;
}
int mid=(l+r)>>1;
pushdown(l,r,rt);
if(L<=mid) update(L,R,c,lson);
if(R>mid) update(L,R,c,rson);
pushup(rt);
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
build(1,n,1);
char str[10];
for(int i=0;i<q;i++)
{
int a,b,c;
scanf("%s%d%d",str,&a,&b);
if(str[0]=='Q')
printf("%lld\n",query(a,b,1,n,1));
else
{
scanf("%d",&c);
update(a,b,c,1,n,1);
}
}
return 0;
}