bzoj 3224: Tyvj 1728 普通平衡树(splay 模板题)

3224: Tyvj 1728 普通平衡树

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 7756   Solved: 3267
[ 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.每个数的数据范围:[-1e7,1e7]

数据如下http://pan.baidu.com/s/1jHMJwO2

Source

[ Submit][ Status][ Discuss]

 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
int sz,i,n,root;
int f[100003],ch[100003][5],size[100003],cnt[100003],key[100003];
void clear(int x)
{
    f[x]=ch[x][0]=ch[x][1]=size[x]=cnt[x]=key[x]=0;
}
int get(int x)
{
    return ch[f[x]][1]==x;
}
void update(int x)
{
    if (x)
     {
        size[x]=cnt[x];
        if (ch[x][0])  size[x]+=size[ch[x][0]];
        if (ch[x][1])  size[x]+=size[ch[x][1]];
     }
}
void rotate(int x)
{
    int fa=f[x],oldfa=f[f[x]],which=get(x);
    ch[fa][which]=ch[x][which^1];f[ch[fa][which]]=fa;//^表示取反,若为0变为1,若为1变为0
    f[fa]=x;ch[x][which^1]=fa;
    f[x]=oldfa;
    if (oldfa)
      ch[oldfa][ch[oldfa][1]==fa]=x;
    update(fa);update(x);
}
void splay(int x)
{
    for (int fa;(fa=f[x]);rotate(x))
     if (f[fa])
      rotate((get(x)==get(fa))?fa:x);
    root=x;
}
void insert(int x)
{
    if (root==0)
     {
        sz++;f[1]=ch[1][0]=ch[1][1]=0;size[1]=1;key[1]=x;cnt[1]=1;root=sz; return;
     }
    int now=root,fa=0;
    while (true)
     {
        if (key[now]==x)
         {
            cnt[now]++; update(now); update(fa); splay(now); return ;
         }
        fa=now;
        now=ch[now][key[now]<x];//x大于当前点,向右儿子方向
        if (now==0)//山穷水尽,自立为王  
         {
            sz++; size[sz]=1; key[sz]=x; cnt[sz]=1;
            ch[sz][0]=ch[sz][1]=0; f[sz]=fa; ch[fa][key[fa]<x]=sz;
            update(sz); update(fa); splay(sz);
            return;
         } 
     }
}
int find(int x)
{
  int ans=0,now=root;
  while (true)
  {
    if (x<key[now])
     {
        now=ch[now][0];
     }
    else
     {
        if (ch[now][0])
         ans+=size[ch[now][0]];
        if (key[now]==x)  { splay(now);return ans+1;}
        ans+=cnt[now];
        now=ch[now][1];
     }
  } 
  //return -1;
}
int findx(int x)
{
    int ans=0,now=root;
    while (true)
     {
        if (ch[now][0]&&x<=size[ch[now][0]])
         now=ch[now][0];
        else
         {
            ans=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];
            if (x<=ans) return key[now];//进入else,就说明该点要么在子树的根节点要么在右子树,如果x<左子树的大小+该点的个数,那么编号x的点必在子树的根节点无疑 
            x-=ans; now=ch[now][1];
         }
     }
}
int pre()
{
    int now=ch[root][0];
    while (ch[now][1]) now=ch[now][1];
    return  now;
}
int next()
{
    int now=ch[root][1];
    while(ch[now][0]) now=ch[now][0];
    return now; 
}
bool del(int x)
{
    if (find(x)==-1) return false;//find操作中已经把x转的根的位置 
    if (cnt[root]>1) 
     {
        cnt[root]--; return true;
     } 
    if (!ch[root][0]&&!ch[root][1])
     {
        clear(root); root=0; return true;
     }
    if (!ch[root][0])
     {
        int ordr=root; root=ch[root][1]; f[root]=0; clear(ordr); return true;
     }
    if (!ch[root][1])
     {
        int ordr=root; root=ch[root][0]; f[root]=0; clear(ordr); return true;
     }
    int leftbig=pre(),oldr=root;
    splay(leftbig);  f[ch[oldr][1]]=root;  ch[root][1]=ch[oldr][1]; clear(oldr); update(root);
    return true;
} 
int main()
{
    scanf("%d",&n);
    for (i=1;i<=n;i++)
     {
        int order,x;
        scanf("%d%d",&order,&x);
        switch(order)
         {
            case 1:insert(x);break;
            case 2:del(x); break;
            case 3:printf("%d\n",find(x));break;
            case 4:printf("%d\n",findx(x));break;
            case 5:insert(x); printf("%d\n",key[pre()]); del(x); break;
            case 6:insert(x); printf("%d\n",key[next()]); del(x); break;
         }
     }
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值