-
Notifications
You must be signed in to change notification settings - Fork 4
/
ALLIN1.cpp
225 lines (193 loc) · 4.67 KB
/
ALLIN1.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
// https://www.spoj.com/problems/ALLIN1/
// Time complexity: all queries are O(log(N))
// Space complexity: O(N)
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef struct Node *pnode;
struct Node {
int key, prior, cnt, sub;
pnode l, r;
Node(int key) : key(key), prior(rand()), cnt(1), sub(1), l(nullptr), r(nullptr) {}
};
struct Treap {
private:
pnode root;
void clear(pnode t) {
if (t) {
clear(t->l);
clear(t->r);
delete t;
}
}
int sub(pnode t) {
return t ? t->sub : 0;
}
void upd(pnode t) {
if (t) {
t->sub = t->cnt + sub(t->l) + sub(t->r);
}
}
void split(pnode t, int key, pnode &l, pnode &r) {
if (!t) {
l = r = nullptr;
} else if (key < t->key) {
split(t->l, key, l, t->l), r = t;
} else {
split(t->r, key, t->r, r), l = t;
}
upd(t);
}
void merge(pnode &t, pnode l, pnode r) {
if (!l || !r) {
t = l ? l : r;
} else if (l->prior > r->prior) {
merge(l->r, l->r, r), t = l;
} else {
merge(r->l, l, r->l), t = r;
}
upd(t);
}
pnode find(pnode t, int key) {
if (!t) {
return nullptr;
}
if (t->key == key) {
return t;
}
if (key < t->key) {
return find(t->l, key);
} else {
return find(t->r, key);
}
}
void updPath(pnode t, int key) {
if (!t) {
return;
}
if (t->key == key) {}
else if (key < t->key) {
updPath(t->l, key);
} else {
updPath(t->r, key);
}
upd(t);
}
void insert(pnode &t, pnode it) {
if (!t) {
t = it;
} else if (t->prior > it->prior) {
insert(t->key <= it->key ? t->r : t->l, it);
} else {
split(t, it->key, it->l, it->r), t = it;
}
upd(t);
}
void erase(pnode &t, int key) {
if (!t) {
return;
}
if (t->key == key) {
pnode toDel = t;
merge(t, t->l, t->r);
delete toDel;
upd(t);
} else {
erase(t->key < key ? t->r : t->l, key);
upd(t);
}
}
int rank(pnode t, int key) {
int rank = 1;
while (t) {
if (key < t->key) {
t = t->l;
} else if (key > t->key) {
rank += sub(t->l) + t->cnt;
t = t->r;
} else {
return rank + sub(t->l);
}
}
return -1;
}
int kthSmallest(pnode t, int k) {
if (sub(t) < k) {
return -1;
}
while (t) {
int lst = sub(t->l);
if (lst >= k) {
t = t->l;
} else if (lst + t->cnt >= k) {
return t->key;
} else {
k -= lst + t->cnt;
t = t->r;
}
}
return -1; // we will never reach here
}
public:
Treap() : root(nullptr) {
srand(time(nullptr));
}
~Treap() {
clear(this->root);
}
void smartInsert(int key) { // if it exist - increment and update Path, else insert new element
pnode found = find(this->root, key);
if (!found) {
insert(this->root, new Node(key));
} else {
++found->cnt;
updPath(this->root, key);
}
}
void smartErase(int key) {
pnode found = find(this->root, key);
if (found) {
int freq = --found->cnt;
if (!freq) {
erase(this->root, key);
}
updPath(this->root, key);
}
}
int rank(int key) { // rank = index of key if all keys were sorted
return rank(this->root, key);
}
int kthSmallest(int k) {
return kthSmallest(this->root, k);
}
};
int main() {
Treap t;
int type, x;
while (scanf("%d", &type) && type != -1) {
scanf("%d", &x);
switch (type) {
case 1: {
t.smartInsert(x);
break;
}
case 2: {
t.smartErase(x);
break;
}
case 3: {
printf("%d\n", t.rank(x));
break;
}
case 4: {
printf("%d\n", t.kthSmallest(x));
break;
}
default: {
printf("ivalid query type\n");
}
}
}
return 0;
}