-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path104.cpp
130 lines (118 loc) · 3.35 KB
/
104.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
/**
* @file 104.cpp
* @author Macesuted ([email protected])
* @date 2022-10-27
*
* @copyright Copyright (c) 2022
*
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif
bool mem1;
class FhqTreap {
private:
struct Node {
Node *l, *r;
int v, siz, rnk;
Node(int _v) { l = r = nullptr, v = _v, siz = 1, rnk = rand(); }
};
Node* root;
int getSiz(Node* p) { return p ? p->siz : 0; }
void pushUp(Node* p) { return p->siz = 1 + getSiz(p->l) + getSiz(p->r), void(); }
void splitV(Node* p, Node*& t1, Node*& t2, int v) {
if (!p) return t1 = t2 = nullptr, void();
if (p->v <= v)
t1 = p, splitV(p->r, t1->r, t2, v);
else
t2 = p, splitV(p->l, t1, t2->l, v);
return pushUp(p);
}
void splitS(Node* p, Node*& t1, Node*& t2, int s) {
if (!p) return t1 = t2 = nullptr, void();
if (1 + getSiz(p->l) <= s)
t1 = p, splitS(p->r, t1->r, t2, s - 1 - getSiz(p->l));
else
t2 = p, splitS(p->l, t1, t2->l, s);
return pushUp(p);
}
void merge(Node*& p, Node* t1, Node* t2) {
if (!t1) return p = t2, void();
if (!t2) return p = t1, void();
if (t1->rnk < t2->rnk)
p = t1, merge(p->r, t1->r, t2);
else
p = t2, merge(p->l, t1, t2->l);
return pushUp(p);
}
public:
FhqTreap(void) { root = nullptr; }
void insert(int v) {
Node* tl = nullptr;
splitV(root, tl, root, v);
return merge(tl, tl, new Node(v)), merge(root, tl, root);
}
void erase(int v) {
Node *tl = nullptr, *tp = nullptr;
splitV(root, tl, root, v), splitS(tl, tl, tp, getSiz(tl) - 1);
delete tp;
return merge(root, tl, root);
}
int getRank(int v) {
Node* tl = nullptr;
splitV(root, tl, root, v - 1);
int ans = getSiz(tl) + 1;
return merge(root, tl, root), ans;
}
int findRank(int v) {
Node *tl = nullptr, *tp = nullptr;
splitS(root, tl, root, v - 1), splitS(root, tp, root, 1);
return merge(tl, tl, tp), merge(root, tl, root), tp->v;
}
int pre(int v) {
Node *tl = nullptr, *tp = nullptr;
splitV(root, tl, root, v - 1), splitS(tl, tl, tp, getSiz(tl) - 1);
return merge(tl, tl, tp), merge(root, tl, root), tp->v;
}
int nex(int v) {
Node *tr = nullptr, *tp = nullptr;
splitV(root, root, tr, v), splitS(tr, tp, tr, 1);
return merge(tr, tp, tr), merge(root, root, tr), tp->v;
}
} FT;
void solve(void) {
int n;
cin >> n;
while (n--) {
int op, v;
cin >> op >> v;
if (op == 1)
FT.insert(v);
else if (op == 2)
FT.erase(v);
else if (op == 3)
cout << FT.getRank(v) << endl;
else if (op == 4)
cout << FT.findRank(v) << endl;
else if (op == 5)
cout << FT.pre(v) << endl;
else
cout << FT.nex(v) << endl;
}
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
int _ = 1;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}