平衡树模板

时间:2019-04-20
本文章向大家介绍平衡树模板,主要包括平衡树模板使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N=1e6+10;

int ch[N][2];  //ch[][0] 表示左儿子 ch[][1] 表示右儿子
int f[N];      //节点的父亲节点
int sz[N];     //当前节点给所在的子树的节点个数
int cnt[N];    //当前节点所表示的值的个数
int root;      //记录根节点的
int tot;       //计算树中节点个数
int key[N];    //当前节点表示的值

void array_clear(int x)
{
    f[x]=cnt[x]=ch[x][0]=ch[x][1]=key[x]=0;
}

bool get(int x)
{
    return ch[f[x]][1]==x;
}
///0 左  1 右

void pushup(int x)
{
    if(x)
    {
        sz[x]=cnt[x];
        if(ch[x][0]) sz[x]+=sz[ch[x][0]];
        if(ch[x][1]) sz[x]+=sz[ch[x][1]];
    }
}

void rotates(int x)
{
    int old=f[x],oldf=f[old],which=get(x);
    ch[old][which]=ch[x][which^1];
    f[ch[old][which]]=old;
    ///当前点的儿子过继给当前点的爸爸;
    ///同时处理父子两个方向上的信息
    ch[x][which^1]=old;
    f[old]=x;
    ///当前点变成曾经爸爸的爸爸;
    f[x]=oldf;///爷爷变爸爸
    if(oldf) ch[oldf][ch[oldf][1]==old]=x;///相等就是1,不等就是0
    pushup(old);///维护信息
    pushup(x);
}

void splay(int x)
{
    for(int fa;fa=f[x];rotates(x))
        if(f[fa])
        rotates((get(x)==get(fa))?fa:x);
    root=x;
}

void tree_insert(int x)
{
    if(root==0)
    {
        tot++;
        key[tot]=x;root=tot;
        cnt[tot]=sz[tot]=1;
        ch[tot][0]=ch[tot][1]=f[tot]=0;
        return;
    }
    int now=root,fa=0;
    while(true)
    {
        if(x==key[now])
        {
            cnt[now]++;
            pushup(now);pushup(fa);splay(now);
            return;
        }
        fa=now;now=ch[now][key[now]<x];
        if(now==0)
        {
            tot++;
            sz[tot]=cnt[tot]=1;
            ch[tot][0]=ch[tot][1]=0;
            ch[fa][x>key[fa]]=tot;
            f[tot]=fa;
            key[tot]=x;
            pushup(fa);splay(tot);return;
        }
    }
}

int kth(int x)///找到排名为x的点
{
    int now=root;
    while(true)
    {
        if(ch[now][0]&&x<=sz[ch[now][0]])
            now=ch[now][0];
        else
        {
            int tmp=sz[ch[now][0]]+cnt[now];
            if(x<=tmp)
                return key[now];
            x-=tmp;now=ch[now][1];
        }
    }
}

int rak(int x)///查询x的排名
{
    int now=root,ans=0;
    while(true)
    {
        if(x<key[now]) now=ch[now][0];
        else
        {
            ans+=sz[ch[now][0]];
            if(x==key[now])
            {
                splay(now);
                return ans+1;
            }
            ans+=cnt[now];
            now=ch[now][1];
        }
    }
}

int preve()
{
    int now=ch[root][0];
    while(ch[now][1]) now=ch[now][1];
    return now;
}

int Nextt()
{
    int now=ch[root][1];
    while(ch[now][0]) now=ch[now][0];
    return now;
}

void del(int x)
{
    rak(x);
    if(cnt[root]>1) {cnt[root]--; pushup(root); return ;}
    if(!ch[root][0]&&!ch[root][1]) {array_clear(root);root=0;return; }
    if(!ch[root][0])
    {
        int oldrt=root;
        root=ch[root][1];
        f[root]=0;
        array_clear(oldrt);
        return ;
    }
    else if(!ch[root][1])
    {
        int oldrt=root;
        root=ch[root][0];
        f[root]=0;
        array_clear(oldrt);
        return;
    }
    int oldrt=root;
    int leftbig=preve();
    splay(leftbig);
    ch[root][1]=ch[oldrt][1];
    f[ch[oldrt][1]]=root;
    array_clear(oldrt);
    pushup(root);
}

int main()
{
    int n,op,a;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&op,&a);
        if(op==1) tree_insert(a);
        else if(op==2) del(a);
        else if(op==3) printf("%d\n",rak(a));
        else if(op==4) printf("%d\n",kth(a));
        else if(op==5)
        {
            tree_insert(a);
            printf("%d\n",key[preve()]);
            del(a);
        }
        else
        {
            tree_insert(a);
            printf("%d\n",key[Nextt()]);
            del(a);
        }
    }
    return 0;
}