2
一个简单的问题 分析
见《进阶指南》第207
页。
实现
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 100010;
int n, a[N], m;
LL c[2][N], sum_a[N];
int lowbit (int x) {
return x & -x;
}
void add (int k, int idx, int val) {
while (idx <= n) {
c[k][idx] += val;
idx += lowbit(idx);
}
}
LL sum (int k, int idx) {
LL res = 0;
while (idx >= 1) {
res += c[k][idx];
idx -= lowbit(idx);
}
return res;
}
int main () {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++ i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++ i) sum_a[i] = sum_a[i - 1] + a[i];
while (m --) {
char opt[5];
scanf("%s", opt);
if (opt[0] == 'C') {
int l, r, d;
scanf("%d%d%d", &l, &r, &d);
add(0, l, d);
add(0, r + 1, -d);
add(1, l, l * d);
add(1, r + 1, -(r + 1) * d);
} else {
int l, r;
scanf("%d%d", &l, &r);
LL res = sum_a[r] + (r + 1) * sum(0, r) - sum(1, r)
- (sum_a[l - 1] + l * sum(0, l - 1) - sum(1, l - 1));
printf("%lld\n", res);
}
}
return 0;
}