-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathP3374.c
More file actions
44 lines (39 loc) · 673 Bytes
/
P3374.c
File metadata and controls
44 lines (39 loc) · 673 Bytes
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
#include "stdio.h"
#include "stdlib.h"
int * c, n;
int lowbit(int x) {
return x & -x;
}
void add(int x, int k) {
for (int i = x; i <= n; i += lowbit(i)) {
c[i] += k;
}
}
int sum(int x) {
int result = 0;
for (int i = x; i > 0; i -= lowbit(i)) {
result += c[i];
}
return result;
}
int main() {
int m, i, cmd, x, y;
scanf("%d %d", &n, &m);
c = (int *)malloc((n + 1) * sizeof(int));
for (i = 1; i <= n; i++) c[i] = 0;
for (i = 1; i <= n; i++) {
scanf("%d", &x);
add(i, x);
}
for (i = 0; i < m; i++) {
scanf("%d %d %d", &cmd, &x, &y);
if (cmd == 1) {
add(x, y);
}
else {
printf("%d\n", sum(y) - sum(x - 1));
}
}
free(c);
return 0;
}