-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreap.cpp
149 lines (121 loc) · 2.52 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>
using namespace std;
typedef unsigned short usint;
typedef unsigned uint;
typedef long lint;
typedef short sint;
typedef long double ld;
typedef long long llint;
typedef unsigned long ulint;
typedef unsigned long long ullint;
#define INF (1<<30)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pii pair <int,int>
#define pcc pair <char,char>
#define pss pair <string,string>
#define np nullptr
inline int randNum()
{
return (rand()<<15)|rand();
}
struct Node
{
int key, pr;
Node *c[2];
Node(int k = 0, int p = randNum())
{
key = k;
pr = p;
c[0] = c[1] = np;
}
};
void deleteTreap(Node *x)
{
if (x == np) return;
deleteTreap(x->c[0]);
deleteTreap(x->c[1]);
delete x;
}
void inOrder(Node *&root, Node *x)
{
if (x == np) return;
inOrder(root, x->c[0]);
cout << x->key << ' ';
inOrder(root, x->c[1]);
if (x == root) cout << '\n';
}
void Split(Node *root, int key, Node *&L, Node *&R)
{
if (root == np)
L = R = np;
else if (key < root->key)
Split(root->c[0], key, L, root->c[0]), R = root;
else
Split(root->c[1], key, root->c[1], R), L = root;
}
void Merge(Node *&root, Node *L, Node *R)
{
if (L == np || R == np)
root = (L != np) ? L:R;
else if (L->pr > R->pr)
Merge(L->c[1], L->c[1], R), root = L;
else
Merge(R->c[0], L, R->c[0]), root = R;
}
void Insert(Node *&root, Node *x)
{
if (root == np)
{
root = x;
return;
}
if (x->pr > root->pr)
{
Split(root, x->key, x->c[0], x->c[1]);
root = x;
}
else Insert(root->c[x->key > root->key], x);
}
void Delete(Node *&root, int key)
{
if (root == np) return;
if (key == root->key)
Merge(root, root->c[0], root->c[1]);
else
Delete(root->c[key > root->key], key);
}
Node* Union(Node *A, Node *B)
{
if (A == np || B == np) return (A != np) ? A:B;
if (B->pr > A->pr) swap(A,B);
Node *L = np, *R = np;
Split(B, A->key, L, R);
A->c[0] = Union(A->c[0], L);
A->c[1] = Union(A->c[1], R);
return A;
}
int main()
{
ios_base::sync_with_stdio(0);
int q;
cin >> q;
srand(time(0));
Node *root = np;
while (q)
{
char c;
int x;
cin >> c >> x;
if (c == 'i')
Insert(root, new Node(x));
else if (c == 'd')
Delete(root, x);
inOrder(root,root);
--q;
}
deleteTreap(root);
root = np;
return 0;
}