题目链接:传送门
线段树维护前缀和
简单明了
修改就修改
当然还有更快的树状数组差分的做法
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define
using namespace std;
typedef long long ll;
struct node {
int l, r; ll w, f;
}tree[A];
ll s[A], delta;
int n, m, a[A], x, y;
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {
tree[k].w = s[l];
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void down(int k) {
tree[k << 1].f += tree[k].f; tree[k << 1 | 1].f += tree[k].f;
tree[k << 1].w += tree[k].f * (tree[k << 1].r - tree[k << 1].l + 1);
tree[k << 1 | 1].w += tree[k].f * (tree[k << 1 | 1].r - tree[k << 1 | 1].l + 1);
tree[k].f = 0;
}
void change(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) {
tree[k].w += delta * ll(tree[k].r - tree[k].l + 1);
tree[k].f += delta;
return;
}
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) change(k << 1, l, r);
if (r > m) change(k << 1 | 1, l, r);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
ll ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1; ll ans = 0;
if (l <= m) ans += ask(k << 1, l, r);
if (r > m) ans += ask(k << 1 | 1, l, r);
return ans;
}
int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s[i] = s[i - 1] + a[i];
build(1, 1, n);
while (m--) {
char opt[7]; scanf("%s", opt);
if (opt[0] == 'Q') scanf("%d", &x), printf("%lld\n", ask(1, 1, x));
else scanf("%d%d", &x, &y), delta = y - a[x], a[x] = y, change(1, x, n);
}
return 0;
}