bzoj3224 Tyvj 1728 普通平衡树

传送门

题目描述

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]

解释&代码:




你还想要解释嘛。。。
心好累。什么也不想讲。
见代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int INF=1e9;
struct splay_node
{
    int value,size,cnt;
    splay_node *ch[2],*pre;
    void update()
    {
        size=ch[0]->size+ch[1]->size+cnt;
    }
    int get_wh()
    {
        return pre->ch[0]==this?0:1;
    }
    void set_ch(int wh,splay_node *child);
}pool[100010],*null,*root;
void splay_node::set_ch(int wh,splay_node *child)
{
    ch[wh]=child;
    if(child!=null) child->pre=this;
    update();
}
int n,opt,x,tot;
void read(int &n)
{
    n=0;
    char c;bool b=0;
    while((c=getchar())==' '||c=='\n'||c=='\r');
    if(c=='-') b=1;
    else n=c-48;
    while(isdigit(c=getchar())) n=n*10+c-48;
    if(b) n*=-1;
}
inline splay_node *getnew(int value)
{
    splay_node *newnode=pool+ ++tot;
    newnode->ch[0]=newnode->ch[1]=newnode->pre=null;
    newnode->cnt=1;
    newnode->size=1;
    newnode->value=value;
    return newnode;
}
inline void rotate(splay_node *&now)
{
    splay_node *father=now->pre,*grand=now->pre->pre;
    int wh=now->get_wh();
    father->set_ch(wh,now->ch[wh^1]);
    now->set_ch(wh^1,father);
    now->pre=grand;
    if(grand!=null) grand->ch[grand->ch[0]==father?0:1]=now;
}
inline void splay(splay_node *now,splay_node *tar)
{
    for(;now->pre!=tar;rotate(now))
      if(now->pre->pre!=tar)
        now->get_wh()==now->pre->get_wh()?rotate(now->pre):rotate(now);
    if(tar==null) root=now;
}
inline void insert(int value)
{
    splay_node *last=null,*now=root,*newone=getnew(value);
    while(now!=null)
    {
        last=now;
        if(newone->value==now->value)
        {
            now->cnt++;
            now->size++;
            splay(now,null);
            return;
        }
        if(newone->value<now->value) now=now->ch[0];
        else now=now->ch[1];
    }
    if(last==null) root=newone;
    else
    {
        if(newone->value<last->value) last->set_ch(0,newone);
        else last->set_ch(1,newone);
        splay(newone,null);
    }
}
inline splay_node *find(int value)
{
    splay_node *now=root;
    while(now!=null)
    {
        if(now->value==value) break;
        if(value<now->value) now=now->ch[0];
        else now=now->ch[1];
    }
    if(now!=null) splay(now,null);
    return now;
}
inline void del(int value)
{
    splay_node *now=find(value);
    if(now==null) return;
    if(now->cnt>1)
    {
        now->cnt--;
        now->size--;
        return;
    }
    if(now->ch[0]==null&&now->ch[1]==null)
    {
        root=null;
        return;
    }
    if(now->ch[1]==null)
    {
        now->ch[0]->pre=null;
        root=now->ch[0];
        return;
    }
    if(now->ch[0]==null)
    {
        now->ch[1]->pre=null;
        root=now->ch[1];
        return;
    }
    splay_node *_=now->ch[0];
    while(_->ch[1]!=null) _=_->ch[1];
    splay(_,now);
    _->set_ch(1,now->ch[1]);
    _->pre=null;
    root=_;
}
inline int rank(int value)
{
    splay_node *now=root;
    int ranking=0; 
    while(now!=null)
    {
        if(now->value==value)
        {
            ranking+=now->ch[0]->size+1;
            splay(now,null);
            return ranking;
        }
        if(value<now->value) now=now->ch[0];
        else ranking+=now->ch[0]->size+now->cnt,now=now->ch[1];
    }
    return -1;
}
inline int kth(int k)
{
    splay_node *now=root;
    int ranking=0;
    while(now!=null)
    {
        int tmp=ranking+now->ch[0]->size;
        if(tmp+1<=k&&k<=tmp+now->cnt)
        {
            splay(now,null);
            return now->value;
        }
        if(k<=tmp) now=now->ch[0];
        else ranking=tmp+now->cnt,now=now->ch[1];
    }
    return -1;
}
inline int pre(int value)
{
    splay_node *now=root;
    int ans=-INF;
    while(now!=null)
      if(now->value<value)
      {
        ans=max(ans,now->value);
        now=now->ch[1];
      }
      else now=now->ch[0];
    return ans;
}
inline int next(int value)
{
    splay_node *now=root;
    int ans=INF;
    while(now!=null)
      if(now->value>value)
      {
        ans=min(ans,now->value);
        now=now->ch[0];
      }
      else now=now->ch[1];
    return ans;
}
int main()
{
    null=pool;
    null->ch[0]=null->ch[1]=null->pre=null;
    null->size=0;
    root=null;
    read(n);
    while(n--)
    {
        read(opt),read(x);
        if(opt==1) insert(x);
        else if(opt==2) del(x);
        else if(opt==3) printf("%d\n",rank(x));
        else if(opt==4) printf("%d\n",kth(x));
        else if(opt==5) printf("%d\n",pre(x));
        else printf("%d\n",next(x));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值