Splay练习1——P3369 【模板】普通平衡树(Treap/SBT)

本文详细介绍了Splay树的基本操作及模板实现,包括插入、删除、查找等,并提供了完整的代码示例。通过本文,读者可以快速掌握Splay树的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蒟蒻的垂死挣扎

搞一波splay吧? ——好。

                                                               (以上是一切罪恶的根源)


毕竟之前写过几次splay的,这次重新捡回来还是比较轻松的(仅限于模板orz)

好啦先从模板复习起,打个两三遍什么的不就好了。

模板支持的是几个基本操作,有几个值得注意的地方:

1.添加inf 和-inf 两个哨兵节点,防止炸裂。

2.查找可以有两种方式,一个是根据键值查找,一个是根据排名,具体情况一定要想清楚自己splay维护的是什么。

3.根据splay形状性质进行操作,合理利用其特性,比如删除一个数,就将其前驱后继转到一起,这些应用十分灵活,自己yy。

4.每次将splay更改为特定形状的操作一定要注意splay保证复杂度,比如insert后要转上去。


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#define RG register
#define N 500100
#define ls s[x][0]
#define rs s[x][1]
#define ll long long
#define ld long double
using namespace std;

inline int read(){
  RG int x=0,o=1; RG char ch=getchar();
  while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
  if(ch=='-') o=-1,ch=getchar();
  while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
  return x*o;
}

int siz[N],s[N][2],num[N],fa[N],w[N],root,tot;

inline void Pushup(RG int x) { siz[x]=siz[ls]+siz[rs]+num[x]; }

inline void Rotate(RG int x){
    RG int y=fa[x],z=fa[y],k=s[y][1]==x,ot=s[x][k^1];
    s[z][s[z][1]==y]=x,s[x][k^1]=y,s[y][k]=ot;
    fa[x]=z,fa[ot]=y,fa[y]=x; Pushup(y),Pushup(x);
}

inline void Splay(RG int x,RG int goal){
    while(fa[x]!=goal){
        RG int y=fa[x],z=fa[y];
        if(z!=goal) (s[y][0]==x)^(s[z][0]==y)?Rotate(x):Rotate(y);
        Rotate(x);
    } if(!goal) root=x;
}

inline void Insert(RG int x){
    RG int u=root,fat=0;
    while(u&&w[u]!=x) fat=u,u=s[u][x>w[u]];
    if(u) ++num[u];
    else{
        u=++tot; if(fat) s[fat][w[fat]<x]=u;
        siz[u]=1,fa[u]=fat,num[u]=1,w[u]=x;
    } Splay(u,0);
}

inline void Find(RG int x){
    RG int u=root; //if(!u) return ;
    while(s[u][x>w[u]]&&x!=w[u]) u=s[u][x>w[u]];
    Splay(u,0);
}

inline int Next(RG int x,RG int op){
    Find(x); RG int u=root;
    if(w[u]<x&&!op) return u;
    if(w[u]>x&&op)  return u;
    u=s[u][op];
    while(s[u][op^1]) u=s[u][op^1];
    return u;
}

inline void Del(RG int x){
    RG int las=Next(x,0),nxt=Next(x,1);
    Splay(las,0),Splay(nxt,las);
    RG int goal=s[nxt][0];
    if(num[goal]>1) --num[goal],Splay(goal,0);
    else            s[nxt][0]=0;
}

inline int Kth(RG int x){
    RG int u=root;
    if(siz[u]<x) return 0;
    while(233){
        if(x>siz[s[u][0]]+num[u]) x-=siz[s[u][0]]+num[u],u=s[u][1];
        else  if(x<=siz[s[u][0]]) u=s[u][0];
        else  return w[u];
    }
}

int main(){
    RG int T=read(); Insert(2147483647),Insert(-2147483647);
    while(T--){
        RG int op=read(),x=read();
        if(op==1) Insert(x);
        if(op==2) Del(x);
        if(op==3) Find(x),printf("%d\n",siz[s[root][0]]);
        if(op==4) printf("%d\n",Kth(x+1));
        if(op==5) printf("%d\n",w[Next(x,0)]);
        if(op==6) printf("%d\n",w[Next(x,1)]);
    } return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值