-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreap implementation using rotations.cpp
More file actions
185 lines (166 loc) · 3.59 KB
/
Treap implementation using rotations.cpp
File metadata and controls
185 lines (166 loc) · 3.59 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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bst.in");
ofstream fout("bst.out");
const int mod = 805306457;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct treapNode {
treapNode* l;
treapNode* r;
int key, prior;
treapNode(int _key) : l(nullptr), r(nullptr), key(_key), prior(rng() % mod) {}
};
using pt = pair<treapNode*, treapNode*>;
/*
r R
/ \ Left Rotate / \
L R ———> r Y
/ \ / \
X Y L X
*/
void rotateLeft(treapNode* &node) {
treapNode* R = node->r;
treapNode* X = R->l;
R->l = node;
node->r = X;
node = R;
}
/*
r L
/ \ Right Rotate / \
L R ———> X r
/ \ / \
X Y Y R
*/
void rotateRight(treapNode* &node) {
treapNode* L = node->l;
treapNode* Y = L->r;
L->r = node;
node->l = Y;
node = L;
}
pt split(treapNode* node, int x) {
if (node == nullptr) {
return {nullptr, nullptr};
}
if (node->key < x) {
pt p = split(node->r, x);
node->r = p.first;
return {node, p.second};
}
pt p = split(node->l, x);
node->l = p.second;
return {p.first, node};
}
treapNode* join(treapNode* A, treapNode* B) {
if (A == nullptr) {
return B;
}
if (B == nullptr) {
return A;
}
if (B->prior < A->prior) {
A->r = join(A->r, B);
return A;
}
B->l = join(A, B->l);
return B;
}
void add(treapNode* &node, int key) {
if (node == nullptr) {
node = new treapNode(key);
return;
}
if (key < node->key) {
add(node->l, key);
if (node->l != nullptr && node->l->prior < node->prior) {
rotateRight(node);
}
} else {
add(node->r, key);
if (node->r != nullptr && node->r->prior < node->prior) {
rotateLeft(node);
}
}
}
void rem(treapNode* &node, int key) {
if (node == nullptr) {
return;
}
if (key == node->key) {
if (node->l == nullptr && node->r == nullptr) {
delete node;
node = nullptr;
return;
}
if (node->l != nullptr && node->r != nullptr) {
if (node->r->prior < node->l->prior) {
rotateLeft(node);
rem(node->l, key);
} else {
rotateRight(node);
rem(node->r, key);
}
return;
}
treapNode* son = (node->l != nullptr) ? node->l : node->r;
treapNode* victim = node;
node = son;
delete victim;
return;
}
if (key < node->key) {
rem(node->l, key);
} else {
rem(node->r, key);
}
}
int getMin(treapNode* node) {
if (node->l != nullptr) {
return getMin(node->l);
}
return node->key;
}
int getMax(treapNode* node) {
if (node->r != nullptr) {
return getMax(node->r);
}
return node->key;
}
bool check(treapNode* node, int x) {
if (node == nullptr) {
return false;
}
if (node->key == x) {
return true;
}
if (x < node->key) {
return check(node->l, x);
}
return check(node->r, x);
}
void TestCase() {
int n;
fin >> n;
treapNode* root = nullptr;
for (int i = 1; i <= n; ++i) {
int x;
fin >> x;
add(root, x);
}
for (int i = 1; i <= n; ++i) {
int x = getMin(root);
fout << x << ' ';
rem(root, x);
}
fout << '\n';
}
int main() {
int tests = 1;
for (int tc = 1; tc <= tests; ++tc) {
TestCase();
}
fin.close();
fout.close();
return 0;
}