P3369 【模板】普通平衡树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
fhq Treap (无旋 treap)
treap 即 tree + heap , 可以叫树堆 (?) , 通过在结点上存放一个随机的索引来避免平衡树在有序输入的最坏情况下变成链表, 而所需的一系列操作则通过类似将树依照某个值 撕开 再 合并 的方式进行
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
template <typename T> inline void read(T& t) {
int f = 0, c = getchar(); t = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) t = t * 10 + c - 48, c = getchar();
if (f) t = -t;
}
template <typename T> void print(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) print(x / 10);
putchar(x % 10 + 48);
}
#include <random>
typedef struct node
{
ll l, r, val, key, size;
}node;
node fhq[N];
ll cnt = 0, root;
mt19937 rnd(233);
inline ll newnode(ll val)
{
fhq[++cnt].val = val;
fhq[cnt].key = rnd();
fhq[cnt].size = 1;
return cnt;
}
inline void update(ll now)
{
fhq[now].size = fhq[fhq[now].l].size + fhq[fhq[now].r].size + 1;
}
void split(ll now, ll val, ll& x, ll& y)
{
if (!now)
x = y = 0;
else
{
if (fhq[now].val <= val)
{
x = now;
split(fhq[now].r, val, fhq[now].r, y);
}
else
{
y = now;
split(fhq[now].l, val, x, fhq[now].l);
}
update(now);
}
}
ll merge(ll x, ll y)
{
if (!x || !y)
return x + y;
if (fhq[x].key > fhq[y].key)
{
fhq[x].r = merge(fhq[x].r, y);
update(x);
return x;
}
else
{
fhq[y].l = merge(x, fhq[y].l);
update(y);
return y;
}
}
inline void insert(ll val)
{
ll x, y;
split(root, val, x, y);
root = merge(merge(x, newnode(val)), y);
}
inline void del(ll val)
{
ll x, y, z;
split(root, val, x, z);
split(x, val - 1, x, y);
y = merge(fhq[y].l, fhq[y].r);
root = merge(merge(x, y), z);
}
inline ll getrank(ll val)
{
ll x, y, r;
split(root, val - 1, x, y);
r = fhq[x].size + 1;
root = merge(x, y);
return r;
}
inline ll getnum(ll rank)
{
ll now = root;
while (now)
{
if (fhq[fhq[now].l].size + 1 == rank)
break;
else if (fhq[fhq[now].l].size >= rank)
now = fhq[now].l;
else
{
rank -= fhq[fhq[now].l].size + 1;
now = fhq[now].r;
}
}
return fhq[now].val;
}
inline ll getpre(ll val)
{
ll x, y;
split(root, val - 1, x, y);
ll now = x;
while (fhq[now].r)
{
now = fhq[now].r;
}
root = merge(x, y);
return fhq[now].val;
}
inline ll getaft(ll val)
{
ll x, y;
split(root, val, x, y);
ll now = y;
while (fhq[now].l)
now = fhq[now].l;
root = merge(x, y);
return fhq[now].val;
}
ll n, opt, xx;
int main()
{
read(n);
while (n--)
{
read(opt); read(xx);
if (opt == 1)
insert(xx);
else if (opt == 2)
del(xx);
else if (opt == 3)
{
print(getrank(xx));
putchar('\n');
}
else if (opt == 4)
{
print(getnum(xx));
putchar('\n');
}
else if (opt == 5)
{
print(getpre(xx));
putchar('\n');
}
else
{
print(getaft(xx));
putchar('\n');
}
}
return 0;
}