同 AcWing 253 普通平衡树
Version 1: Treap
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const int maxn(1e5 + 10);
const int inf(0x3f3f3f3f);
int idx, root;
struct node {
int l, r;
int key, val;
int cnt, siz;
} tree[maxn];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
inline int new_node(int key)
{
++idx;
tree[idx].key = key;
tree[idx].val = rand();
tree[idx].cnt = tree[idx].siz = 1;
return idx;
}
inline void push_up(int cur)
{
tree[cur].siz = tree[tree[cur].l].siz + tree[tree[cur].r].siz + tree[cur].cnt;
}
inline void zig(int& cur)
{
int ls = tree[cur].l;
tree[cur].l = tree[ls].r;
tree[ls].r = cur;
cur = ls;
push_up(tree[cur].r);
push_up(cur);
}
inline void zag(int& cur)
{
int rs = tree[cur].r;
tree[cur].r = tree[rs].l;
tree[rs].l = cur;
cur = rs;
push_up(tree[cur].l);
push_up(cur);
}
void build()
{
root = new_node(-inf);
tree[root].r = new_node(inf);
push_up(root);
if (tree[root].val < tree[tree[root].r].val) {
zag(root);
}
}
inline void insert(int& cur, int key)
{
if (not cur) {
cur = new_node(key);
} else if (key == tree[cur].key) {
++tree[cur].cnt;
} else if (key < tree[cur].key) {
insert(tree[cur].l, key);
if (tree[tree[cur].l].val > tree[cur].val) {
zig(cur);
}
} else {
insert(tree[cur].r, key);
if (tree[tree[cur].r].val > tree[cur].val) {
zag(cur);
}
}
push_up(cur);
}
inline void remove(int& cur, int key)
{
if (not cur) return;
if (key == tree[cur].key) {
if (tree[cur].cnt > 1) {
--tree[cur].cnt;
} else if (tree[cur].l or tree[cur].r) {
if (not tree[cur].r or tree[tree[cur].l].val > tree[tree[cur].r].val) {
zig(cur);
remove(tree[cur].r, key);
} else {
zag(cur);
remove(tree[cur].l, key);
}
} else {
cur = 0;
}
} else if (key < tree[cur].key) {
remove(tree[cur].l, key);
} else {
remove(tree[cur].r, key);
}
push_up(cur);
}
inline int get_rank(int cur, int key)
{
if (not cur) {
return 0;
}
if (key == tree[cur].key) {
return tree[tree[cur].l].siz + 1;
}
if (key < tree[cur].key) {
return get_rank(tree[cur].l, key);
}
return get_rank(tree[cur].r, key) + tree[tree[cur].l].siz + tree[cur].cnt;
}
inline int get_key(int cur, int rank)
{
if (not cur) {
return inf;
}
if (rank <= tree[tree[cur].l].siz) {
return get_key(tree[cur].l, rank);
}
if (rank <= tree[tree[cur].l].siz + tree[cur].cnt) {
return tree[cur].key;
}
return get_key(tree[cur].r, rank - tree[cur].cnt - tree[tree[cur].l].siz);
}
inline int get_prev(int cur, int key)
{
if (not cur) {
return -inf;
}
if (key <= tree[cur].key) {
return get_prev(tree[cur].l, key);
}
return max(tree[cur].key, get_prev(tree[cur].r, key));
}
inline int get_next(int cur, int key)
{
if (not cur) {
return inf;
}
if (key >= tree[cur].key) {
return get_next(tree[cur].r, key);
}
return min(tree[cur].key, get_next(tree[cur].l, key));
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
build();
int n = read();
while (n--) {
int op = read(), x = read();
if (op == 1) {
insert(root, x);
} else if (op == 2) {
remove(root, x);
} else if (op == 3) {
write(get_rank(root, x) - 1, true);
} else if (op == 4) {
write(get_key(root, x + 1), true);
} else if (op == 5) {
write(get_prev(root, x), true);
} else {
write(get_next(root, x), true);
}
}
return 0;
}
Version 2: fhq Treap
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const int inf(0x3f3f3f3f);
const int maxn(1e5 + 10);
int idx, root;
int x, y, z;
struct node {
int l, r;
int key, val;
int siz;
} tree[maxn];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
inline int new_node(int key)
{
++idx;
tree[idx].key = key;
tree[idx].val = rand();
tree[idx].siz = 1;
return idx;
}
inline void push_up(int cur)
{
tree[cur].siz = tree[tree[cur].l].siz + tree[tree[cur].r].siz + 1;
}
inline void split(int cur, int key, int& x, int& y)
{
if (not cur) {
x = y = 0;
} else if (tree[cur].key <= key) {
x = cur;
split(tree[cur].r, key, tree[cur].r, y);
push_up(cur);
} else {
y = cur;
split(tree[cur].l, key, x, tree[cur].l);
push_up(cur);
}
}
inline int merge(int x, int y)
{
if (not x or not y) {
return x + y;
}
if (tree[x].val > tree[y].val) {
tree[x].r = merge(tree[x].r, y);
push_up(x);
return x;
}
tree[y].l = merge(x, tree[y].l);
push_up(y);
return y;
}
inline void insert(int key)
{
split(root, key, x, y);
root = merge(merge(x, new_node(key)), y);
}
inline void remove(int key)
{
split(root, key, x, z);
split(x, key - 1, x, y);
y = merge(tree[y].l, tree[y].r);
root = merge(merge(x, y), z);
}
inline int get_rank(int key)
{
split(root, key - 1, x, y);
int res = tree[x].siz + 1;
root = merge(x, y);
return res;
}
inline int get_key(int cur, int rank)
{
if (rank == tree[tree[cur].l].siz + 1) {
return tree[cur].key;
}
if (rank <= tree[tree[cur].l].siz) {
return get_key(tree[cur].l, rank);
}
return get_key(tree[cur].r, rank - tree[tree[cur].l].siz - 1);
}
inline int get_prev(int key)
{
split(root, key - 1, x, y);
int cur = x;
while (tree[cur].r) {
cur = tree[cur].r;
}
int res = tree[cur].key;
root = merge(x, y);
return res;
}
inline int get_next(int key)
{
split(root, key, x, y);
int cur = y;
while (tree[cur].l) {
cur = tree[cur].l;
}
int res = tree[cur].key;
root = merge(x, y);
return res;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
int n = read();
while (n--) {
int op = read(), x = read();
if (op == 1) {
insert(x);
} else if (op == 2) {
remove(x);
} else if (op == 3) {
write(get_rank(x), true);
} else if (op == 4) {
write(get_key(root, x), true);
} else if (op == 5) {
write(get_prev(x), true);
} else {
write(get_next(x), true);
}
}
return 0;
}
Version 3: Splay
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const int maxn(1e5 + 10);
int idx, root;
struct node {
int val;
int siz, cnt;
int fa, ch[2];
} tree[maxn];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
inline int new_node(int val)
{
++idx;
tree[idx].val = val;
tree[idx].siz = tree[idx].cnt = 1;
return idx;
}
inline bool get_rel(int cur, int fa)
{
return tree[fa].ch[1] == cur;
}
inline void connect(int cur, int fa, int rel)
{
tree[fa].ch[rel] = cur;
tree[cur].fa = fa;
}
inline void push_up(int cur)
{
tree[cur].siz = tree[tree[cur].ch[0]].siz + tree[tree[cur].ch[1]].siz + tree[cur].cnt;
}
inline void rotate(int cur)
{
int fa = tree[cur].fa;
int gf = tree[fa].fa;
bool rel = get_rel(cur, fa);
connect(tree[cur].ch[rel ^ 1], fa, rel);
connect(cur, gf, get_rel(fa, gf));
connect(fa, cur, rel ^ 1);
push_up(fa);
push_up(cur);
}
inline void splaying(int cur, int top)
{
while (tree[cur].fa not_eq top) {
int fa = tree[cur].fa;
int gf = tree[fa].fa;
if (gf not_eq top) {
get_rel(cur, fa) ^ get_rel(fa, gf) ? rotate(cur) : rotate(fa);
}
rotate(cur);
}
if (not top) {
root = cur;
}
}
inline void insert(int val)
{
int cur = root, fa = 0;
while (cur and tree[cur].val not_eq val) {
fa = cur;
cur = tree[cur].ch[val > tree[cur].val];
}
if (cur) {
++tree[cur].cnt;
++tree[cur].siz;
} else {
cur = new_node(val);
connect(cur, fa, val > tree[fa].val);
}
splaying(cur, 0);
}
inline void remove(int val)
{
int cur = root, fa = 0;
while (tree[cur].val not_eq val) {
fa = cur;
cur = tree[cur].ch[val > tree[cur].val];
}
splaying(cur, 0);
if (tree[cur].cnt > 1) {
--tree[cur].cnt;
--tree[cur].siz;
} else if (tree[cur].ch[1]) {
int nxt = tree[cur].ch[1];
while (tree[nxt].ch[0]) {
nxt = tree[nxt].ch[0];
}
splaying(nxt, cur);
connect(tree[cur].ch[0], nxt, 0);
root = nxt;
tree[root].fa = 0;
push_up(root);
} else {
root = tree[cur].ch[0];
tree[root].fa = 0;
}
}
inline int get_rank(int val)
{
int cur = root, rank = 1;
while (cur) {
if (tree[cur].val == val) {
rank += tree[tree[cur].ch[0]].siz;
splaying(cur, 0);
break;
} else if (val < tree[cur].val) {
cur = tree[cur].ch[0];
} else {
rank += tree[tree[cur].ch[0]].siz + tree[cur].cnt;
cur = tree[cur].ch[1];
}
}
return rank;
}
inline int get_val(int rank)
{
int cur = root;
while (cur) {
int size_l = tree[tree[cur].ch[0]].siz;
if (size_l + 1 <= rank and rank <= size_l + tree[cur].cnt) {
splaying(cur, 0);
break;
} else if (size_l >= rank) {
cur = tree[cur].ch[0];
} else {
rank -= size_l + tree[cur].cnt;
cur = tree[cur].ch[1];
}
}
return tree[cur].val;
}
inline int get_prev(int val)
{
return get_val(get_rank(val) - 1);
}
inline int get_next(int val)
{
return get_val(get_rank(val + 1));
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
int n = read();
while (n--) {
int op = read(), x = read();
if (op == 1) {
insert(x);
} else if (op == 2) {
remove(x);
} else if (op == 3) {
write(get_rank(x), true);
} else if (op == 4) {
write(get_val(x), true);
} else if (op == 5) {
write(get_prev(x), true);
} else {
write(get_next(x), true);
}
}
return 0;
}