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.

51 lines
1.1 KiB

#include <cstdio>
#include <cstring>
#include <algorithm>
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 x, y, k;
scanf("%s", op);
if (op[0] == 'Q') {
scanf("%d%d", &x, &y);
printf("%lld\n", query(y) - query(x - 1));
} else {
scanf("%d%d%d", &x, &y, &k);
add(c1, x, k), add(c1, y + 1, -k);
add(c2, x, k * x), add(c2, y + 1, (y + 1) * -k);
}
}
return 0;
}