forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreap1.cpp
More file actions
60 lines (54 loc) · 1.15 KB
/
Treap1.cpp
File metadata and controls
60 lines (54 loc) · 1.15 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
struct Node {
int key, aux, size;
Node *l, *r; // BST w.r.t. key; min-heap w.r.t. aux
Node(int k) : key(k), aux(rand()), size(1), l(0), r(0) {
}
};
Node *upd(Node *p) {
if (p) p->size = 1 + (p->l ? p->l->size : 0)+(p->r ? p->r->size : 0);
return p;
}
void split(Node *p, Node *by, Node **L, Node **R) {
if (p == NULL) {
*L = *R = NULL;
} else if (p->key < by->key) {
split(p->r, by, &p->r, R);
*L = upd(p);
} else {
split(p->l, by, L, &p->l);
*R = upd(p);
}
}
Node *merge(Node *L, Node *R) {
Node *p;
if (L == NULL || R == NULL) p = (L != NULL ? L : R);
else if (L->aux < R->aux) {
L->r = merge(L->r, R);
p = L;
} else {
R->l = merge(L, R->l);
p = R;
}
return upd(p);
}
Node *insert(Node *p, Node *n) {
if (p == NULL) return upd(n);
if (n->aux <= p->aux) {
split(p, n, &n->l, &n->r);
return upd(n);
}
if (n->key < p->key) p->l = insert(p->l, n);
else p->r = insert(p->r, n);
return upd(p);
}
Node *erase(Node *p, int key) {
if (p == NULL) return NULL;
if (key == p->key) {
Node *q = merge(p->l, p->r);
delete p;
return upd(q);
}
if (key < p->key) p->l = erase(p->l, key);
else p->r = erase(p->r, key);
return upd(p);
}