算法模板——splay

splay

一种二叉查找树(BST),操作核心是splay操作。

BST?

定义:一棵二叉树,其中每一个节点的左儿子的权值都小于自己再小于右儿子的权值。
支持操作:插入,删除,查找第 k 大,查找x的排名,查找前驱,查找后继。
如果数据随机,那么不调整树,只是按题目意思插入,那么是可以过的;
如果出题人很(sang)有(xin)意(bin)思(kuang)的出了一组权值递增的数据,那么必须调整树的结构,否则就是一条链了;
那么如何调整呢?先看两种基本操作:zig和zag

旋转操作

左旋和右旋;
首先看图:
这里写图片描述
可以发现:左旋和右旋互为反操作;
这个有什么用呢?
图中右旋相当与将 x 向根节点提了1个深度;
左旋则将 f 向根提了1个深度。
先放代码吧:

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

t(i) 代表的是 i 是它父亲的哪个儿子;
k=0时左旋, k=1 时右旋。
这个操作就是整个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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值