-
Notifications
You must be signed in to change notification settings - Fork 74
/
set_range_sum.py
201 lines (168 loc) · 4.74 KB
/
set_range_sum.py
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
# python3
from sys import stdin
# Splay tree implementation
# Vertex of a splay tree
class Vertex:
def __init__(self, key, sum, left, right, parent):
(self.key, self.sum, self.left, self.right,
self.parent) = (key, sum, left, right, parent)
def update(v):
if v is None:
return
v.sum = v.key + (v.left.sum if v.left is not None else 0) + \
(v.right.sum if v.right is not None else 0)
if v.left is not None:
v.left.parent = v
if v.right is not None:
v.right.parent = v
def smallRotation(v):
parent = v.parent
if parent is None:
return
grandparent = v.parent.parent
if parent.left == v:
m = v.right
v.right = parent
parent.left = m
else:
m = v.left
v.left = parent
parent.right = m
update(parent)
update(v)
v.parent = grandparent
if grandparent is not None:
if grandparent.left == parent:
grandparent.left = v
else:
grandparent.right = v
def bigRotation(v):
if v.parent.left == v and v.parent.parent.left == v.parent:
# Zig-zig
smallRotation(v.parent)
smallRotation(v)
elif v.parent.right == v and v.parent.parent.right == v.parent:
# Zig-zig
smallRotation(v.parent)
smallRotation(v)
else:
# Zig-zag
smallRotation(v)
smallRotation(v)
# Makes splay of the given vertex and makes
# it the new root.
def splay(v):
if v is None:
return None
while v.parent is not None:
if v.parent.parent is None:
smallRotation(v)
break
bigRotation(v)
return v
# Searches for the given key in the tree with the given root
# and calls splay for the deepest visited node after that.
# Returns pair of the result and the new root.
# If found, result is a pointer to the node with the given key.
# Otherwise, result is 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,
# then result is None.
def find(root, key):
v = root
last = root
next = None
while v is not None:
if v.key >= key and (next is None or v.key < next.key):
next = v
last = v
if v.key == key:
break
if v.key < key:
v = v.right
else:
v = v.left
root = splay(last)
return (next, root)
def split(root, key):
(result, root) = find(root, key)
if result is None:
return (root, None)
right = splay(result)
left = right.left
right.left = None
if left is not None:
left.parent = None
update(left)
update(right)
return (left, right)
def merge(left, right):
if left is None:
return right
if right is None:
return left
while right.left is not None:
right = right.left
right = splay(right)
right.left = left
update(right)
return right
root = None
def insert(x):
global root
(left, right) = split(root, x)
new_vertex = None
if right is None or right.key != x:
new_vertex = Vertex(x, x, None, None, None)
root = merge(merge(left, new_vertex), right)
def erase(x):
global root
if search(x) is None:
return
root = splay(root)
root = merge(root.left, root.right)
if root is not None:
root.parent = None
def search(x):
global root
result, root = find(root, x)
if result is None or result.key != x:
return None
return result.key
def sum(fr, to):
global root
(left, middle) = split(root, fr)
(middle, right) = split(middle, to + 1)
if middle is None:
ans = 0
root = merge(left, right)
else:
ans = middle.sum
root = merge(merge(left, middle), right)
return ans
MODULO = 1000000001
n = int(stdin.readline())
last_sum_result = 0
for i in range(n):
line = stdin.readline().split()
if line[0] == '+':
x = int(line[1])
insert((x + last_sum_result) % MODULO)
elif line[0] == '-':
x = int(line[1])
erase((x + last_sum_result) % MODULO)
elif line[0] == '?':
x = int(line[1])
print(
'Found' if search(
(x + last_sum_result) %
MODULO) is not None else 'Not found')
elif line[0] == 's':
m = int(line[1])
r = int(line[2])
res = sum(
(m + last_sum_result) %
MODULO, (r + last_sum_result) %
MODULO)
print(res)
last_sum_result = res % MODULO