差分视角
$[l, r] \xleftarrow{+d}$
$B_i = A_i - A_{i - 1}$
$$ A_i = \sum_{j = 1}^{i} B_j \\\ \\ \\\ S_{\text{prefix}}(x) = \sum_{i = 1}^{x} A_i = \sum_{i = 1}^{x}\sum_{j = 1}^{i} B_j \\\ \\ \\\ = \sum_{i = 1}^{x}(x-i+1)B_i = (x+1)\sum_{i = 1}^{x}B_i - \sum_{i = 1}^{x}i \cdot B_i $$
$\textbf{algorithm}$
$\textbf{i) } \text{fwick[0]} \leftarrow \sum_{i = 1}^{x} B_i, \quad \text{fwick[1]} \leftarrow \sum_{i = 1}^{x} i B_i$
$\textbf{ii) change: } [l, r]$
$\quad \quad \text{fwick[0]}.add(r+1, -d), \quad \text{fwick[0]}.add(l, d)$
$\quad \quad \text{fwick[1]}.add(r+1, -(r+1)d), \quad \text{fwick[1]}.add(l, ld)$
$\textbf{ii) ask: } [l, r]$
$\quad \quad S(r) = (\text{fwick}[0].ask(r)) \times (r+1) - \text{fwick[1]}(r)$
$\quad \quad S(l -1) = (\text{fwick}[0].ask(l - 1)) \times (l) - \text{fwick[1]}(l - 1)$
$\quad \quad \textbf{ans} = S(r) - S(l - 1)$
const int maxn = 100000 + 10;
int n, m;
ll A[maxn];
// == fenwick definition ==
class Fwick {
public:
vector<ll> C[2];
int n;
void resize(int n) {
this->n = n;
_for(i, 0, 2) C[i].resize(n + 1);
}
void clear() {
_for(i, 0, 2) fill(C[i].begin(), C[i].end(), 0ll);
}
void add(int k, int x, ll d) {
int i = x;
for(; i <= n; i += lowbit(i)) C[k][i] += 1ll * d;
}
ll ask(int k, int x) {
ll ans = 0;
for(int i = x; i; i -= lowbit(i)) ans += C[k][i];
return ans;
}
} fwick;
// == fenwick finsihed ==
ll prefix(int x) {
return fwick.ask(0, x) * (x + 1) - fwick.ask(1, x);
}
void init() {
fwick.resize(maxn);
fwick.clear();
}
int main() {
freopen("input.txt", "r", stdin);
scanf("%d%d", &n, &m);
init();
_rep(i, 1, n) {
scanf("%lld", &A[i]);
fwick.add(0, i, A[i] - A[i - 1]);
fwick.add(1, i, i * (A[i] - A[i - 1]));
}
// then solve
while (m--) {
char cmd[2];
scanf("%s", cmd);
if(cmd[0] == 'C') {
int l, r, d;
scanf("%d%d%d", &l, &r, &d);
fwick.add(0, r + 1, -1ll * d);
fwick.add(0, l, 1ll * d);
fwick.add(1, r + 1, -1ll * d * (r + 1));
fwick.add(1, l, 1ll * d * l);
}
else {
int l, r;
scanf("%d%d", &l, &r);
ll ans = prefix(r) - prefix(l - 1);
printf("%lld\n", ans);
}
}
}