-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtreap.cpp
More file actions
227 lines (182 loc) · 4.41 KB
/
treap.cpp
File metadata and controls
227 lines (182 loc) · 4.41 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
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
#include <bits/stdc++.h>
using namespace std;
struct treap{
private:
struct node{
int key, prior;
node *l = nullptr, *r = nullptr;
int cnt = 1;
node(int k) : key(k), prior(rand()) {}
};
node *t = nullptr;
int cnt(node *n){
return n?n->cnt:0;
}
void upd_cnt(node *n){
if (n) n->cnt = 1+cnt(n->l)+cnt(n->r);
}
void split(node *root, int key, node *&l, node *&r){
if (!root) l = r = nullptr;
else if (key <= root->key){
r = root;
split(root->l, key, l, root->l);
}
else{
l = root;
split(root->r, key, root->r, r);
}
upd_cnt(l);
upd_cnt(r);
}
void insert(node *&root, node *element){
if (!root) return void(root = element);
if (element->prior > root->prior){
split(root, element->key, element->l, element->r);
root = element;
}
else{
if (element->key < root->key) insert(root->l, element);
else insert(root->r, element);
}
upd_cnt(root);
}
void merge(node *&root, node *l, node *r){
if (!r || !l) root = l?l:r;
else if (l->prior > r->prior){
root = l;
merge(l->r, l->r, r);
}
else{
root = r;
merge(r->l, l, r->l);
}
upd_cnt(root);
}
void erase(node *&root, int key){
if (!root) return;
if (root->key == key) {
node *aux = root;
merge(root, root->l, root->r);
delete aux;
}
else{
if (key < root->key) erase(root->l, key);
else erase(root->r, key);
}
upd_cnt(root);
}
void del(node *n){
if (!n) return;
del(n->l);
del(n->r);
delete n;
}
/* ==================== EXTRA ==================== */
int kth(node *root, int pos){ // O(log(N))
if (!root) return 0;
int p = 1+cnt(root->l);
if (p == pos) return root->key;
if (p > pos) return kth(root->l, pos);
else return kth(root->r, pos-p);
}
node *unite(node *l, node *r){ // O(M*log(N/M))
if (!l || !r) return l?l:r;
if (l->prior < r->prior) swap(l, r);
node *tl;
node *tr;
split(r, l->key, tl, tr);
l->l = unite(l->l, tl);
l->r = unite(l->r, tr);
upd_cnt(l);
return l;
}
void print(node *n){
if (!n) return;
print(n->l);
cout << n->key << " ";
print(n->r);
}
public:
void insert(int key){
insert(t, new node(key));
}
void erase(int key){
erase(t, key);
}
~treap(){
del(t);
}
/* ==================== EXTRA ==================== */
bool find(int key){
node *aux = t;
while(aux && aux->key != key){
if (key < aux->key) aux = aux->l;
else aux = aux->r;
}
return aux != nullptr;
}
int order_of_key(int key){
int res = 0;
node *l = nullptr, *r = nullptr;
split(t, key, l, r);
res = cnt(l);
merge(t, l, r);
return res;
}
void erase_range(int l, int r){
node *tl = nullptr, *tr = nullptr, *aux = nullptr;
split(t, l, tl, aux);
split(aux, r+1, aux, tr);
del(aux);
merge(t, tl, tr);
}
int kth(int pos){
return kth(t, pos+1);
}
void unite(treap &b){
t = unite(t, b.t);
b.t = nullptr;
}
int size(){
return cnt(t);
}
void print(){
print(t);
cout << endl;
}
};
int main(){
treap k;
k.insert(-5);
k.insert(20);
treap t;
t.insert(11);
t.insert(200);
t.insert(156);
t.insert(7);
t.insert(10);
t.insert(12);
cout << "Treap t" << endl;
t.print();
cout << endl;
cout << "Treap k" << endl;
k.print();
cout << endl;
cout << "Treap t + k" << endl;
t.unite(k);
t.print();
cout << endl;
cout << "Erase [10, 100]" << endl;
t.erase_range(10, 100);
t.print();
cout << endl;
cout << "3-th element" << endl;
cout << t.kth(3) << endl;
cout << endl;
cout << "order of key 156" << endl;
cout << t.order_of_key(156) << endl;
cout << endl;
cout << "find 11" << endl;
cout << t.find(11) << endl;
return 0;
}