BZOJ 3224 洛谷 3369 【模板】普通平衡树(Treap/SBT)

95 篇文章 1 订阅
55 篇文章 0 订阅

3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 14814 Solved: 6441
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
Input
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)
Output
对于操作3,4,5,6每行输出一个数,表示对应答案
Sample Input
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
106465
84185
492737
HINT
1.n的数据范围:n<=100000
2.每个数的数据范围:[-2e9,2e9]

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAXN 100010
int ch[MAXN][2],fa[MAXN],siz[MAXN],cnt[MAXN],data[MAXN],root,m,n,tot;

#define lc ch[u][0]
#define rc ch[u][1]
inline void UpDate(int x){
    siz[x]=cnt[x];
    if(ch[x][0]) siz[x]+=siz[ch[x][0]];
    if(ch[x][1]) siz[x]+=siz[ch[x][1]];
    return ;
}
inline void Create(int x){
    tot++; data[tot]=x; cnt[tot]=siz[tot]=1;
    ch[tot][0]=ch[tot][1]=fa[tot]=0;
    return ;
}

inline int getson(int x){ return x==ch[fa[x]][1]; }

void Rotate(int x){
    int f=fa[x],ffa=fa[f],k=getson(x);
    ch[f][k]=ch[x][k^1];fa[ch[f][k]]=f;
    ch[x][k^1]=f;fa[f]=x; fa[x]=ffa;
    if(ffa) ch[ffa][f==ch[ffa][1]]=x;
    UpDate(f);UpDate(x);
}
void Splay(int x){
    for(int f;f=fa[x];Rotate(x))
        if(fa[f]) Rotate(getson(x)==getson(f)?f:x);
    root=x;//这句很重要 
}
void Insert(int x){
    if(!root){ Create(x);root=tot; }
    else{
        int now=root,f=0;
        while(1){
            if(data[now]==x){
                cnt[now]++;siz[now]++;
                Splay(now);break;
            }
            f=now;  now=ch[f][x>data[f]];
            if(!now){//走到了叶节点
                Create(x);fa[tot]=f;
                ch[f][x>data[f]]=tot;Splay(tot);
                break;
            }
        }
    }
}

int Get_Rank(int x){//查询数x的排名
    int now=root,ans=0;
    while(1){
        if(x<data[now]) now=ch[now][0];
        else {
            ans+=siz[ch[now][0]];
            if(x==data[now]){ Splay(now);return ans+1; }
            ans+=cnt[now]; now=ch[now][1]; 
        }
    }
}
int Get_Rank_Is(int x){//查询排名为x的数
    int now=root;
    while(1){
        if(ch[now][0]&&x<=siz[ch[now][0]]) now=ch[now][0];
        else{
            int tmp=(ch[now][0]?siz[ch[now][0]]:0)+cnt[now];
            if(x<=tmp) return data[now];
            x-=tmp;now=ch[now][1];
        }
    }
}
inline int Get_Next(){// hou ji
    int p=ch[root][1];
    while(ch[p][0]) p=ch[p][0];
    return p;
}
inline int Get_Pre(){//qian qu
    int p=ch[root][0];
    while(ch[p][1]) p=ch[p][1];
    return p;
}
inline void clear(int x){
    ch[x][0]=ch[x][1]=fa[x]=cnt[x]=siz[x]=data[x]=0;
}
void Delete(int x){
    int rank_x=Get_Rank(x);
    //查了一下x  顺便将它转到了根的位置 
    //有这一句就能A  没有差别很大 
    if(cnt[root]>1){ cnt[root]--;siz[root]--;return ; }
    if(!ch[root][0]&&!ch[root][1]){ clear(root);root=0;return ; }
    if(!ch[root][0]){
        int tmp=root;
        root=ch[root][1];
        fa[root]=0;
        clear(tmp);
        return;
    }
    if(!ch[root][1]){
        int tmp=root;
        root=ch[root][0];
        fa[root]=0;
        clear(tmp);
        return;
    }
    int pre=Get_Pre(),tmp=root;
    Splay(pre);
    ch[root][1]=ch[tmp][1]; fa[ch[tmp][1]]=root;
    clear(tmp);UpDate(root);
}
int main(){
    scanf("%d",&n);
    while(n--){
        int opt,x;
        scanf("%d%d",&opt,&x);
        if(opt==1) Insert(x);
        if(opt==2) Delete(x);
        if(opt==3) printf("%d\n",Get_Rank(x));
        if(opt==4) printf("%d\n",Get_Rank_Is(x));
        if(opt==5) Insert(x),printf("%d\n",data[Get_Pre()]),Delete(x);
        if(opt==6) Insert(x),printf("%d\n",data[Get_Next()]),Delete(x);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值