题解
题解传送门
code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
typedef long long LL;
template <typename T>
inline void read(T &s) {
s = 0;
T w = 1, ch = getchar();
while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
s *= w;
}
int n, m;
int a[maxn];
LL c[2][maxn], sum[maxn];
inline int lowbit(int x) { return x & (-x); }
inline void add(int k, int x, int y) {
for (; x <= n; x += lowbit(x))
c[k][x] += y;
}
inline LL ask(int k, int x) {
LL ans = 0ll;
for (; x; x -= lowbit(x))
ans += c[k][x];
return ans;
}
int main() {
read(n), read(m);
for (int i = 1; i <= n; ++i) {
read(a[i]);
sum[i] = sum[i - 1] + a[i];
}
while (m--) {
char ch; cin >> ch;
if (ch == 'C') {
int l, r, d;
read(l), read(r), read(d);
add(0, l, d);
add(0, r + 1, -d);
add(1, l, l * d);
add(1, r + 1, -(r + 1) * d);
}
else if (ch == 'Q') {
int l, r;
read(l), read(r);
LL ans = sum[r] + (r + 1) * ask(0, r) - ask(1, r);
// check;
ans -= sum[l - 1] + l * ask(0, l - 1) - ask(1, l - 1);
printf("%lld\n", ans);
}
}
return 0;
}