-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_range_sum.py
172 lines (151 loc) · 4.04 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
# 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 == None:
return
v.sum = v.key + (v.left.sum if v.left != None else 0) + (v.right.sum if v.right != None else 0)
if v.left != None:
v.left.parent = v
if v.right != None:
v.right.parent = v
def smallRotation(v):
parent = v.parent
if parent == 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 != 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 == None:
return None
while v.parent != None:
if v.parent.parent == 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 != None:
if v.key >= key and (next == 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 == None:
return (root, None)
right = splay(result)
left = right.left
right.left = None
if left != None:
left.parent = None
update(left)
update(right)
return (left, right)
def merge(left, right):
if left == None:
return right
if right == None:
return left
while right.left != None:
right = right.left
right = splay(right)
right.left = left
update(right)
return right
# Code that uses splay tree to solve the problem
root = None
def insert(x):
global root
(left, right) = split(root, x)
new_vertex = None
if right == 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
# Implement erase yourself
pass
def search(x):
global root
# Implement find yourself
return False
def sum(fr, to):
global root
(left, middle) = split(root, fr)
(middle, right) = split(middle, to + 1)
ans = 0
# Complete the implementation of sum
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) else 'Not found')
elif line[0] == 's':
l = int(line[1])
r = int(line[2])
res = sum((l + last_sum_result) % MODULO, (r + last_sum_result) % MODULO)
print(res)
last_sum_result = res % MODULO