-
Notifications
You must be signed in to change notification settings - Fork 0
/
treap.cpp
111 lines (102 loc) · 2.17 KB
/
treap.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
#include "treap.h"
#include <algorithm>
void treap::recalc()
{
items = 1;
max_value = value;
if(left)
{
items += left->items;
max_value = max(max_value, left->max_value);
}
if(right)
{
items += right->items;
max_value = max(max_value, right->max_value);
}
}
treap* treap::root()
{
return parent ? parent->root() : this;
}
int treap::index()
{
int ans = ::items(left);
if(parent)
{
ans += parent->index() + 1;
if(parent->left == this)
ans -= items + 1;
}
return ans;
}
void treap::setValue(T val)
{
value = val;
treap *t = this;
while(t)
{
t->recalc();
t = t->parent;
}
}
pair<treap*, treap*> split(treap *t, int k) //dzieli na prefiks dlugosci k i reszte
{
if(!t) return make_pair(nullptr, nullptr);
if(items(t->left) < k)
{
auto p = split(t->right, k - items(t->left) - 1);
t->right = p.first;
if(t->right) t->right->parent = t;
t->parent = nullptr;
t->recalc();
return make_pair(t, p.second);
}
else
{
auto p = split(t->left, k);
t->left = p.second;
if(t->left) t->left->parent = t;
t->parent = nullptr;
t->recalc();
return make_pair(p.first, t);
}
}
treap* merge(treap *a, treap *b)
{
if(!a) return b;
if(!b) return a;
if(a->rank > b->rank)
{
a->right = merge(a->right, b);
a->right->parent = a;
a->recalc();
return a;
}
else
{
b->left = merge(a, b->left);
b->left->parent = b;
b->recalc();
return b;
}
}
treap::T select(treap *t, int k) //zwraca k-ty element
{
if(!t) return treap::T();
int i = items(t->left);
if(i == k) return t->value;
if(i > k) return select(t->left, k);
return select(t->right, k - i - 1);
}
treap* insert(treap *t, treap::T val, int k) //wstaw val na pozycje k (liczac od 0)
{
auto p = split(t, k);
return merge(merge(p.first, new treap(val)), p.second);
}
treap* erase(treap *t, int k)
{
auto p1 = split(t, k);
auto p2 = split(p1.second, 1);
return merge(p1.first, p2.second);
}