平衡树模板

#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];    //当前节点表示的值

///将当前点的各项清0
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];
        }
    }
}

///求x的前驱,前驱定义为小于x,最大的数
int preve()
{
    int now=ch[root][0];
    while(ch[now][1]) now=ch[now][1];
    return now;
}

///求x的后继,后继定义为大于x,且最小的数
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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值