「模板」Splay

代码说明

对于一些变量进行说明:

变量名 说明
rt 树根
ff[u] \(u\) 的父节点,特别地, ff[rt]=0
ch[u][0|1] \(u\) 的 左/右儿子
siz[u] \(u\) 及其子树大小
val[u] \(u\) 对应的值
recy[u] \(u\) 对应的 val[u] 出现的次数

代码

#include<cstdio>
#include<vector>
using namespace std;

#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned

#ifdef FILEOI
# define MAXBUFFERSIZE 500000
    inline char fgetc(){
        static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
    }
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=1e5;
const int INF=0x3f3f3f3f;

class Splay{
private:
    int rt;
    int ff[MAXN+5];
    int ch[MAXN+5][2];
    int siz[MAXN+5];
    int recy[MAXN+5];
    int val[MAXN+5];
    vector<int>pool;
    inline void release(const int x){
        if(x)pool.push_back(x);
    }
    inline int generate(){
        int ret=pool.back();
        pool.pop_back();
        return ret;
    }
public:
    Splay(){
        rt=0;
        rep(i,1,MAXN)pool.push_back(i);
    }
    inline void pushup(const int x){
        siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+recy[x];
    }
    inline void Rotate(const int x){
        int y=ff[x],z=ff[y],k=(ch[y][1]==x);
        ch[z][ch[z][1]==y]=x;
        ff[x]=z;
        ch[y][k]=ch[x][k^1];
        ff[ch[x][k^1]]=y;
        ch[x][k^1]=y;
        ff[y]=x;
        pushup(y),pushup(x);
        //注意 上传 的先后顺序
    }
    /*
    void Rotate(int x)//旋转 
    {
        register int y=ff[x];
        register int z=ff[y];
        register int k=ch[y][1]==x;//x是y的左或右儿子
        ch[z][ch[z][1]==y]=x;
        ff[x]=z;
        ch[y][k]=ch[x][k^1];
        ff[ch[x][k^1]]=y;
        ch[x][k^1]=y;
        ff[y]=x;
        pushup(y);pushup(x);
    }
    */
    inline void splay(const int x,const int goal=0){
        int y,z;
        while(ff[x]!=goal){
            y=ff[x],z=ff[y];
            if(z!=goal)
                (ch[z][0]==y)^(ch[y][0]==x)?Rotate(x):Rotate(y);
            Rotate(x);
        }
        if(goal==0)rt=x;
        pushup(x);
    }
    inline void Insert(const int x){
        int u=rt,fa=0;
        while(u && val[u]!=x)fa=u,u=ch[u][val[u]<x];
        if(u)++recy[u];
        else{
            u=generate();
            if(fa)ch[fa][val[fa]<x]=u;
            ff[u]=fa,val[u]=x;
            ch[u][0]=ch[u][1]=0;
            siz[u]=recy[u]=1;
        }
        splay(u);
    }
    inline void Find(const int x){
        int u=rt;
        if(!u)return;
        while(x!=val[u] && ch[u][val[u]<x])u=ch[u][val[u]<x];
        splay(u);
    }
    inline int Next(const int x,const int f){
        Find(x);
        int u=rt;
        if((f && val[u]>x) || (!f && val[u]<x))return u;
        for(u=ch[u][f];ch[u][f^1];u=ch[u][f^1]);
        return u;
    }
    inline void Delete(const int x){
        int pre=Next(x,0);
        int suf=Next(x,1);
        // printf("pre_val == %d, suf_val == %d\n",val[pre],val[suf]);
        splay(pre);
        splay(suf,pre);
        int del=ch[suf][0];
        if(recy[del]>1){
            --recy[del];
            splay(del);
        }
        else{
            if(ch[suf][0])release(ch[suf][0]);
            ch[suf][0]=0;
            splay(suf);
            //这里需要 splay 吗 ?
        }
    }
    inline int Atrnk(int rnk){
        int u=rt,y;
        if(siz[u]<rnk)return -INF;
        while(233){
            y=ch[u][0];
            if(rnk>siz[y]+recy[u]){
                rnk-=siz[y]+recy[u];
                u=ch[u][1];
            }
            else if(rnk<=siz[y])u=ch[u][0];
            else return val[u];
        }
    }
    inline int Getrnk(const int x){
        Find(x);
        return siz[ch[rt][0]]+1;
    }
    inline int Getpre(const int x){
        int u=Next(x,0);
        return val[u];
    }
    inline int Getsuf(const int x){
        int u=Next(x,1);
        return val[u];
    }
    void chk_tre(int u=-1){
        if(u==-1)u=rt;
        printf("Now u == %d, ff == %d, val == %d, siz == %d, lc == %d, rc == %d, recy == %d\n",u,ff[u],val[u],siz[u],ch[u][0],ch[u][1],recy[u]);
        if(ch[u][0])chk_tre(ch[u][0]);
        if(ch[u][1])chk_tre(ch[u][1]);
    }
}Tre;

int n,opt,x;

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    Tre.Insert(INF);
    Tre.Insert(-INF);
    // Tre.chk_tre();
    // Endl;
    qread(n);
    while(n--){
        qread(opt,x);
        if(opt==1)Tre.Insert(x);
        else if(opt==2)Tre.Delete(x);
        else if(opt==3)writc(Tre.Getrnk(x)-1,'\n');
        else if(opt==4)writc(Tre.Atrnk(x+1),'\n');
        else if(opt==5)writc(Tre.Getpre(x),'\n');
        else writc(Tre.Getsuf(x),'\n');
        // Tre.chk_tre();
        // Endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/MachineryCountry/p/12168883.html