-
Notifications
You must be signed in to change notification settings - Fork 0
/
non_rotational_treap.cpp
148 lines (128 loc) · 2.69 KB
/
non_rotational_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
#include <bits/stdc++.h>
class Treap {
private:
const int INF = std::numeric_limits<int>::max();
struct Node {
int ls, rs;
int size, rnd, val;
};
std::vector<Node> t;
int cnt;
int root;
void ModifySize(int p) { t[p].size = t[t[p].ls].size + t[t[p].rs].size + 1; }
void NewNode(int& k, int x) {
t[++cnt] = {0, 0, 1, rand(), x};
k = cnt;
}
int Merge(int x, int y) {
if (!x || !y) {
return x | y;
}
if (t[x].rnd < t[y].rnd) {
t[x].rs = Merge(t[x].rs, y);
ModifySize(x);
return x;
} else {
t[y].ls = Merge(x, t[y].ls);
ModifySize(y);
return y;
}
}
void Split(int p, int k, int& x, int& y) {
if (!p) {
x = y = 0;
return;
}
if (t[p].val <= k) {
x = p;
Split(t[p].rs, k, t[p].rs, y);
ModifySize(x);
} else {
y = p;
Split(t[p].ls, k, x, t[p].ls);
ModifySize(y);
}
}
int RankToNum(int p, int rank) {
if (rank == t[t[p].ls].size + 1) {
return t[p].val;
} else if (rank <= t[t[p].ls].size) {
return RankToNum(t[p].ls, rank);
} else {
return RankToNum(t[p].rs, rank - t[t[p].ls].size - 1);
}
}
public:
Treap(int size) : cnt(0), root(0) {
t = std::vector<Node>(size);
srand(unsigned(time(NULL)));
}
void Push(int v) {
int x, y, z;
Split(root, v, x, y);
NewNode(z, v);
root = Merge(Merge(x, z), y);
}
void Pop(int v) {
int x, y, z;
Split(root, v, x, z);
Split(x, v - 1, x, y);
y = Merge(t[y].ls, t[y].rs);
root = Merge(Merge(x, y), z);
}
int NumToRank(int v) {
int x, y;
Split(root, v - 1, x, y);
int rank = t[x].size + 1;
root = Merge(x, y);
return rank;
}
int RankToNum(int rank) { return RankToNum(root, rank); }
int Pre(int v) {
int x, y, k, pre;
Split(root, v - 1, x, y);
if (!x) {
return -INF;
}
k = t[x].size;
pre = RankToNum(x, k);
root = Merge(x, y);
return pre;
}
int Next(int v) {
int x, y, nex;
Split(root, v, x, y);
if (!y) {
return INF;
}
nex = RankToNum(y, 1);
root = Merge(x, y);
return nex;
}
};
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
const int N = 200000 + 50;
int n, op, val;
cin >> n;
Treap treap(N);
while (n--) {
cin >> op >> val;
if (op == 1) {
treap.Push(val);
} else if (op == 2) {
treap.Pop(val);
} else if (op == 3) {
cout << treap.NumToRank(val) << endl;
} else if (op == 4) {
cout << treap.RankToNum(val) << endl;
} else if (op == 5) {
cout << treap.Pre(val) << endl;
} else {
cout << treap.Next(val) << endl;
}
}
}