forked from loveneeshdhir/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treap.cpp
111 lines (93 loc) · 1.82 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 <bits/stdc++.h>
using namespace std;
typedef struct node {
int val,prior,size;
struct node *l,*r;
} node;
typedef node* pnode;
int sz (pnode t) {
return t ? t->size : 0;
}
void upd_sz (pnode t) {
if (t) {
t->size = sz(t->l) + 1 + sz(t->r);
}
}
pnode find (pnode t, int key) {
if (t == NULL || t->val == key) {
return t;
}
return find(t->val < key ? t->r : t->l, key);
}
void split (pnode t, pnode &l, pnode &r, int key) {
if (!t) {
l = r = NULL;
}
else if (t->val <= key) {
split(t->r, t->r, r, key);
l = t;
}
else {
split(t->l, l, t->l, key);
r = t;
}
upd_sz(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_sz(t);
}
void insert (pnode &t, pnode it) {
if (!t) {
t = it;
}
else if (it->prior > t->prior) {
split(t, it->l, it->r, it->val);
t = it;
}
else {
insert(t->val <= it->val ? t->r : t->l, it);
}
upd_sz(t);
}
void erase (pnode &t, int key) {
pnode p = find(t, key);
if (p == NULL) {
return;
}
pnode temp = t;
merge(t, p->l, p->r);
free(temp);
upd_sz(t);
}
pnode init (int val) {
pnode ret = (pnode)malloc(sizeof(node));
ret->val = val;
ret->size = 1;
ret->prior = rand();
ret->l = ret->r = NULL;
return ret;
}
int main() {
pnode t = init(4);
insert(t, init(19));
insert(t, init(8));
insert(t, init(27));
insert(t, init(20));
insert(t, init(12));
insert(t, init(15));
insert(t, init(6));
insert(t, init(7));
insert(t, init(8));
return 0;
}