-
Notifications
You must be signed in to change notification settings - Fork 39
/
treap_full.cpp
99 lines (83 loc) · 2.27 KB
/
treap_full.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
class Treap {
int key, subsize;
unsigned priority;
Treap *left, *right;
static Treap *nill;
static bool initialized;
Treap* pull() {
subsize = (this != nill) + left->subsize + right->subsize;
return this;
}
static void initialize() {
if (initialized) return;
initialized = true;
nill->left = nill->right = nill;
nill->priority = 0;
nill->subsize = 0;
}
int get_key();
Treap() {}
public:
static Treap* Nill() { initialize(); return nill; }
static Treap* Singleton(int key) {
initialize();
Treap* ret = new Treap();
ret->key = key;
ret->priority = rand() * RAND_MAX + rand();
ret->left = ret->right = nill;
return ret->pull();
}
pair<Treap*, Treap*> Split(int key) {
if (this == nill) return {nill, nill};
if (get_key() > key) {
auto p = left->Split(key);
left = p.second;
p.second = this->pull();
return p;
} else {
auto p = right->Split(key);
right = p.first;
p.first = this->pull();
return p;
}
}
Treap* Join(Treap *rhs) {
if (this == nill) return rhs;
if (rhs == nill) return this;
if (priority > rhs->priority) {
right = right->Join(rhs);
return this->pull();
} else {
rhs->left = Join(rhs->left);
return rhs->pull();
}
}
bool Find(int key) {
if (this == nill) return false;
if (get_key() == key) return true;
if (get_key() > key) return left->Find(key);
return right->Find(key);
}
void Dump() {
if (this == nill) return;
left->Dump();
cerr << get_key() << " ";
right->Dump();
}
};
Treap* Treap::nill = new Treap();
bool Treap::initialized = false;
int Treap::get_key() {
return key;
// return left->subsize + 1;
}
Treap* Insert(Treap *root, int key) {
Treap* single = Treap::Singleton(key);
auto p = root->Split(key);
return p.first->Join(single)->Join(p.second);
}
Treap* Erase(Treap *root, int key) {
auto p = root->Split(key);
auto q = p.second->Split(key + 1);
return p.first->Join(q.second);
}