#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define maxn 110000
#define ll long long
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
ll tree[maxn<<2],add[maxn<<2];
void pushup(int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void pushdown(int num,int mid)
{
if(add[num])
{
add[num<<1] += add[num];
add[num<<1|1] += add[num];
tree[num<<1] += ((mid-(mid>>1)) * add[num]);
tree[num<<1|1] += ((mid>>1) * add[num]);
add[num] = 0;
}
}
void build(int rt,int l,int r)
{
add[rt]=0;
if(l==r)
{
scanf("%lld",&tree[rt]);
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int rt,int l,int r,int x,int y,int val)
{
if(x<=l&&y>=r)
{
add[rt]+=val;
tree[rt] += (ll)val * (r - l + 1);
return;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(x<=m)
{
update(lson,x,y,val);
}
if(y>m)
{
update(rson,x,y,val);
}
pushup(rt);
}
ll query(int rt,int l,int r,int x,int y)
{
if(x==l&&y==r)
{
return tree[rt];
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
ll tmp=0;
if(x>m)
{
return query(rson,x,y);
}
else if(y<=m)
{
return query(lson,x,y);
}
else
{
return query(lson,x,m)+query(rson,m+1,y);
}
}
int main()
{
int n,m,q;
char str[5];
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i=1;i<=m;i++)
{
//printf("op = %d %d\n",op,n);
int a,b,c;
scanf("%s",str);
if(str[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(1,1,n,a,b));
}
else if(str[0]=='C')
{
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,c);
}
}
return 0;
}
poj3468 A Simple Problem with Integers 线段树区间更新
最新推荐文章于 2020-05-15 23:49:15 发布