【模板】Splay

(updated)

Code:

#include <iostream>
#include <cstdio>
using namespace std;
//Mystery_Sky
//
#define M 1000100
#define INF 0x3f3f3f3f
inline int read()
{
    int x=0,f=1; char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

struct Splay{
    int cnt, size;
    int val;
    int fa, son[2];
}t[M];
int root, tot;
int n, opt, x;

inline void push_up(int x) 
{
    if(x) {
        t[x].size = t[x].cnt;
        if(t[x].son[0]) t[x].size += t[t[x].son[0]].size;
        if(t[x].son[1]) t[x].size += t[t[x].son[1]].size;
    }
}
inline bool check(int x) {return t[t[x].fa].son[1] == x;}
inline void Clear(int x) {t[x].val = t[x].size = t[x].cnt = t[x].fa = t[x].son[0] = t[x].son[1] = 0;}
inline int New(int x, int fa) 
{
    t[++tot].val = x;
    t[tot].size = t[tot].cnt = 1;
    t[tot].son[0] = t[tot].son[1] = 0;
    t[tot].fa = fa;
    return tot;
}

inline void Rotate(int x)
{
    int y = t[x].fa, z = t[y].fa, k = check(x), w = t[x].son[k^1];
    t[y].son[k] = w; t[t[y].son[k]].fa = y;
    t[y].fa = x;
    t[x].son[k^1] = y;
    t[x].fa = z;
    if(z) t[z].son[t[z].son[1] == y] = x;
    push_up(y);
    push_up(x);
}

inline void splay(int x)//Splay->一直Rotate到根节点 
{
    for(int y; (y = t[x].fa); Rotate(x))
        if(t[y].fa) Rotate((check(x) == check(y) ? y : x));
    root = x;
}

inline void Insert(int x)
{
    if(!root) {
        root = New(x, 0);
        return;
    }
    int now = root, y = 0;
    while(1) {
        //printf("1\n");
        if(t[now].val == x) {
            t[now].cnt++;
            push_up(now), push_up(y);
            splay(now);
            break;
        }
        y = now;
        now = t[now].son[t[now].val < x];
        if(!now) {
            New(x, y);
            t[y].son[t[y].val < x] = tot;
            push_up(y);
            splay(tot);
            return;
        }
    }
}

inline int query_rank(int x)
{
    int ans = 0, now = root;
    while(1) {
        if(x < t[now].val) now = t[now].son[0];
        else {
            ans += t[t[now].son[0]].size;
            if(x == t[now].val) {
                splay(now);
                return ans + 1; 
            }
            ans += t[now].cnt;
            now = t[now].son[1];
        }
    }
}

inline int query_val(int rank)
{
    int now = root;
    while(1) {
        if(t[now].son[0] && rank <= t[t[now].son[0]].size) now = t[now].son[0];
        else {
            int temp = t[t[now].son[0]].size + t[now].cnt;
            if(rank <= temp) return t[now].val;
            rank -= temp;
            now = t[now].son[1];
        } 
    }
} 

inline int Pre()
{
    int now = t[root].son[0];
    while(t[now].son[1]) now = t[now].son[1];
    return now; 
}

inline int Succ()
{
    int now = t[root].son[1];
    while(t[now].son[0]) now = t[now].son[0];
    return now;
}

inline void Delete(int x)
{
    int q = query_rank(x);
    if(t[root].cnt > 1) {
        t[root].cnt--;
        push_up(root);
        return;
    }
    if(!t[root].son[0] && !t[root].son[1]) {
        Clear(root);
        root = 0;
        return;
    }
    if(!t[root].son[0]) {
        int y = root;
        root = t[y].son[1];
        t[root].fa = 0;
        Clear(y);
        return;
    }
    else if(!t[root].son[1]) {
        int y = root;
        root = t[y].son[0];
        t[root].fa = 0;
        Clear(y);
        return;
    }
    int lc = Pre(), y = root;
    splay(lc);
    t[t[y].son[1]].fa = root;
    t[root].son[1] = t[y].son[1];
    Clear(y);
    push_up(root);
    return;
}

int main() {
    n = read();
        Insert(INF), Insert(-INF);
    for(int i = 1; i <= n; i++) {
        opt = read(), x = read();
        switch(opt) {
            case 1: Insert(x); break;
            case 2: Delete(x); break;
            case 3: printf("%d\n", query_rank(x)); break;
            case 4: printf("%d\n", query_val(x)); break;
            case 5: Insert(x), printf("%d\n", t[Pre()].val), Delete(x); break;
            case 6: Insert(x), printf("%d\n", t[Succ()].val), Delete(x); break;
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/Benjamin-cpp/p/11517171.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值