思路
代码
#include <cstdio>
#include <cstring>
int n, m;
struct {
int at[1000005];
long long int arr[1000005 << 2];
void build(int k, int l, int r) {
if (l == r) {
arr[k] = at[l];
return;
}
int mid = l + r >> 1;
this->build(k << 1, l, mid);
this->build(k << 1 | 1, mid + 1, r);
arr[k] = arr[k << 1] + arr[k << 1 | 1];
}
void change(int k, int l, int r, int x, int y, int v) {
if (x > r || y < l) return;
if (x <= l && r <= y) {
arr[k] += v;
return;
}
int mid = l + r >> 1;
this->change(k << 1, l, mid, x, y, v);
this->change(k << 1 | 1, mid + 1, r, x, y, v);
arr[k] = arr[k << 1] + arr[k << 1 | 1];
}
long long query(int k, int l, int r, int x, int y) {
if (x > r || y < l) return 0;
if (x <= l && r <= y) return arr[k];
int mid = l + r >> 1;
return this->query(k << 1, l, mid, x, y) + this->query(k << 1 | 1, mid + 1, r, x, y);
}
} t;
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * f;
}
int main() {
n = read(), m = read();
for (register int i = 1; i <= n; ++i) t.at[i] = read();
t.build(1, 1, n);
for (register int i = 1, a, b, c; i <= m; ++i) {
a = read(), b = read(), c = read();
if (a == 1) {
t.change(1, 1, n, b, b, c);
} else {
printf("%lld
", t.query(1, 1, n, b, c));
}
}
return 0;
}