2022-02-16 14:09:57 +00:00
|
|
|
|
#include <iostream>
|
2022-07-01 13:35:25 +00:00
|
|
|
|
#include <limits>
|
2021-01-02 16:00:16 +00:00
|
|
|
|
|
2022-02-16 14:09:57 +00:00
|
|
|
|
using std::cin;
|
|
|
|
|
using std::cout;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
const char endl = '\n';
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
const int N = 1e5 + 5;
|
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
class Splay {
|
2022-02-16 14:09:57 +00:00
|
|
|
|
private:
|
2022-07-14 23:27:47 +00:00
|
|
|
|
size_t root, cnt;
|
|
|
|
|
|
2022-02-16 14:09:57 +00:00
|
|
|
|
struct node {
|
2022-07-14 23:27:47 +00:00
|
|
|
|
size_t l, r, f, c, s;
|
|
|
|
|
int v;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
|
|
|
|
node()
|
2022-07-14 23:27:47 +00:00
|
|
|
|
: l(0), r(0), f(0), c(0), s(0), v(0) {}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
node(int _v, int _f)
|
|
|
|
|
: l(0), r(0), f(_f), c(1), s(1), v(_v) {}
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
size_t &child(unsigned x) {
|
|
|
|
|
return !x ? l : r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
2022-07-14 23:27:47 +00:00
|
|
|
|
} tr[N];
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 上传信息
|
|
|
|
|
void pushup(size_t u) {
|
|
|
|
|
tr[u].s = tr[tr[u].l].s + tr[tr[u].r].s + tr[u].c;
|
|
|
|
|
}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
unsigned relation(size_t u) {
|
|
|
|
|
// 如果当前节点是其父亲节点的左儿子则返回 0,否则返回 1
|
|
|
|
|
return u == tr[tr[u].f].l ? 0 : 1;
|
|
|
|
|
}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
void rotate(size_t u) {
|
|
|
|
|
// 旧的父节点
|
|
|
|
|
size_t p = tr[u].f;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 当前节点与父节点之间的关系
|
|
|
|
|
unsigned x = relation(u);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 当前节点 <-> 父节点的父节点
|
|
|
|
|
if (tr[p].f) {
|
|
|
|
|
tr[tr[p].f].child(relation(p)) = u;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
}
|
2022-07-14 23:27:47 +00:00
|
|
|
|
tr[u].f = tr[p].f;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 原先的另一个子节点 <-> 父节点
|
|
|
|
|
if (tr[u].child(x ^ 1)) {
|
|
|
|
|
tr[tr[u].child(x ^ 1)].f = p;
|
|
|
|
|
}
|
|
|
|
|
tr[p].child(x) = tr[u].child(x ^ 1);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 原先的父节点 -> 子节点
|
|
|
|
|
tr[u].child(x ^ 1) = p;
|
|
|
|
|
tr[p].f = u;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 更新节点信息
|
|
|
|
|
pushup(p);
|
|
|
|
|
pushup(u);
|
|
|
|
|
}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// Splay
|
|
|
|
|
//
|
|
|
|
|
// 旋转到给定的位置(target),默认行为为旋转为根节点
|
|
|
|
|
void splay(size_t u, size_t t = 0) {
|
|
|
|
|
while (tr[u].f != t) {
|
|
|
|
|
if (tr[tr[u].f].f == t) {
|
|
|
|
|
rotate(u);
|
|
|
|
|
} else if (relation(u) == relation(tr[u].f)) {
|
|
|
|
|
rotate(tr[u].f);
|
|
|
|
|
rotate(u);
|
|
|
|
|
} else {
|
|
|
|
|
rotate(u);
|
|
|
|
|
rotate(u);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 更新根节点
|
|
|
|
|
if (!t) root = u;
|
|
|
|
|
}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 前驱
|
|
|
|
|
//
|
|
|
|
|
// 左子树的最右点
|
|
|
|
|
size_t _predecessor(size_t u) {
|
|
|
|
|
size_t cur = tr[u].l;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
while (tr[cur].r) {
|
|
|
|
|
cur = tr[cur].r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 后继
|
|
|
|
|
//
|
|
|
|
|
// 右子树的最左点
|
|
|
|
|
size_t _successor(size_t u) {
|
|
|
|
|
size_t cur = tr[u].r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
while (tr[cur].l) {
|
|
|
|
|
cur = tr[cur].l;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
size_t _find(const int &v) {
|
|
|
|
|
size_t u = root;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
while (u && tr[u].v != v) {
|
|
|
|
|
// 根据数值大小向左右子树迭代
|
|
|
|
|
u = v < tr[u].v ? tr[u].l : tr[u].r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
if (u) splay(u);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
return u;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
size_t _insert(const int &v) {
|
|
|
|
|
size_t u = root, f = 0;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
while (u && tr[u].v != v) {
|
|
|
|
|
f = u;
|
|
|
|
|
// 根据数值大小向左右子树迭代
|
|
|
|
|
u = v < tr[u].v ? tr[u].l : tr[u].r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
if (u) {
|
|
|
|
|
tr[u].c++;
|
|
|
|
|
tr[u].s++;
|
|
|
|
|
} else {
|
|
|
|
|
tr[u = ++cnt] = node(v, f);
|
|
|
|
|
if (f) tr[f].child(v > tr[f].v) = u;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
splay(u);
|
|
|
|
|
|
|
|
|
|
return root;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
void _erase(size_t u) {
|
|
|
|
|
if (!u) return;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
if (tr[u].c > 1) { // 存在重复的数
|
|
|
|
|
splay(u);
|
|
|
|
|
tr[u].c--;
|
|
|
|
|
tr[u].s--;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
|
|
|
|
return;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
size_t pred = _predecessor(u),
|
|
|
|
|
succ = _successor(u);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
splay(pred); // 将前驱旋转到根节点
|
|
|
|
|
splay(succ, pred); // 将后继旋转到根节点的右儿子
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
tr[succ].l = 0; // 此时要删的节点为根节点的左儿子且为叶子节点
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 更新节点信息
|
|
|
|
|
pushup(succ);
|
|
|
|
|
pushup(pred);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2022-07-01 13:35:25 +00:00
|
|
|
|
Splay()
|
2022-07-14 23:27:47 +00:00
|
|
|
|
: root(0), cnt(0) {
|
|
|
|
|
// 插入哨兵节点
|
|
|
|
|
insert(std::numeric_limits<int>::min());
|
|
|
|
|
insert(std::numeric_limits<int>::max());
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
// 插入
|
2022-07-14 23:27:47 +00:00
|
|
|
|
void insert(const int &v) {
|
|
|
|
|
_insert(v);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
// 删除
|
2022-07-14 23:27:47 +00:00
|
|
|
|
void erase(const int &v) {
|
|
|
|
|
_erase(_find(v));
|
2022-02-16 14:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
// 排名
|
2022-07-14 23:27:47 +00:00
|
|
|
|
unsigned rank(const int &v) {
|
|
|
|
|
size_t u = _find(v);
|
|
|
|
|
|
|
|
|
|
if (!u) { // 不存在则插入一个方便查找
|
|
|
|
|
u = _insert(v);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
// 此时 u 已经成为根节点,直接取左子树大小即可
|
|
|
|
|
unsigned r = tr[tr[u].l].s;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
_erase(u);
|
|
|
|
|
|
|
|
|
|
return r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
return tr[tr[u].l].s;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
// 选择
|
2022-07-14 23:27:47 +00:00
|
|
|
|
const int &select(unsigned k) {
|
|
|
|
|
size_t u = root;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
while (k < tr[tr[u].l].s || k >= tr[tr[u].l].s + tr[u].c) {
|
|
|
|
|
if (k < tr[tr[u].l].s) {
|
|
|
|
|
u = tr[u].l;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
} else {
|
2022-07-14 23:27:47 +00:00
|
|
|
|
k -= tr[tr[u].l].s + tr[u].c;
|
|
|
|
|
u = tr[u].r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
splay(u);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
return tr[u].v;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
// 前驱
|
2022-07-14 23:27:47 +00:00
|
|
|
|
const int &predecessor(const int &v) {
|
|
|
|
|
size_t u = _find(v);
|
|
|
|
|
|
|
|
|
|
if (!u) { // 不存在则插入一个方便查找
|
|
|
|
|
u = _insert(v);
|
|
|
|
|
|
|
|
|
|
const int &r = tr[_predecessor(u)].v;
|
|
|
|
|
|
|
|
|
|
_erase(u); // 删除
|
|
|
|
|
|
|
|
|
|
return r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
return tr[_predecessor(u)].v;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
// 后继
|
2022-07-14 23:27:47 +00:00
|
|
|
|
const int &successor(const int &v) {
|
|
|
|
|
size_t u = _find(v);
|
|
|
|
|
|
|
|
|
|
if (!u) { // 不存在则插入一个方便查找
|
|
|
|
|
u = _insert(v);
|
|
|
|
|
|
|
|
|
|
const int &r = tr[_successor(u)].v;
|
|
|
|
|
|
|
|
|
|
_erase(u); // 删除
|
|
|
|
|
|
|
|
|
|
return r;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
return tr[_successor(u)].v;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2022-02-16 14:09:57 +00:00
|
|
|
|
|
2022-07-01 13:35:25 +00:00
|
|
|
|
int n;
|
2022-07-14 23:27:47 +00:00
|
|
|
|
Splay tree;
|
2021-01-02 16:00:16 +00:00
|
|
|
|
|
|
|
|
|
int main() {
|
2022-02-16 14:09:57 +00:00
|
|
|
|
std::ios::sync_with_stdio(false);
|
2022-07-01 13:35:25 +00:00
|
|
|
|
cin.tie(nullptr);
|
|
|
|
|
|
2021-01-02 16:00:16 +00:00
|
|
|
|
cin >> n;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
while (n--) {
|
2022-07-01 13:35:25 +00:00
|
|
|
|
int op, x;
|
|
|
|
|
|
2022-02-16 14:09:57 +00:00
|
|
|
|
cin >> op >> x;
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2022-07-14 23:27:47 +00:00
|
|
|
|
if (op == 1) {
|
|
|
|
|
tree.insert(x);
|
|
|
|
|
} else if (op == 2) {
|
|
|
|
|
tree.erase(x);
|
|
|
|
|
} else if (op == 3) {
|
|
|
|
|
cout << tree.rank(x) << endl;
|
|
|
|
|
} else if (op == 4) {
|
|
|
|
|
cout << tree.select(x) << endl;
|
|
|
|
|
} else if (op == 5) {
|
|
|
|
|
cout << tree.predecessor(x) << endl;
|
|
|
|
|
} else { // op == 6
|
|
|
|
|
cout << tree.successor(x) << endl;
|
2021-01-02 16:00:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-01 13:35:25 +00:00
|
|
|
|
|
2021-01-02 16:00:16 +00:00
|
|
|
|
return 0;
|
2022-02-16 14:09:57 +00:00
|
|
|
|
}
|