Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数optxopt表示操作的序号(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. 每个数的数据范围:[2×109,2×109]

Source

平衡树

思路

2018年第一篇博客啊!
平衡树裸题。我用的splay和可持久化treap两种方法。

代码(splay版)

#include <cstdio>
#include <iostream>

const int maxn=100000;

struct splay_tree
{
  int son[2][maxn+10],fa[maxn+10],size[maxn+10],val[maxn+10],tot,root,cnt[maxn+10];

  inline int t(int x)
  {
    return son[1][fa[x]]==x;
  }

  inline int updata(int x)
  {
    size[x]=cnt[x];
    if(son[0][x])
      {
        size[x]+=size[son[0][x]];
      }
    if(son[1][x])
      {
        size[x]+=size[son[1][x]];
      }
    return 0;
  }

  inline int rotate(int x)
  {
    int k=t(x),f=fa[x];
    fa[x]=fa[f];
    if(fa[f])
      {
        son[t(f)][fa[f]]=x;
      }
    son[k][f]=son[!k][x];
    if(son[!k][x])
      {
        fa[son[!k][x]]=f;
      }
    son[!k][x]=f;
    fa[f]=x;
    updata(f);
    updata(x);
    return 0;
  }

  inline int splay(int x,int c)
  {
    while(fa[x]!=c)
      {
        int f=fa[x];
        if(fa[f]==c)
          {
            rotate(x);
          }
        else if(t(x)==t(f))
          {
            rotate(f);
            rotate(x);
          }
        else
          {
            rotate(x);
            rotate(x);
          }
      }
    if(c==0)
      {
        root=x;
      }
    return 0;
  }

  inline int newnode(int x)
  {
    ++tot;
    val[tot]=x;
    cnt[tot]=1;
    size[tot]=1;
    return 0;
  }

  inline int ins(int x)
  {
    if(!root)
      {
        newnode(x);
        fa[tot]=son[0][tot]=son[1][tot]=0;
        root=tot;
        return 0;
      }
    int now=root;
    while(now)
      {
        if(val[now]==x)
          {
            ++cnt[now];
            ++size[now];
            break;
          }
        else
          {
            int k=(val[now]<x);
            if(!son[k][now])
              {
                newnode(x);
                fa[tot]=now;
                son[0][tot]=son[1][tot]=0;
                son[k][now]=tot;
                now=son[k][now];
                break;
              }
            now=son[k][now];
          }
      }
    splay(now,0);
    return 0;
  }

  inline int findnode(int x)
  {
    int now=root;
    while(now)
      {
        if(val[now]==x)
          {
            break;
          }
        else if(val[now]<x)
          {
            now=son[1][now];
          }
        else
          {
            now=son[0][now];
          }
      }
    splay(now,0);
    return now;
  }

  inline int del(int x)
  {
    findnode(x);
    if(cnt[root]>1)
      {
        --cnt[root];
        --size[root];
        return 0;
      }
    int p=son[0][root];
    if(!p)
      {
        if(son[1][root])
          {
            fa[son[1][root]]=0;
          }
        root=son[1][root];
        return 0;
      }
    while(son[1][p])
      {
        p=son[1][p];
      }
    splay(p,root);
    son[1][p]=son[1][root];
    fa[p]=0;
    if(son[1][root])
      {
        fa[son[1][root]]=p;
      }
    updata(p);
    cnt[root]=0;
    size[root]=0;
    root=p;
    return 0;
  }

  inline int getpre(int x)
  {
    ins(x);
    int w=findnode(x);
    int now=son[0][w];
    if(!now)
      {
        return 0;
      }
    while(son[1][now])
      {
        now=son[1][now];
      }
    del(x);
    return val[now];
  }

  inline int getnext(int x)
  {
    ins(x);
    int w=findnode(x);
    int now=son[1][w];
    if(!now)
      {
        del(x);
        return 0;
      }
    while(son[0][now])
      {
        now=son[0][now];
      }
    del(x);
    return val[now];
  }

  inline int getrank(int x)
  {
    x=findnode(x);
    return size[son[0][x]]+1;
  }

  inline int getkth(int x)
  {
    if(size[root]<x)
      {
        return 0;
      }
    int now=root;
    while(now)
      {
        if((size[son[0][now]]+1<=x)&&(size[son[0][now]]+cnt[now]>=x))
          {
            break;
          }
        else if(size[son[0][now]]+cnt[now]<x)
          {
            x-=size[son[0][now]]+cnt[now];
            now=son[1][now];
          }
        else
          {
            now=son[0][now];
          }
      }
    return val[now];
  }
};

splay_tree st;
int n,opt,x;

int main()
{
  scanf("%d",&n);
  while(n--)
    {
      scanf("%d%d",&opt,&x);
      switch(opt)
        {
        case 1:
          {
            st.ins(x);
            break;
          }
        case 2:
          {
            st.del(x);
            break;
          }
        case 3:
          {
            printf("%d\n",st.getrank(x));
            break;
          }
        case 4:
          {
            printf("%d\n",st.getkth(x));
            break;
          }
        case 5:
          {
            printf("%d\n",st.getpre(x));
            break;
          }
        case 6:
          {
            printf("%d\n",st.getnext(x));
            break;
          }
        }
    }
  return 0;
}

代码(可持久化treap)

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;

const int sor='y'+'c'+'z'+' '+'i'+'s'+' '+'o'+'u'+'r'+' '+'s'+'u'+'n';

struct treap
{ 
    int tot,root,son[3000009][2],size[3000009],val[3000009];

    int updata(int a)
    {
        return size[a]=size[son[a][0]]+size[son[a][1]]+1;
    }

    int random(int limit)
    {
        return rand()%limit+1;
    }

    int merge(int a,int b) 
    {
        if ((a==0)||(b==0))
        {
            return a+b;
        }
        if (random(size[a]+size[b])<=size[a])
        {
            son[a][1]=merge(son[a][1],b);
            updata(a);
            return a;
        }
        else
        {
            son[b][0]=merge(a,son[b][0]);
            updata(b);
            return b;
        }
    }

    pii split(int a,int k)
    {
        if (size[a]==k)
        {
            return make_pair(a,0);
        }
        if (k==0)
        {
            return make_pair(0,a);
        }
        pii t;
        if (size[son[a][0]]>=k)
        {
            t=split(son[a][0],k);
            son[a][0]=t.second;
            updata(a);
            return make_pair(t.first,a);
        }
        else
        {
            t=split(son[a][1],k-size[son[a][0]]-1);
            son[a][1]=t.first;
            updata(a);
            return make_pair(a,t.second);
        }
    }

    int rank(int v)
    {
        int i=root,ans=0;
        while (i!=0)
        {
            if (val[i]>=v)
            {
                i=son[i][0];
            }
            else
            {
                ans+=size[son[i][0]]+1;
                i=son[i][1];
            }
        }
        return ans+1;
    }

    int id(int v,int k)
    {
        if (k==0)
        {
            return 0;
        }
        if (size[son[v][0]]>=k)
        {
            return id(son[v][0],k);
        }
        if (size[son[v][0]]+1==k)
        {
            return v;
        }
        return id(son[v][1],k-size[son[v][0]]-1);
    }

    int getval(int v)
    {
        return val[id(root,v)];
    }

    int pre(int v)
    {
        return val[id(root,rank(v)-1)];
    }

    int next(int v)
    {
        return val[id(root,rank(v+1))];
    }

    int insert(int v)
    {
        tot++;
        son[tot][0]=son[tot][1]=0;
        val[tot]=v;
        size[tot]=1;
        pii t=split(root,rank(v)-1);
        root=merge(merge(t.first,tot),t.second);
        return root;
    }

    int adelete(int v)
    {
        int x;
        x=rank(v);
        pii s,t;
        s=split(root,x-1);
        t=split(s.second,1);
        root=merge(s.first,t.second);
        return root;
    }
};

treap tree;
int n,opt,x;

int main()
{
    srand(sor);
    scanf("%d",&n);
    while (n--)
    {
        scanf("%d%d",&opt,&x);
        if (opt==1)
        {
            tree.insert(x);
        }
        if (opt==2)
        {
            tree.adelete(x);
        }
        if (opt==3)
        {
            printf("%d\n",tree.rank(x));
        }
        if (opt==4)
        {
            printf("%d\n",tree.getval(x));
        }
        if (opt==5)
        {
            printf("%d\n",tree.pre(x));
        }
        if (opt==6)
        {
            printf("%d\n",tree.next(x));
        }
    }
    return 0;
}