/*
POJ 3468
区间增加
区间求和
懒惰标记
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson (pos<<1)
#define rson (pos<<1|1)
typedef long long LL;
const int maxn = 100005;
int n,m;
LL sum[maxn << 2],col[maxn << 2];
void pushdown(int pos,int l,int r){
if(col[pos]){
int mid = (l + r) >> 1;
int v1 = mid - l + 1;
int v2 = r - mid;
sum[lson] += (col[pos] * v1);
col[lson] += col[pos];
sum[rson] += (col[pos] * v2);
col[rson] += col[pos];
col[pos] = 0;
}
}
void pushup(int pos){
sum[pos] = sum[lson] + sum[rson];
}
void build(int l,int r,int pos){
col[pos] = 0;
if(l == r){
scanf("%I64d",&sum[pos]);
return;
}
int mid = (l + r) >> 1;
build(l,mid,lson);
build(mid + 1,r,rson);
pushup(pos);
}
void update(int L,int R,int l,int r,int pos,LL d){
if(l <= L && R <= r){
sum[pos] += (d * (R - L + 1));
col[pos] += d;
return;
}
pushdown(pos,L,R);
int mid = (L + R) >> 1;
if(l <= mid)
update(L,mid,l,r,lson,d);
if(r > mid)
update(mid + 1,R,l,r,rson,d);
pushup(pos);
}
LL query(int L,int R,int l,int r,int pos){
if(l <= L && R <= r)
return sum[pos];
pushdown(pos,L,R);
int mid = (L + R) >> 1;
LL ans = 0;
if(l <= mid)
ans += query(L,mid,l,r,lson);
if(r > mid)
ans += query(mid + 1,R,l,r,rson);
pushup(pos);
return ans;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m) != EOF){
build(1,n,1);
for(int i = 0;i < m; i++){
char op[10];
int a,b,c;
scanf("%s",op);
if(op[0] == 'Q'){
scanf("%d%d",&a,&b);
printf("%I64d\n",query(1,n,a,b,1));
}
else{
scanf("%d%d%d",&a,&b,&c);
update(1,n,a,b,1,c);
}
}
}
return 0;
}
POJ 3468
最新推荐文章于 2021-09-06 19:16:04 发布