-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreap.cpp
More file actions
40 lines (40 loc) · 999 Bytes
/
treap.cpp
File metadata and controls
40 lines (40 loc) · 999 Bytes
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
#include <bits/stdc++.h>
using namespace std;
struct node {
int key, cnt, prior;
node *l, *r;
node() {}
node(int key, int prior) : key(key), cnt(1), prior(prior), l(NULL), r(NULL) {}
};
typedef node* pnode;
void insert_(pnode& it, node* item) {
if (!it)
it = new node(item->key, item->prior);
else if (it->prior > item->prior)
insert_(it->key > item->key ? it->l : it->r, item);
else
split(it, key, item->l, item->r), it = item;
}
void split(pnode it, int key, pnode l, pnode r) {
if (!it)
l = r = NULL;
else if (it->key > key)
split(it->l, key, l, it->l), r = it;
else
split(it->r, key, it->r, r), l = it;
}
void erase_(pnode& it, int key) {
if (!it) return;
if (it == key)
merge_(it, it->l, it->r);
else
erase_(it->key > key ? it->l : it->r, key);
}
void merge_(pnode& it, pnode l, pnode r) {
if (!l || !r)
it = l ? l : r;
else if (l->prior > r->prior)
merge_(l->r, l->r, r), it = l;
else
merge_(r->l, l, r->l), it = r;
}