P3369 【模板】普通平衡树(FHQ_treap平衡树)

// #pragma GCC optimize(2)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <random>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);              \
    // cin.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Ndoe
{
    int L, R;
    int val;
    int key;
    int size;
} fhq[maxn];
int cnt, root;
int x, y, z;
mt19937 rnd(123);
inline int newnode(int val)
{
    fhq[++cnt].val = val;
    fhq[cnt].key = rnd();
    fhq[cnt].size = 1;
    return cnt;
}
inline void update(int now)
{
    fhq[now].size = fhq[fhq[now].L].size + fhq[fhq[now].R].size + 1;
}
int merge(int x, int 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;
    }
}
void split(int now, int val, int &x, int &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);
    }
    return;
}
inline void insert(int val)
{
    split(root, val, x, y);
    root = merge(merge(x, newnode(val)), y);
    return;
}
inline void del(int val)
{
    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);
    return;
}
inline void getrank(int val)
{
    split(root, val - 1, x, y);
    cout << fhq[x].size + 1 << "\n";
    root = merge(x, y);
    return;
}
inline void getnum(int rank)
{
    int 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;
        }
    }
    cout << fhq[now].val << "\n";
    return;
}
inline void pre(int val)
{
    split(root, val - 1, x, y);
    int now = x;
    while (fhq[now].R)
        now = fhq[now].R;
    cout << fhq[now].val << "\n";
    root = merge(x, y);
    return;
}
inline void post(int val)
{
    split(root, val, x, y);
    int now = y;
    while (fhq[now].L)
        now = fhq[now].L;
    cout << fhq[now].val << "\n";
    root = merge(x, y);
    return;
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    int n, op, a;
    cin >> n;
    while (n--)
    {
        cin >> op >> a;
        switch (op)
        {
        case 1:
            insert(a);
            break;
        case 2:
            del(a);
            break;
        case 3:
            getrank(a);
            break;
        case 4:
            getnum(a);
            break;
        case 5:
            pre(a);
            break;
        case 6:
            post(a);
            break;
        default:
            break;
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值