-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTree.cpp
More file actions
157 lines (121 loc) · 3.18 KB
/
SegmentTree.cpp
File metadata and controls
157 lines (121 loc) · 3.18 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/// UVa OJ - 12086
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200001;
vector<int>OHM(MAX, 0);
vector<int>TREE(MAX * 4, 0);
void fastScan(int &number);
void buildTree(int node, int beg, int end);
void updateTree(int node, int beg, int end, int x, int newValue);
int queryTree(int node, int beg, int end, int x, int y);
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int test = 1, n, i, x, y, ans;
char op[5];
while (true) {
//scanf("%d", &n);
fastScan(n);
if (!n) {
break;
}
for (i = 1; i <= n; i++) {
//scanf("%d", &OHM[i]);
fastScan(OHM[i]);
}
buildTree(1, 1, n);
if (test != 1) {
printf("\n");
}
printf("Case %d:\n", test++);
while (true) {
scanf("%s", op);
getchar();
if (op[0] == 'E') {
break;
}
//scanf("%d%d", &x, &y);
fastScan(x);
fastScan(y);
switch (op[0]) {
case 'S':
updateTree(1, 1, n, x, y);
break;
case 'M':
ans = queryTree(1, 1, n, x, y);
printf("%d\n", ans);
break;
}
}
}
return 0;
}
void fastScan(int &number)
{
/// Variable to indicate sign of input number
bool negative = false;
register int c;
number = 0;
/// Extract current character from buffer
c = getchar();
if (c == '-')
{
/// Number is negative
negative = true;
/// Extract the next character from the buffer
c = getchar();
}
/// Keep on extracting characters if they are integers
/// i.e ASCII Value lies from '0'(48) to '9' (57)
for (; (c > 47 && c < 58); c = getchar())
number = number *10 + c - 48;
/// If scanned input has a negative sign,
/// Negate the value of the input number
if (negative)
number *= -1;
}
void buildTree(int node, int beg, int end)
{
if (beg == end) {
TREE[node] = OHM[beg];
return;
}
int Left = node * 2;
int Right = Left + 1;
int mid = (beg + end) / 2;
buildTree(Left, beg, mid);
buildTree(Right, mid + 1, end);
TREE[node] = TREE[Left] + TREE[Right];
}
void updateTree(int node, int beg, int end, int x, int newValue)
{
if (x > end || x < beg) {
return;
}
if (beg >= x && end <= x) {
TREE[node] = newValue;
return;
}
int Left = node * 2;
int Right = Left + 1;
int mid = (beg + end) / 2;
updateTree(Left, beg, mid, x, newValue);
updateTree(Right, mid + 1, end, x, newValue);
TREE[node] = TREE[Left] + TREE[Right];
}
int queryTree(int node, int beg, int end, int x, int y)
{
if (x > end || y < beg) {
return 0;
}
if (beg >= x && end <= y) {
return TREE[node];
}
int Left = node * 2;
int Right = Left + 1;
int mid = (beg + end) / 2;
int sumLeft = queryTree(Left, beg, mid, x, y);
int sumRight = queryTree(Right, mid + 1, end, x, y);
return sumLeft + sumRight;
}