-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
381 lines (327 loc) · 8.26 KB
/
main.cpp
File metadata and controls
381 lines (327 loc) · 8.26 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <iostream>
#include <initializer_list>
#include <algorithm>
template<class ValueType>
struct node {
const ValueType key;
int height;
size_t size;
node* parent;
node* left;
node* right;
node(ValueType key) :
key(key),
height(1),
size(1),
parent(nullptr),
left(nullptr),
right(nullptr) {}
node(const node<ValueType>& other) :
key(other.key),
height(other.height),
size(other.size),
parent(other.parent),
left(other.left),
right(other.right) {}
};
template<typename ValueType>
class SetIterator {
public:
typedef node<ValueType>* tree;
typedef SetIterator<ValueType> iterator;
private:
tree t_;
size_t tree_size_;
size_t cnt_;
tree find_min(tree t) const {
return t->left ? find_min(t->left) : t;
}
tree find_max(tree t) const {
return t->right ? find_max(t->right) : t;
}
public:
explicit SetIterator(tree t = nullptr, size_t tree_size = 0, size_t cnt = 1)
: t_(t),
tree_size_(tree_size),
cnt_(cnt)
{}
bool operator==(const SetIterator& other) {
return cnt_ == other.cnt_;
}
bool operator!=(const SetIterator& other) {
return !(*this == other);
}
iterator& operator++() {
cnt_ = std::min(cnt_ + 1, tree_size_ + 1);
if (cnt_ > tree_size_)
return *this;
if (t_->right) {
t_ = find_min(t_->right);
} else if (t_->parent->left == t_) {
t_ = t_->parent;
} else {
tree prev = t_;
t_ = t_->parent;
while (!t_->right || t_->right == prev) {
prev = t_;
t_ = t_->parent;
}
}
return *this;
}
iterator operator++(int) {
iterator tmp(t_, cnt_);
++*this;
return tmp;
}
iterator& operator--() {
if (cnt_ == 1)
return *this;
--cnt_;
if (cnt_ == tree_size_)
return *this;
if (t_->left) {
t_ = find_max(t_->left);
} else if (t_->parent->right == t_) {
t_ = t_->parent;
} else {
tree prev = t_;
t_ = t_->parent;
while (!t_->left || t_->left == prev) {
prev = t_;
t_ = t_->parent;
}
}
return *this;
}
iterator operator--(int) {
iterator tmp(t_, cnt_);
--*this;
return tmp;
}
const ValueType operator*() const {
return t_->key;
}
const ValueType* operator->() const {
return &t_->key;
}
};
template<class ValueType>
class Set {
public:
typedef node<ValueType>* tree;
typedef SetIterator<ValueType> iterator;
private:
tree root_;
int height(tree t) const {
return t ? t->height : 0;
}
size_t size(tree t) const {
return t ? t->size : 0;
}
int balance_factor(tree t) const {
return height(t->right) - height(t->left);
}
void set_height(tree t) {
t->height = std::max(height(t->left), height(t->right)) + 1;
}
void set_parent(tree t) {
if (t->right)
t->right->parent = t;
if (t->left)
t->left->parent = t;
}
void set_size(tree t) {
t->size = size(t->left) + size(t->right) + 1;
}
void set_all(tree t) {
if (!t)
return;
set_parent(t);
set_height(t);
set_size(t);
}
tree left_rotate(tree t) {
tree p = t->right;
t->right = p->left;
p->left = t;
set_all(t);
set_all(p);
return p;
}
tree right_rotate(tree t) {
tree p = t->left;
t->left = p->right;
p->right = t;
set_all(t);
set_all(p);
return p;
}
tree find_min(tree t) const {
return t->left ? find_min(t->left) : t;
}
tree find_max(tree t) const {
return t->right ? find_max(t->right) : t;
}
tree balance(tree t) {
if (balance_factor(t) == 2) {
if (balance_factor(t->right) < 0)
t->right = right_rotate(t->right);
set_parent(t);
return left_rotate(t);
}
if (balance_factor(t) == -2) {
if (balance_factor(t->left) > 0)
t->left = left_rotate(t->left);
set_parent(t);
return right_rotate(t);
}
return t;
}
tree insert_rec(tree t, ValueType key) {
if (!t)
return new node<ValueType>(key);
if (t->key < key)
t->right = insert_rec(t->right, key);
else if (key < t->key)
t->left = insert_rec(t->left, key);
set_all(t);
return balance(t);
}
tree erase_min(tree t) {
if (!t->left) {
tree tmp = t->right;
t->right = nullptr;
set_all(t);
return tmp;
}
t->left = erase_min(t->left);
set_all(t);
return balance(t);
}
tree erase_rec(tree t, ValueType key) {
if (!t)
return nullptr;
if (t->key < key) {
t->right = erase_rec(t->right, key);
} else if (key < t->key) {
t->left = erase_rec(t->left, key);
} else {
tree l = t->left;
tree r = t->right;
delete t;
if (!r)
return l;
tree mn = find_min(r);
mn->right = erase_min(r);
mn->left = l;
set_all(mn);
return balance(mn);
}
set_all(t);
return balance(t);
}
void clear(tree t) {
if (!t)
return;
clear(t->left);
clear(t->right);
delete t;
}
tree copy(tree t) {
if (!t)
return nullptr;
tree new_t = new node<ValueType>(*t);
new_t->left = copy(t->left);
new_t->right = copy(t->right);
set_all(new_t);
return new_t;
}
public:
Set(const Set<ValueType>& other) : Set() {
if (this == &other)
return;
clear(root_);
root_ = nullptr;
root_ = copy(other.root_);
set_all(root_);
}
Set<ValueType>& operator=(const Set<ValueType>& other) {
if (this != &other) {
clear(root_);
root_ = nullptr;
root_ = copy(other.root_);
set_all(root_);
}
return *this;
}
Set() : root_(nullptr) {
}
template<typename Iter>
Set(Iter first, Iter last) : Set() {
for (auto it = first; it != last; ++it)
insert(*it);
}
Set(std::initializer_list<ValueType> init_list) : Set() {
for (const auto& el : init_list)
insert(el);
}
void insert(ValueType key) {
root_ = insert_rec(root_, key);
set_all(root_);
}
void erase(ValueType key) {
root_ = erase_rec(root_, key);
set_all(root_);
}
size_t size() const {
return size(root_);
}
bool empty() const {
return size(root_) == 0;
}
iterator begin() const {
if (empty())
return iterator();
tree m = find_min(root_);
return iterator(m, size(root_), 1);
}
iterator end() const {
if (empty())
return iterator();
tree m = find_max(root_);
return iterator(m, size(root_), size(root_) + 1);
}
iterator find(ValueType key) const {
auto it = lower_bound(key);
if (it == end())
return it;
if (*it < key || key < *it) {
return end();
}
return it;
}
iterator lower_bound(ValueType key) const {
tree t = root_, best = nullptr;
size_t cnt = 0, cnt_best = 0;
while (t) {
if (t->key < key) {
cnt += size(t->left) + 1;
t = t->right;
} else {
best = t;
cnt_best = cnt;
if (key < t->key)
t = t->left;
else
break;
}
}
if (!best)
return end();
return iterator(best, size(root_), cnt_best + 1);
}
~Set() {
clear(root_);
}
};