[BZOJ3224]Tyvj 1728 普通平衡树 && 重写平衡树

5 篇文章 0 订阅
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define SF scanf
#define PF printf
using namespace std;
typedef long long LL;
const int MAXN = 500000;
int n;
struct Splay_Tree {
    int root, ncnt;
    int fa[MAXN+10], ch[MAXN+10][2], sz[MAXN+10];
    int cnt[MAXN+10], val[MAXN+10];
    void up(int x) {
        sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x];
    }
    void Rotate(int x) {
        int y = fa[x], z = fa[y];
        bool d = x == ch[y][0];
        if(ch[y][!d] = ch[x][d]) fa[ch[x][d]] = y;
        if(fa[x] = z) ch[z][y == ch[z][1]] = x;
        up(ch[x][d] = y);
        fa[y] = x;
    }
    void splay(int x, int goal) {
        for(int y = fa[x]; y != goal; Rotate(x), y = fa[x])
            if(fa[y] != goal) Rotate( (x == ch[y][0]) == (y == ch[fa[y]][0]) ? y : x);
        up(x); if(!goal) root = x;
    }
    void NewNode(int &x, int key, int pre) {
        x = ++ncnt; val[x] = key; fa[x] = pre;
        sz[x] = cnt[x] = 1;
        ch[x][0] = ch[x][1] = 0;
    }
    void ins(int key) {
        int x = root;
        if(sz[x]) {
            int y = fa[x];
            while(x && key != val[x]) {
                sz[x]++;
                y = x;
                x = ch[x][key > val[x]];
            }
            bool d = key > val[y];
            if(x) sz[x]++, cnt[x]++;
            else NewNode(ch[y][d], key, y);
        }
        else NewNode(root, key, 0);
    }
    void del(int key) {
        int x = root;
        while(x && key != val[x]) x = ch[x][key > val[x]];
        if(!x) return ;
        if(cnt[x] == 1) {
            bool f = !ch[x][0];
            int y = ch[x][f] ? ch[x][f] : x;
            while(ch[y][!f]) y = ch[y][!f];
            bool d = ch[fa[y]][1] == y;
            if(fa[y]) ch[fa[y]][d] = ch[y][f];
            if(ch[y][f]) fa[ch[y][f]] = fa[y];
            val[x] = val[y]; cnt[x] = cnt[y];
            for(cnt[x = y] = 0; x; x = fa[x]) up(x);
            if(root == y) root = 0;
        }
        else for(cnt[x]--; x; x = fa[x]) sz[x]--;
    }
    int Find(int key) {
        int x = root;
        while(x && key != val[x]) x = ch[x][key > val[x]];
        splay(x, root);
        return x;
    }
    int Rank(int key) {
        int x = root, ret = 0;
        while(x && key != val[x]) 
            if(key < val[x]) x = ch[x][0];
            else ret += cnt[x] + sz[ch[x][0]], x = ch[x][1];
        return ret + sz[ch[x][0]] + 1;
    }
    int Find_kth(int k) {
        if(k < 0 || k > sz[root]) return 0;
        int x = root;
        while(k <= sz[ch[x][0]] || k > sz[ch[x][0]] + cnt[x])
            if(k <= sz[ch[x][0]]) x = ch[x][0];
            else k -= sz[ch[x][0]] + cnt[x], x = ch[x][1];
        splay(x, 0); return x;
    }
    int Find_pre(int key) {
        int x = root, y = 0;
        while(x)
            if(key <= val[x]) x = ch[x][0];
            else y = x, x = ch[x][1];
        if(y) splay(y, 0);
        return y;
    }
    int Find_nex(int key) {
        int x = root, y = 0;
        while(x)
            if(key >= val[x]) x = ch[x][1];
            else y = x, x = ch[x][0];
        if(y) splay(y, 0);
        return y;
    }
} sp;
int main() {
      for (scanf("%d", &n); n; --n) {
        int op, x; scanf("%d%d", &op, &x);
        switch (op) {
            case 1: sp.ins(x); break;
            case 2: sp.del(x); break;
            case 3: printf("%d\n", sp.Rank(x)); break;
            case 4: printf("%d\n", sp.val[sp.Find_kth(x)]); break;
            case 5: printf("%d\n", sp.val[sp.Find_pre(x)]); break;
            case 6: printf("%d\n", sp.val[sp.Find_nex(x)]); break;
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值