2022-02-17 06:35:51 +00:00
|
|
|
#include <iostream>
|
2022-06-01 01:45:41 +00:00
|
|
|
#include <cstdlib>
|
2022-02-17 06:35:51 +00:00
|
|
|
|
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
#define endl '\n'
|
|
|
|
|
|
|
|
const int N = 100005;
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
class Treap {
|
|
|
|
private:
|
|
|
|
int root, cnt;
|
|
|
|
|
|
|
|
struct node {
|
|
|
|
int l, r, s, v, k;
|
|
|
|
|
|
|
|
node();
|
|
|
|
node(int);
|
|
|
|
} tr[N];
|
|
|
|
|
|
|
|
void pushup(int);
|
|
|
|
std::pair<int, int> split(int, int);
|
|
|
|
std::pair<int, int> splitByValue(int, int);
|
|
|
|
int merge(int, int);
|
|
|
|
int find(int, int);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void insert(int);
|
|
|
|
void erase(int);
|
|
|
|
int getRank(int);
|
|
|
|
int getKth(int);
|
|
|
|
} tree;
|
2022-02-17 06:35:51 +00:00
|
|
|
|
|
|
|
int n, op, x;
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
cin >> n;
|
|
|
|
while (n--) {
|
|
|
|
cin >> op >> x;
|
|
|
|
if (op == 1) {
|
2022-02-18 00:57:40 +00:00
|
|
|
tree.insert(x);
|
2022-02-17 06:35:51 +00:00
|
|
|
} else if (op == 2) {
|
2022-02-18 00:57:40 +00:00
|
|
|
tree.erase(x);
|
2022-02-17 06:35:51 +00:00
|
|
|
} else if (op == 3) {
|
2022-02-18 00:57:40 +00:00
|
|
|
cout << tree.getRank(x) << endl;
|
2022-02-17 06:35:51 +00:00
|
|
|
} else if (op == 4) {
|
2022-02-18 00:57:40 +00:00
|
|
|
cout << tree.getKth(x) << endl;
|
2022-02-17 06:35:51 +00:00
|
|
|
} else if (op == 5) {
|
2022-02-18 00:57:40 +00:00
|
|
|
cout << tree.getKth(tree.getRank(x) - 1) << endl;
|
2022-02-17 06:35:51 +00:00
|
|
|
} else {
|
2022-02-18 00:57:40 +00:00
|
|
|
cout << tree.getKth(tree.getRank(x + 1)) << endl;
|
2022-02-17 06:35:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// === Treap ===
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
// struct Treap::node
|
2022-02-17 06:35:51 +00:00
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
Treap::node::node()
|
2022-06-01 01:45:41 +00:00
|
|
|
: l(0),
|
|
|
|
r(0),
|
|
|
|
s(0),
|
|
|
|
v(0),
|
|
|
|
k(rand()) {}
|
2022-05-20 01:23:27 +00:00
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
Treap::node::node(int _v)
|
2022-06-01 01:45:41 +00:00
|
|
|
: l(0),
|
|
|
|
r(0),
|
|
|
|
s(1),
|
|
|
|
v(_v),
|
|
|
|
k(rand()) {}
|
2022-02-17 06:35:51 +00:00
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
// class Treap
|
2022-02-17 06:35:51 +00:00
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
inline void Treap::pushup(int u) {
|
|
|
|
this->tr[u].s = this->tr[this->tr[u].l].s + this->tr[this->tr[u].r].s + 1;
|
2022-02-17 06:35:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
std::pair<int, int> Treap::split(int p, int k) {
|
2022-02-17 06:35:51 +00:00
|
|
|
if (!p) return std::make_pair(0, 0);
|
2022-02-18 00:57:40 +00:00
|
|
|
if (k <= this->tr[this->tr[p].l].s) {
|
|
|
|
auto o = this->split(this->tr[p].l, k);
|
|
|
|
this->tr[p].l = o.second;
|
|
|
|
this->pushup(p);
|
2022-02-17 06:35:51 +00:00
|
|
|
o.second = p;
|
|
|
|
return o;
|
|
|
|
}
|
2022-02-18 00:57:40 +00:00
|
|
|
auto o = this->split(this->tr[p].r, k - this->tr[this->tr[p].l].s - 1);
|
|
|
|
this->tr[p].r = o.first;
|
|
|
|
this->pushup(p);
|
2022-02-17 06:35:51 +00:00
|
|
|
o.first = p;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
std::pair<int, int> Treap::splitByValue(int p, int v) {
|
2022-02-17 06:35:51 +00:00
|
|
|
if (!p) return std::make_pair(0, 0);
|
2022-02-18 00:57:40 +00:00
|
|
|
if (this->tr[p].v < v) {
|
|
|
|
auto o = this->splitByValue(tr[p].r, v);
|
|
|
|
this->tr[p].r = o.first;
|
|
|
|
this->pushup(p);
|
2022-02-17 06:35:51 +00:00
|
|
|
o.first = p;
|
|
|
|
return o;
|
|
|
|
}
|
2022-02-18 00:57:40 +00:00
|
|
|
auto o = splitByValue(this->tr[p].l, v);
|
|
|
|
this->tr[p].l = o.second;
|
|
|
|
this->pushup(p);
|
2022-02-17 06:35:51 +00:00
|
|
|
o.second = p;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
int Treap::merge(int x, int y) {
|
2022-02-17 06:35:51 +00:00
|
|
|
if (!x) return y;
|
|
|
|
if (!y) return x;
|
2022-02-18 00:57:40 +00:00
|
|
|
if (this->tr[x].k > this->tr[y].k) {
|
|
|
|
this->tr[x].r = merge(this->tr[x].r, y);
|
|
|
|
this->pushup(x);
|
2022-02-17 06:35:51 +00:00
|
|
|
return x;
|
|
|
|
}
|
2022-02-18 00:57:40 +00:00
|
|
|
this->tr[y].l = merge(x, this->tr[y].l);
|
|
|
|
this->pushup(y);
|
2022-02-17 06:35:51 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
int Treap::find(int p, int v) {
|
2022-02-17 06:35:51 +00:00
|
|
|
if (!p) return 0;
|
2022-02-18 00:57:40 +00:00
|
|
|
if (this->tr[p].v == v) return p;
|
|
|
|
if (this->tr[p].v > v) return this->find(this->tr[p].l, v);
|
|
|
|
return this->find(this->tr[p].r, v);
|
2022-02-17 06:35:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
void Treap::insert(int v) {
|
|
|
|
auto o = this->splitByValue(root, v);
|
|
|
|
int p = ++this->cnt;
|
|
|
|
this->tr[p] = Treap::node(v);
|
|
|
|
o.first = this->merge(o.first, p);
|
|
|
|
this->root = this->merge(o.first, o.second);
|
2022-02-17 06:35:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
void Treap::erase(int v) {
|
|
|
|
auto o = this->splitByValue(root, v);
|
2022-02-17 06:35:51 +00:00
|
|
|
auto t = o;
|
2022-02-18 00:57:40 +00:00
|
|
|
if (this->find(o.second, v)) {
|
|
|
|
t = this->split(o.second, 1);
|
2022-02-17 06:35:51 +00:00
|
|
|
}
|
2022-02-18 00:57:40 +00:00
|
|
|
this->root = this->merge(o.first, t.second);
|
2022-02-17 06:35:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
inline int Treap::getRank(int v) {
|
2022-02-18 06:39:32 +00:00
|
|
|
auto x = this->splitByValue(this->root, v);
|
|
|
|
int r = this->tr[x.first].s;
|
|
|
|
this->root = merge(x.first, x.second);
|
|
|
|
return ++r;
|
2022-02-17 06:35:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 00:57:40 +00:00
|
|
|
int Treap::getKth(int k) {
|
|
|
|
auto x = this->split(this->root, k - 1);
|
|
|
|
auto y = this->split(x.second, 1);
|
2022-02-17 06:35:51 +00:00
|
|
|
int o = y.first;
|
2022-02-18 00:57:40 +00:00
|
|
|
this->root = this->merge(x.first, this->merge(y.first, y.second));
|
2022-02-17 06:35:51 +00:00
|
|
|
return tr[o].v;
|
|
|
|
}
|