-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva12086.cpp
68 lines (57 loc) · 1.41 KB
/
uva12086.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdio>
#include <cstring>
int arr[200005];
struct Node {
int sum;
} node [400100];
int LC(int i) { return (i << 1); }
int RC(int i) { return (i << 1) + 1; }
void build() {
memset(node, 0, sizeof(node));
}
void update(int L, int R, int idx, int pos, int value) {
if(L == R) {
node[idx].sum = value;
return;
}
int M = (L + R) >> 1;
if(pos <= M) update(L, M, LC(idx), pos, value);
if(pos > M) update(M+1, R, RC(idx), pos, value);
node[idx].sum = node[LC(idx)].sum + node[RC(idx)].sum;
}
int query(int L, int R, int idx, int tL, int tR) {
if(tL <= L && R <= tR || node[idx].sum == 0) {
return node[idx].sum;
}
int total = 0;
int M = (L + R) >> 1;
if (tL <= M) total += query(L, M, LC(idx), tL, tR);
if (tR > M) total += query(M+1, R, RC(idx), tL, tR);
return total;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout)
int n, tmp, x, y;
int count = 1;
char command[5];
while(scanf("%d", &n) != EOF) {
build();
if(count != 1) printf("\n");
printf("Case %d\n", count++);
for(int i = 1; i <= n; ++i) {
scanf("%d", &tmp);
update(1, n, 1, i, tmp);
}
while(scanf("%s", command) && command[0] != 'E') {
scanf("%d%d", &x, &y);
if(command[0] == 'S') {
update(1, n, 1, x, y);
}
if(command[0] == 'M') {
printf("%d\n", query(1, n, 1, x, y));
}
}
}
return 0;
}