应该没有大问题(坑)

int key[100005];
int fa[100005];
int ch[100005][2];
int root,tot,t1,t2;
bool dir(int x)
{
    return x==ch[fa[x]][1];
}
void rotate(int x)
{
    bool b=dir(x);
    int y=fa[x];
    int z=fa[y],a=ch[x][!b];
    if(z==0)
        root=x;
    else
        ch[z][dir(y)]=x;
    fa[x]=z;
    ch[x][!b]=y;
    fa[y]=x;
    ch[y][b]=a;
    if(a)   fa[a]=y;
}
void splay(int x,int i)
{
    while(fa[x]!=i)
    {
        int y=fa[x];
        int z=fa[y];
        if(z==i)
            rotate(x);
        else
        {
            bool b=dir(x),c=dir(y);
            if(b^c)
            {
                rotate(x);
                rotate(x);
            }
            else
            {
                rotate(y);
                rotate(x);
            }
        }
    }
}
void insert(int &k,int x,int last)
{
    if(k==0)
    {
        k=++tot;
        key[k]=x;
        fa[k]=last;
        splay(k,0);
        return ;
    }
    if(x<key[k])
        insert(ch[k][0],x,k);
    else
        insert(ch[k][1],x,k);
}
void find_pre(int i,int x)
{
    if(i==0)
        return ;
    if(key[i]<x)
    {
        t1=i;
        find_pre(ch[i][1],x);
    }
    else
        find_pre(ch[i][0],x);
}
void find_nxt(int i,int x)
{
    if(i==0)
        return ;
    if(key[i]>x)
    {
        t2=i;
        find_nxt(ch[i][0],x);
    }
    else
        find_nxt(ch[i][1],x);
}
void del(int x)
{
    splay(x,0);
    if(!ch[x][0])
    {
        root=ch[x][1];
        fa[ch[x][1]]=0;
        return ;
    }
    if(!ch[x][1])
    {
        root=ch[x][0];
        fa[ch[x][0]]=0;
        return ;
    }
    find_pre(root,key[x]);
    splay(t1,root);
    ch[t1][1]=ch[root][1];
    fa[ch[root][1]]=t1;
    root=t1;    fa[t1]=0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值