poj 3486 A Simple Problem with Integers(树状数组第三种模板改段求段)

时间:2022-04-19 09:08:01
 /*
树状数组第三种模板(改段求段)不解释!
不明白的点这里:here
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std; typedef long long LL; LL ss[N], B[N], C[N]; int n, m; void addB(int x, int k){//B[i]表示被1...i整体一共加了多少的总和
for(int i=x; i<=n; i+=i&(-i)) B[i]+=x*k;
} void addC(int x, int k){//1....x节点的每个节点的增量
for(int i=x; i>; i-=i&(-i)) C[i]+=k;
} LL sumB(int x){
LL s=;
for(int i=x; i>; i-=i&(-i)) s+=B[i];
return s;
} LL sumC(int x){//x节点总共的增量
LL s=;
for(int i=x; i<=n; i+=i&(-i)) s+=C[i];
return s;
} LL sum(int x){
return x== ? : sumC(x)*x + sumB(x-);
} void update(int a, int b, int c){
addB(b, c);
addC(b, c);
if(a->){
addB(a-, -c);
addC(a-, -c);
}
} int main(){
int m;
while(scanf("%d%d", &n, &m)!=EOF){
for(int i=; i<=n; ++i){
scanf("%lld", &ss[i]);
ss[i]+=ss[i-];
}
char ch[];
int a, b, c;
while(m--){
scanf("%s", ch);
if(ch[]=='Q'){
scanf("%d%d", &a, &b);
printf("%lld\n", ss[b]-ss[a-]+sum(b)-sum(a-));
}
else{
scanf("%d%d%d", &a, &b, &c);
update(a, b, c);
}
}
}
return ;
}