HDUOJ 1166 敌兵布阵

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
46
47
48
49
50
51
52
53
54
#include <cstdio>
#include <cstring>

using namespace std;
const int N = 50001;
int T, n, x, y, a[N], c[N];
char s[5];

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", &T);
for (int t = 1; t <= T; ++t) {
scanf("%d", &n);
memset(a, 0, (n + 1) * 4);
memset(c, 0, (n + 1) * 4);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
update(i, a[i]);
}
printf("Case %d:\n", t);
while (true) {
scanf("%s", s);
if (s[0] == 'E') break;
scanf("%d%d", &x, &y);
if (s[0] == 'Q') {
printf("%d\n", sum(y) - sum(x - 1));
} else if (s[0] == 'A') {
update(x, y);
} else {
update(x, -y);
}
}
}
return 0;
}