A Simple Problem with Integers
// 区间修改,区间求和
// A为原数组,d 和 df 为树状差分数组,d[i] = A[i] - A[i-1],df[i] = i*d[i]
// A的前缀和 = d的前缀和 - df的前缀和 (易证)
同类
#include <stdio.h>
#define MXN 100010
#define LL long long
LL A[MXN], d[MXN], df[MXN], ans;
int n, q;
void add(LL* diff, int p, LL val){
while(p <= n) diff[p] += val, p += p & -p;
}
LL query(LL* diff, int p){
LL res = 0;
while(p > 0) res += diff[p], p -= p & -p;
return res;
}
int main(){
char op[10];
int a, b, c;
scanf("%d %d", &n, &q);
A[0] = 0;
for(int i = 1; i <= n; i++) {
scanf("%lld", A+i);
add(d, i, A[i]-A[i-1]);
add(df, i, i*(A[i]-A[i-1])); // i*(A[i]-A[i-1]) long long
}
while(q--){
scanf("%s %d %d", op, &a, &b);
if(op[0] == 'Q'){
ans = (b+1)*query(d, b) - a*query(d, a-1);
ans += query(df, a-1) - query(df, b);
printf("%lld\n", ans);
}
else{
scanf("%d", &c);
add(d, a, c);
add(d, b+1, -c);
add(df, a, a*c);
add(df, b+1, -(b+1)*c);
}
}
return 0;
}