-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVA12086.cpp
executable file
·74 lines (61 loc) · 1.5 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
69
70
71
72
73
74
#include <stdio.h>
#include <string.h>
using namespace std;
int pots[900000];
//Segmend tree implemetation
void insert_update(int idx, int st, int ed, int pos, int value)
{
if(st == pos && pos == ed)
{
pots[idx] = value;
return;
}
int l = idx * 2;
int r = l + 1;
int mid = (st + ed) / 2;
if(pos <= mid) insert_update(l, st, mid, pos, value);
else insert_update(r, mid+1, ed, pos, value);
pots[idx] = pots[l] + pots[r];
}
int quer(int idx, int st, int ed, int i, int j)
{
if(i==st && j==ed) return pots[idx];
int l = idx * 2;
int r = l + 1;
int mid = (st + ed) / 2;
if(j<=mid)
{
return quer(l, st, mid, i, j);
}
else if(i > mid)
{
return quer(r, mid+1, ed, i, j);
}
else return quer(l, st, mid, i, mid) + quer(r, mid+1, ed, mid+1, j);
}
int main()
{
int n, tc = 0, x, y, num;
char type[4];
while (scanf("%d", &n), n)
{
for (int i = 1; i <= n; i++){
scanf("%d", &num);
insert_update(1, 1, n, i, num);
}
if(tc) printf("\n");
printf("Case %d:\n", ++tc);
while (scanf("%s", type), strcmp(type, "END") !=0)
{
scanf("%d %d", &x, &y);
if (type[0] == 'S')
{
insert_update(1, 1, n, x, y);
} else if (type[0] == 'M'){
int sum = quer(1, 1, n, x, y);
printf("%d\n", sum);
}
}
}
return 0;
}