-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathP2068.c
More file actions
45 lines (39 loc) · 730 Bytes
/
P2068.c
File metadata and controls
45 lines (39 loc) · 730 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
45
#include "stdio.h"
#include "stdlib.h"
int * c, n;
int lowbit(int x) {
return x & -x;
}
void add(int x, int k) {
for (; x <= n; x += lowbit(x))
c[x - 1] += k; //c从下标0开始使用
}
int sum(int x) {
int result = 0;
for (; x > 0; x -= lowbit(x))
result += c[x - 1]; //c从下标0开始使用
return result;
}
int range_sum(int x, int y) {
return sum(y) - sum(x - 1);
}
int main() {
scanf("%d", &n);
c = (int *)malloc(n * sizeof(int)); //c从下标0开始使用
for (int i = 0; i < n; i++) c[i] = 0;
int w;
scanf("%d", &w);
char cmd;
int a, b;
for (int i = 0; i < w; i++) {
scanf(" %c %d %d", &cmd, &a, &b);
if (cmd == 'x') {
add(a, b);
}
else {
printf("%d\n", range_sum(a, b));
}
}
free(c);
return 0;
}