You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
typedef long long LL;
int n, m;
LL a[N];
LL c1[N], c2[N];
#define lowbit(x) (x & -x)
void add(LL c[], LL x, LL v) {
while (x < N) c[x] += v, x += lowbit(x);
}
LL sum(LL c[], LL x) {
LL res = 0;
while (x) res += c[x], x -= lowbit(x);
return res;
}
LL query(LL x) {
return sum(c1, x) * (x + 1) - sum(c2, x);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
add(c1, i, a[i]), add(c1, i + 1, -a[i]);
add(c2, i, a[i] * i), add(c2, i + 1, -(i + 1) * a[i]);
}
while (m--) {
char op[110];
int i, j, d;
scanf("%s", op);
if (op[0] == 'Q') {
scanf("%d%d", &i, &j);
printf("%lld\n", query(j) - query(i - 1));
} else {
scanf("%d%d%d", &i, &j, &d);
add(c1, i, d), add(c1, j + 1, -d);
add(c2, i, d * i), add(c2, j + 1, -(j + 1) * d);
}
}
return 0;
}