-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrangesum.cpp
329 lines (302 loc) · 7.26 KB
/
rangesum.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <cstdio>
// Splay tree implementation
// Vertex of a splay tree
struct Vertex {
int key;
// Sum of all the keys in the subtree - remember to update
// it after each operation that changes the tree.
long long sum;
Vertex* left;
Vertex* right;
Vertex* parent;
Vertex(int key, long long sum, Vertex* left, Vertex* right, Vertex* parent)
: key(key)
, sum(sum)
, left(left)
, right(right)
, parent(parent)
{
}
};
void update(Vertex* v)
{
if (v == NULL)
return;
v->sum = v->key + (v->left != NULL ? v->left->sum : 0ll) + (v->right != NULL ? v->right->sum : 0ll);
if (v->left != NULL) {
v->left->parent = v;
}
if (v->right != NULL) {
v->right->parent = v;
}
}
void small_rotation(Vertex* v)
{
Vertex* parent = v->parent;
if (parent == NULL) {
return;
}
Vertex* grandparent = v->parent->parent;
if (parent->left == v) {
Vertex* m = v->right;
v->right = parent;
parent->left = m;
}
else {
Vertex* m = v->left;
v->left = parent;
parent->right = m;
}
update(parent);
update(v);
v->parent = grandparent;
if (grandparent != NULL) {
if (grandparent->left == parent) {
grandparent->left = v;
}
else {
grandparent->right = v;
}
}
}
void big_rotation(Vertex* v)
{
if (v->parent->left == v && v->parent->parent->left == v->parent) {
// Zig-zig
small_rotation(v->parent);
small_rotation(v);
}
else if (v->parent->right == v && v->parent->parent->right == v->parent) {
// Zig-zig
small_rotation(v->parent);
small_rotation(v);
}
else {
// Zig-zag
small_rotation(v);
small_rotation(v);
}
}
// Makes splay of the given vertex and makes
// it the new root.
void splay(Vertex*& root, Vertex* v)
{
if (v == NULL)
return;
while (v->parent != NULL) {
if (v->parent->parent == NULL) {
small_rotation(v);
break;
}
big_rotation(v);
}
root = v;
}
// Searches for the given key in the tree with the given root
// and calls splay for the deepest visited node after that.
// If found, returns a pointer to the node with the given key.
// Otherwise, returns a pointer to the node with the smallest
// bigger key (next value in the order).
// If the key is bigger than all keys in the tree,
// returns NULL.
Vertex* find(Vertex*& root, int key)
{
Vertex* v = root;
Vertex* last = root;
Vertex* next = NULL;
while (v != NULL) {
if (v->key >= key && (next == NULL || v->key < next->key)) {
next = v;
}
last = v;
if (v->key == key) {
break;
}
if (v->key < key) {
v = v->right;
}
else {
v = v->left;
}
}
splay(root, last);
return next;
}
void split(Vertex* root, int key, Vertex*& left, Vertex*& right)
{
right = find(root, key);
splay(root, right);
if (right == NULL) {
left = root;
return;
}
left = right->left;
right->left = NULL;
if (left != NULL) {
left->parent = NULL;
}
update(left);
update(right);
}
Vertex* merge(Vertex* left, Vertex* right)
{
if (left == NULL)
return right;
if (right == NULL)
return left;
Vertex* min_right = right;
while (min_right->left != NULL) {
min_right = min_right->left;
}
splay(right, min_right);
right->left = left;
update(right);
return right;
}
// Code that uses splay tree to solve the problem
Vertex* root = NULL;
void insert(int x)
{
Vertex* left = NULL;
Vertex* right = NULL;
Vertex* new_vertex = NULL;
split(root, x, left, right);
if (right == NULL || right->key != x) {
new_vertex = new Vertex(x, x, NULL, NULL, NULL);
}
root = merge(merge(left, new_vertex), right);
}
Vertex* left_descendant(Vertex* n)
{
if (n->left == NULL) {
return n;
}
else {
return left_descendant(n->left);
}
}
Vertex* right_ancestor(Vertex* n)
{
if (n->key < n->parent->key) {
return n->parent;
}
else {
return right_ancestor(n->parent);
}
}
Vertex* next(Vertex* n)
{
if (n->right != NULL) {
return left_descendant(n->right);
}
else {
return right_ancestor(n);
}
}
void delete_node(Vertex* n)
{
if (n->right == NULL) {
root = n->left;
}
else {
Vertex* x = next(n);
n = x;
root = n->right;
root->left = n->left;
}
if (root != NULL) {
root->parent = NULL;
}
update(root);
}
void erase(int x)
{
Vertex* next = find(root, x + 1);
if (next != NULL) {
splay(root, next);
}
Vertex* n = find(root, x);
if (n != NULL) {
if (n->key == x) {
delete_node(n);
}
}
}
bool find(int x)
{
if (root == NULL)
return false;
if (root->key == x)
return true;
else if (root->key > x) {
if (root->left == NULL)
return false;
else {
root = root->left;
return find(x);
}
}
else if (root->key < x) {
if (root->right == NULL)
return false;
else {
root = root->right;
return find(x);
}
}
return false;
}
long long sum(int from, int to)
{
Vertex* left = NULL;
Vertex* middle = NULL;
Vertex* right = NULL;
split(root, from, left, middle);
split(middle, to + 1, middle, right);
long long ans = 0;
if (left != NULL && left->key >= from && left->key <= to)
ans += left->sum;
if (middle != NULL && middle->key >= from && middle->key <= to)
ans += middle->sum;
if (right != NULL && right->key >= from && right->key <= to)
ans += right->sum;
root = merge(merge(left, middle), right);
return ans;
}
const int MODULO = 1000000001;
int main()
{
int n;
scanf("%d", &n);
int last_sum_result = 0;
for (int i = 0; i < n; i++) {
char buffer[10];
scanf("%s", buffer);
char type = buffer[0];
switch (type) {
case '+': {
int x;
scanf("%d", &x);
insert((x + last_sum_result) % MODULO);
} break;
case '-': {
int x;
scanf("%d", &x);
erase((x + last_sum_result) % MODULO);
} break;
case '?': {
int x;
scanf("%d", &x);
printf(find((x + last_sum_result) % MODULO) ? "Found\n" : "Not found\n");
} break;
case 's': {
int l, r;
scanf("%d %d", &l, &r);
long long res = sum((l + last_sum_result) % MODULO, (r + last_sum_result) % MODULO);
printf("%lld\n", res);
last_sum_result = int(res % MODULO);
}
}
}
return 0;
}