Tyvj 1728 普通平衡树

本文介绍了一种自我调整的二叉查找树——Splay树的实现细节,并提供了一个具体的C++实现案例,包括插入、删除、查找等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

裸的平衡树,Splay和Treap均可水过,我的splay也是debug了好久….

#include<cstdio>
#define MAXN 300005
struct node {
    int v, cnt, sz, ch[2], f;
}t[MAXN];
int rt, sz, n;
#define Upd(r) {t[r].sz = t[t[r].ch[0]].sz + t[t[r].ch[1]].sz + t[r].cnt;}
void rot(int x)
{
    int y = t[x].f, z = t[y].f;
    bool f = (t[y].ch[1] == x);
    t[y].ch[f] = t[x].ch[f^1];
    if(t[y].ch[f]) t[t[y].ch[f]].f = y;
    t[x].ch[f^1] = y; t[y].f = x;
    t[x].f = z;
    if(z) t[z].ch[t[z].ch[1]==y] = x;
    Upd(y);
}
void Spaly(int r, int tp) {
    for(int y, z; (y = t[r].f) != tp; rot(r)) {
        z = t[y].f;
        if(z == tp) continue;
        if( (t[z].ch[0] == y) == (t[y].ch[0] == r) ) rot(y);
        else rot(r);
    }
    if(!tp) rt = r; Upd(r);
}
void Ins(int r, int x) {
    int y = 0;
    while(r && t[r].v != x) { y = r; r = t[r].ch[x > t[r].v]; }
    if(r) ++ t[r].cnt;
    else {
        r = ++ sz; t[r].sz = t[r].cnt = 1;
        t[r].v = x; t[r].f = y; if(y) t[y].ch[x > t[y].v] = r;
    }
    Spaly(r, 0);
}
void Find(int v) {
    int x = rt; if(!x) return;
    while(t[x].ch[v > t[x].v] && t[x].v != v) x = t[x].ch[v > t[x].v];
    Spaly(x, 0);
}
int Ran(int v) {
    Find(v);
    return t[t[rt].ch[0]].sz;
}
int Kth(int x)
{
    int y=rt,p;
    if(x>t[rt].sz)return 0;
    while(1)
    {
        p=t[y].ch[0];
        if(t[p].sz+t[y].cnt<x) {
            x-=t[p].sz+t[y].cnt;
            y=t[y].ch[1];
        }
        else if(t[p].sz>=x) y=p;
        else return t[y].v;
    }
}
int Nxt(int x, bool f)
{
    Find(x);
    if((t[rt].v>x&&f)||(t[rt].v<x&&!f)) return rt;
    int p = t[rt].ch[f];
    while(t[p].ch[f^1]) p = t[p].ch[!f];
    return p;
}
void Del(int v) {
    int p = Nxt(v, 0), s = Nxt(v, 1);
    Spaly(p, 0); Spaly(s, p);
    p = t[s].ch[0];
    if(t[p].cnt > 1) -- t[p].cnt, Spaly(p, 0);
    else t[s].ch[0] = 0;
}
char c, f;
inline void GET(int &n) {
    n = 0; f = 1;
    do {c = getchar(); if(c == '-') f = -1;} while(c > '9' || c < '0');
    while(c >= '0' && c <= '9') {n=n*10+c-'0';c=getchar();}
    n *= f;
}
int main() {
    freopen("phs.in","r",stdin);
    freopen("phs.out","w",stdout);
    GET(n);
    int opt,x;
    Ins(rt, -0x7fffffff); Ins(rt, +0x7fffffff);
    for(int i=1; i<=n; i++) {
        GET(opt); GET(x);
        switch(opt) {
            case 1: Ins(rt,x); break;
            case 2: Del(x); break;
            case 3: printf("%d\n",Ran(x)); break;
            case 4: printf("%d\n",Kth(x+1)); break;
            case 5: printf("%d\n",t[Nxt(x, 0)].v); break;
            case 6: printf("%d\n",t[Nxt(x, 1)].v); break;
        }
    }
    return 0;
}
                                                HelenKeller
                                                 2016.7.4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值