洛谷 P3368 【模板】树状数组 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdio>

using namespace std;
const int N = 500001;
int n, m, x, y, k, a[N], c[N];

int lowbit(int x) {
return x & (-x);
}

void update(int i, int k) {
while (i <= n) {
c[i] += k;
i += lowbit(i);
}
}

int sum(int i) {
int s = 0;
while (i > 0) {
s += c[i];
i -= lowbit(i);
}
return s;
}

int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
update(i, a[i] - a[i - 1]);
}
for (int i = 0; i < m; ++i) {
scanf("%d", &x);
if (x == 1) {
scanf("%d%d%d", &x, &y, &k);
update(x, k);
update(y + 1, -k);
} else {
scanf("%d", &x);
printf("%d\n", sum(x));
}
}
return 0;
}