平衡树

· · 个人记录

平衡树

Treap

Splay

namespace Splay
{
    int val[N],pos[N],fa[N],son[N][2],siz[N],root; 

    inline bool which(int x)
    {
        return son[fa[x]][1]==x;
    }
    inline void update(int x)
    {
        siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;
    }
    void build(int l,int r,int f)
    {
        int x=l+r>>1;
        val[x]=a[x],pos[a[x]]=x;
        if(f)
        {
            if(f>r) son[f][0]=x;
            else son[f][1]=x;
            fa[x]=f;
        }
        if(l<=x-1) build(l,x-1,x);
        if(x+1<=r) build(x+1,r,x);  
        update(x);
    }
    inline void rotate(int x)
    {
        int d=which(x),f=fa[x];
        int ff=fa[f],y=son[x][d^1];
        fa[x]=ff,fa[y]=f,fa[f]=x;
        son[ff][son[ff][1]==f]=x,son[x][d^1]=f,son[f][d]=y;
        update(f),update(x);
    }
    inline void splay(int x,int p)
    {
        for(int f;fa[x]!=p;rotate(x))
            if(fa[f=fa[x]]!=p)
                rotate(which(x)^which(f)?x:f);
        if(!p) root=x;
    }
    void print(int x)
    {
        if(!x) return;
        print(son[x][0]);
        printf("%d ",val[x]);
        print(son[x][1]);
    }
}using namespace Splay;
inline void ins(int x)
{
    if(!root)//根为空 
    {
        tot++;
        son[tot][0]=son[tot][1]=f[tot]=0;
        root=tot;
        siz[tot]=cnt[tot]++;
        val[tot]=x;
        return;
    }
    int now=root,fa;
    while(1)
    {
        if(x==val[now])//找到了 
        {
            cnt[now]++;
            update(now);
            update(fa);
            splay(now);
            break;
        }
        fa=now;
        now=son[now][val[now]<x];
        if(!now)
        {
            tot++;
            son[tot][0]=son[tot][1]=0;
            siz[tot]=cnt[tot]=1;
            f[tot]=fa,val[tot]=x;
            son[fa][val[fa]<x]=tot;

            update(fa);
            splay(tot);
            break;
        }
    }
}

inline int find_num(int x)
{
    int now=root;
    while(1)
    {
        if(son[now][0]&&x<=siz[son[now][0]])
            now=son[now][0];
        else 
        {
            int temp=siz[son[now][0]]+cnt[now];
//          int temp=(son[now][0]?siz[son[now][0]]:0)+cnt[now];
            if(x<=temp) return val[now];
            x-=temp,now=son[now][1];
        }
    }
}
inline int find_rank(int x)
{
    int now=root,ans=0;
    while(1)
    {
        if(x<val[now])
        {
            if(!son[now][0]) return ans+1;
            now=son[now][0];
        }

        else
        {
            ans+=siz[son[now][0]];
//          ans+=(son[now][0]?siz[son[now][0]]:0);
            if(x==val[now]) return splay(now),ans+1;
            ans+=cnt[now],now=son[now][1];
        }
    }
}
inline int find_pre()
{
    int now=son[root][0];
    while(son[now][1]) now=son[now][1];
    return now;
}
inline int find_suf()
{
    int now=son[root][1];
    while(son[now][0]) now=son[now][0];
    return now;
}
inline void del(int x)
{
    find_rank(x);
    if(cnt[root]>1)
    {
        cnt[root]--;
        update(root);
        return ;
    }
    if(!son[root][0]&&!son[root][1]) return clear(root),root=0;

    if(!son[root][0])
    {
        int old=root;
        root=son[root][1],f[root]=0;
        clear(old);
        return;
    }
    else if(!son[root][1])
    {
        int old=root;
        root=son[root][0],f[root]=0;
        clear(old);
        return;
    }
    int left=find_pre(),old=root;
    splay(left);//一定异构 
    son[root][1]=son[old][1],f[son[old][1]]=root;
    clear(old),update(root);
}