-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplayTree.cpp
292 lines (257 loc) · 7.86 KB
/
SplayTree.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
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define fixed(n) fixed << setprecision(n)
#define ceil(n, m) (((n) + (m) - 1) / (m))
#define add_mod(a, b, m) (((a % m) + (b % m)) % m)
#define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
#define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define sz(x) int(x.size())
#define debug(x) cout << #x << ": " << (x) << "\n";
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define EPS 1e-9
constexpr int INF = 1 << 30, Mod = 1e9 + 7;
constexpr ll LINF = 1LL << 62;
#define PI acos(-1)
template < typename T = int > using Pair = pair < T, T >;
vector < string > RET = {"NO", "YES"};
template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
for (auto &x : v) in >> x;
return in;
}
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
for (const T &x : v) out << x << ' ';
return out;
}
template < typename T = int > struct SplayTree {
struct Node {
Node *ch[2], *par;
T val;
int subSz, freq;
Node() : subSz(0), freq(0) {
par = ch[0] = ch[1] = this;
val = numeric_limits < T > :: min();
}
Node(T V) : val(V), subSz(1), freq(1) {
par = ch[0] = ch[1] = EMPTY;
}
void update() {
subSz = freq + ch[0] -> subSz + ch[1] -> subSz;
}
};
static Node* EMPTY;
Node *root;
enum dir {LEFT, RIGHT};
SplayTree(){
root = EMPTY;
}
// Link two nodes with direction d
void link(Node *p, Node *c, int d){
if(p != EMPTY) p -> ch[d] = c, p -> update();
if(c != EMPTY) c -> par = p;
}
// 0 for LEFT, 1 for RIGHT
int get_dir(Node *p, Node *c){
return p -> ch[RIGHT] == c;
}
/*
gp gp
| gd | gd
p q
/ \d !d/ \
a q -> <- p c
!d/ \ / \d
b c a b
*/
// rotate node p with direction d
void rotate(Node *p, int d){
Node *q = p -> ch[d];
Node *gp = p -> par;
int gd = get_dir(gp, p);
link(p, q -> ch[!d], d);
link(q, p, !d);
link(gp, q, gd);
}
// splay node p to the root of the tree it belongs to
void splay(Node *q){
// splay p until it becomes the root of the tree
while(q -> par != EMPTY){
Node *p = q -> par;
Node *gp = p -> par;
int d1 = get_dir(p, q);
int d2 = get_dir(gp, p);
if(gp == EMPTY){ // direct parent
rotate(p, d1);
}else if(d1 == d2){ // zig-zig
rotate(gp, d2);
rotate(p, d1);
}else { // zig-zag
rotate(p, d1);
rotate(gp, d2);
}
}
root = q;
}
// find node with value val or the node that should be the parent of the node with value val
Node* find(Node *p, T val){
if(p == EMPTY) return EMPTY;
Node * ch = p -> ch[val > p -> val];
if(p -> val == val || ch == EMPTY) return p;
return find(ch, val);
}
// splay node with value val to the root of the tree it belongs to
Node* splay_by_value(Node *p, T val){
p = find(p, val);
splay(p);
return p;
}
// insert node with value val to the tree
Node* insert(Node *p, T val){
if(p == EMPTY) return new Node(val);
p = splay_by_value(p, val);
if(p -> val == val){
p -> freq++;
p -> subSz++;
return p;
}
Node *q = new Node(val);
if(p -> ch[val > p -> val] != EMPTY){
auto ch = p -> ch[val > p -> val];
link(p, EMPTY, val > p -> val);
link(q, ch, q -> val < ch -> val);
link(q, p, q -> val < p -> val);
p = q;
}else
link(p, q, val > p -> val);
return p;
}
// insert node with value val to the tree
void insert(T val){
root = insert(root, val);
}
// split tree into two trees, one with values less than val and the other with values greater than or equal val
void split(Node *p, T val, Node* &ls, Node * &ge){
p = splay_by_value(p, val);
if(p -> val < val){
ls = p;
ge = p -> ch[RIGHT];
link(ls, EMPTY, RIGHT);
link(EMPTY, ge, LEFT);
}else {
ls = p -> ch[LEFT];
ge = p;
link(ge, EMPTY, LEFT);
link(EMPTY, ls, RIGHT);
}
}
// merge two trees into one tree
Node* merge(Node *ls, Node *ge){
if(ls == EMPTY) return ge;
if(ge == EMPTY) return ls;
ge = splay_by_value(ge, numeric_limits < T > :: min());
link(ge, ls, LEFT);
return ge;
}
// erase node with value val from the tree
Node* erase(Node *p, T val){
p = splay_by_value(p, val);
if(p -> val != val) return p;
if(p -> freq > 1){
p -> freq--;
p -> subSz--;
return p;
} else {
Node *ls = p -> ch[LEFT];
Node *ge = p -> ch[RIGHT];
delete p;
link(EMPTY, ls, LEFT);
link(EMPTY, ge, RIGHT);
return merge(ls, ge);
}
}
// erase node with value val from the tree
void erase(T val){
root = erase(root, val);
}
// find the kth smallest value in the tree
Node* kth(Node *p, T k){
if(p == EMPTY) return EMPTY;
if(k > p -> subSz) return EMPTY;
int sz = p -> ch[LEFT] -> subSz;
if(sz > k) return kth(p -> ch[LEFT], k);
if(sz + p -> freq <= k) return kth(p -> ch[RIGHT], k - sz - p -> freq);
return p;
}
// find the kth smallest value in the tree
T kth(T k){
auto p = kth(root, k);
splay(p);
root = p;
return p -> val;
}
// count the number of values less than val in the tree
int count_less(T val){
root = splay_by_value(root, val);
return root -> ch[LEFT] -> subSz + (root -> val < val ? root -> freq : 0);
}
// get the size of the subtree rooted at node p
int get_size(){
return root -> subSz;
}
void print(Node* p, int depth){
if(p == EMPTY) return;
print(p -> ch[LEFT], depth + 1);
cout << string(2 * depth, ' ') << setw(2) << p -> val << "\n";
print(p -> ch[RIGHT], depth + 1);
}
void print(){
print(root, 0);
cout << "-----------------------------------\n";
}
bool search(T val){
root = splay_by_value(root, val);
return root -> val == val;
}
};
template < typename T > typename SplayTree < T > :: Node* SplayTree < T > :: EMPTY = new typename SplayTree < T > :: Node();
void Solve(){
int n;
cin >> n;
SplayTree < int > st;
while(n--){
char type;
int x;
cin >> type >> x;
switch(type) {
case 'I':
if(!st.search(x)) st.insert(x);
break;
case 'D':
st.erase(x);
break;
case 'K':
if(st.get_size() < x) cout << "invalid\n";
else cout << st.kth(--x) << "\n";
break;
case 'C':
cout << st.count_less(x) << "\n";
break;
}
// st.print();
}
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int test_cases = 1;
// cin >> test_cases;
for(int tc = 1; tc <= test_cases; tc++){
// cout << "Case #" << tc << ": ";
Solve();
}
return 0;
}