Repository navigation
Expand file tree
/
Copy pathTreap.cpp
More file actions
212 lines (212 loc) · 3.79 KB
/
Copy pathTreap.cpp
File metadata and controls
212 lines (212 loc) · 3.79 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// https://www.luogu.com.cn/problem/P3369
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define rrep(i, j, k) for (int i = j; i >= k; --i)
#define clr(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 1e5 + 5;
int tn;
int root;
int key[N];
int prior[N];
int ls[N];
int rs[N];
int cnt[N];
int siz[N];
mt19937 rd(random_device{}());
void upd(int p)
{
siz[p] = siz[ls[p]] + siz[rs[p]] + cnt[p];
}
int lrot(int p)
{
int r = rs[p];
rs[p] = ls[r];
ls[r] = p;
upd(p);
upd(r);
return r;
}
int rrot(int p)
{
int l = ls[p];
ls[p] = rs[l];
rs[l] = p;
upd(p);
upd(l);
return l;
}
int add(int p, int x, int pr)
{
if (!p)
{
key[++tn] = x;
prior[tn] = pr;
cnt[tn] = 1;
siz[tn] = 1;
return tn;
}
if (x == key[p])
{
++cnt[p];
}
else if (x < key[p])
{
ls[p] = add(ls[p], x, pr);
if (prior[ls[p]] > prior[p])
{
p = rrot(p);
}
}
else
{
rs[p] = add(rs[p], x, pr);
if (prior[rs[p]] > prior[p])
{
p = lrot(p);
}
}
upd(p);
return p;
}
int del(int p, int x)
{
if (!p)
return 0;
if (x == key[p])
{
if (cnt[p] > 1)
{
--cnt[p];
}
else
{
if (!ls[p] && !rs[p])
{
return 0;
}
else if (!ls[p] && rs[p])
{
return rs[p];
}
else if (ls[p] && !rs[p])
{
return ls[p];
}
else
{
if (prior[ls[p]] > prior[rs[p]])
{
p = rrot(p);
rs[p] = del(rs[p], x);
}
else
{
p = lrot(p);
ls[p] = del(ls[p], x);
}
}
}
}
else if (x < key[p])
{
ls[p] = del(ls[p], x);
}
else
{
rs[p] = del(rs[p], x);
}
upd(p);
return p;
}
int get_rank(int p, int x)
{
if (!p)
return 1;
if (key[p] >= x)
return get_rank(ls[p], x);
if (key[p] < x)
return siz[ls[p]] + cnt[p] + get_rank(rs[p], x);
}
int get_key(int p, int x)
{
if (siz[ls[p]] >= x)
return get_key(ls[p], x);
if (siz[ls[p]] + cnt[p] < x)
return get_key(rs[p], x - siz[ls[p]] - cnt[p]);
return key[p];
}
int get_pre(int p, int x)
{
return get_key(root, get_rank(root, x) - 1);
}
int get_post(int p, int x)
{
return get_key(root, get_rank(root, x + 1));
}
inline void solve()
{
tn = 0;
root = 0;
clr(key, 0);
clr(prior, 0);
clr(ls, 0);
clr(rs, 0);
clr(cnt, 0);
clr(siz, 0);
int m;
scanf("%d", &m);
int op, x;
while (m--)
{
scanf("%d%d", &op, &x);
if (op == 1)
{
root = add(root, x, rd());
}
else if (op == 2)
{
root = del(root, x);
}
else if (op == 3)
{
printf("%d\n", get_rank(root, x));
}
else if (op == 4)
{
printf("%d\n", get_key(root, x));
}
else if (op == 5)
{
printf("%d\n", get_pre(root, x));
}
else
{
printf("%d\n", get_post(root, x));
}
}
}
inline void init()
{
}
int main()
{
IOS;
init();
int t = 1;
while (t--)
{
solve();
}
return 0;
}