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

题目描述

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

1.插入x数

2.删除x数(若有多个相同的数,因只删除一个)

3.查询x数的排名(若有多个相同的数,因输出最小的排名)

4.查询排名为x的数

5.求x的前驱(前驱定义为小于x,且最大的数)

6.求x的后继(后继定义为大于x,且最小的数)

输入输出格式
输入格式:

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

输出格式:

对于操作3,4,5,6每行输出一个数,表示对应答案

输入输出样例

输入样例#1:
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

输出样例#1:
106465
84185
492737

说明

时空限制:1000ms,128M

1.n的数据范围:n<=100000

2.每个数的数据范围:[-1e7,1e7]


【分析】
真 · 模板

http://blog.csdn.net/clove_unique/article/details/50630280

这个博客真的很好懂↑


【代码】

//tyvj 1728 
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
int n,m,sz,opt,root;
int f[mxn],ch[mxn][2],key[mxn],cnt[mxn],size[mxn];
inline void clear(int x)
{
    f[x]=ch[x][0]=ch[x][1]=key[x]=cnt[x]=size[x]=0;
}
inline int get(int x)   //左子树or右子树 
{
    if(ch[f[x]][0]==x) return 0;
    return 1;
}
inline void update(int x)   //更新size 
{
    if(!x) return;
    size[x]=cnt[x];
    if(ch[x][0]) size[x]+=size[ch[x][0]];
    if(ch[x][1]) size[x]+=size[ch[x][1]];
}
inline void rotate(int x)
{
    int old=f[x],elder=f[old],which=get(x);
    ch[old][which]=ch[x][which^1],f[ch[x][which^1]]=old;
    ch[x][which^1]=old,f[old]=x;
    f[x]=elder;
    if(elder) ch[elder][ch[elder][1]==old]=x;
    update(old),update(x);
}
inline void splay(int x)
{
    for(int fa;(fa=f[x]);rotate(x))
      if(f[fa]) rotate((get(x)==get(fa)?fa:x));
    root=x;
}
inline void insert(int x)
{
    if(!root)
    {
        root=(++sz);
        ch[sz][0]=ch[sz][1]=f[sz]=0;
        key[sz]=x,cnt[sz]=1;
        return;
    }
    int now=root,fa=0;
    while(1)
    {
        if(key[now]==x)
        {
            cnt[now]++;
            update(now),update(fa);
            splay(now);
            return;
        }
        fa=now;now=ch[now][x>key[now]];
        if(now==0)
        {
            sz++;
            ch[sz][0]=ch[sz][1]=0,size[sz]=cnt[sz]=1,key[sz]=x;
            f[sz]=fa,ch[fa][x>key[fa]]=sz;
            update(fa);
            splay(sz);
            return;
        }
    }
}
inline int find(int x)   //x的排名 
{
    int ans=1,now=root;
    while(1)
    {
        if(x<key[now]) now=ch[now][0];
        else
        {
            ans+=(ch[now][0]?size[ch[now][0]]:0);
            if(x==key[now]) {splay(now);return ans;}
            ans+=cnt[now]; 
            now=ch[now][1];
        }
    }
}
inline int number(int x)
{
    int now=root;
    while(1)
    {
        if(ch[now][0] && x<=size[ch[now][0]]) now=ch[now][0];
        else
        {
            int tmp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];
            if(x<=tmp) return key[now];
            x-=tmp;now=ch[now][1];
        }
    }
}
inline int pre()
{
    int now=ch[root][0];
    while(ch[now][1]) now=ch[now][1];
    return now;
}
inline int next()
{
    int now=ch[root][1];
    while(ch[now][0]) now=ch[now][0];
    return now;
}
inline void del(int x)
{
    find(x);
    if(cnt[root]>1) {cnt[root]--;return;} 
    //1.only one point
    if(!ch[root][0] && !ch[root][1]) {clear(root);root=0;return;} 
    //2.no left child
    if(!ch[root][0]) {int old=root;root=ch[root][1];clear(old);f[root]=0;return;} 
    //3.no right child 
    if(!ch[root][1]) {int old=root;root=ch[root][0];clear(old);f[root]=0;return;} 
    //4.two children 
    int left=pre(),old=root;
    splay(left);
    f[ch[old][1]]=root;
    ch[root][1]=ch[old][1];
    clear(old);
    update(root); 
}
int main()
{
    int i,j,x;
    scanf("%d",&n);
    fo(i,1,n) 
    {
        scanf("%d%d",&opt,&x);
        if(opt==1) insert(x);
        if(opt==2) del(x);
        if(opt==3) printf("%d\n",find(x));   
        if(opt==4) printf("%d\n",number(x));
        if(opt==5) insert(x),printf("%d\n",key[pre()]),del(x);
        if(opt==6) insert(x),printf("%d\n",key[next()]),del(x);
    }
//    printf("haha\n");
    return 0;
}
//2
//1 106465
//1 317721
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值