#include<iostream>
#include<algorithm>
#include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int Max=110010;
long long op[Max<<2];
long long sum[Max<<2];
int N,Q,a,b,c;
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int len)
{
if(op[rt])
{
op[rt<<1]+=op[rt];
op[rt<<1|1]+=op[rt];
sum[rt<<1]=sum[rt<<1]+(len-(len>>1))*op[rt];
sum[rt<<1|1]=sum[rt<<1|1]+(len>>1)*op[rt];
op[rt]=0;
}
}
void build(int l,int r,int rt)
{
op[rt]=0;
if(r==l)
{
scanf("%lld",&sum[rt]);
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void Update(int L,int R,int C,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
op[rt]+=C;
sum[rt]=sum[rt]+(r-l+1)*C;
return ;
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
Update(L,R,C,lson);
if(R>m)
Update(L,R,C,rson);
PushUp(rt);
}
long long get_sum(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
long long cnt=0;
if(L<=m)
cnt+=get_sum(L,R,lson);
if(R>m)
cnt+=get_sum(L,R,rson);
return cnt;
}
int main()
{
char gh[2];
while(scanf("%d%d",&N,&Q)!=EOF)
{
build(1,N,1);
for(int i=0; i<Q; i++)
{
scanf("%s",gh);
switch(gh[0])
{
case 'Q':
scanf("%d%d",&a,&b);
printf("%lld\n",get_sum(a,b,1,N,1));
break;
case 'C':
scanf("%d%d%d",&a,&b,&c);
Update(a,b,c,1,N,1);
break;
}
}
}
return 0;
}
poj3468 线段树成段更新
最新推荐文章于 2018-02-25 21:13:37 发布