「模板」Splay

代码说明

对于一些变量进行说明:

变量名 说明
rt 树根
ff[u] \(u\) 的父节点,特别地, ff[rt]=0
`ch[u][0 1]`
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;
}

扩展版

这个板子是针对有 pushdown()pushup() 操作的 Splay ,这里的模板题目是 POJ3580

#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:
    struct node{
        int ff,ch[2],siz,val,lag,delta,minn;
        node(){ff=ch[0]=ch[1]=siz=val=lag=delta=minn=0;}
    };
    // node t[MAXN+5];
    #define ch(x,i) t[x].ch[i]
    vector<node>t;
    int rt,ncnt,__siz;

    vector<int>pol;
    #define SUBBUFFERSIZ 100005
    inline void release(const int u){
        if(u)pol.push_back(u);
    }
    inline void addBuffer(){
        fep(i,__siz+SUBBUFFERSIZ,__siz+1)pol.push_back(i);
        __siz+=SUBBUFFERSIZ;
    }
    inline int generate(){
        if(pol.empty())addBuffer();
        int ret=pol.back();
        pol.pop_back();
        if(ret>ncnt)++ncnt,t.push_back(node());
        return ret;
    }
public:
    Splay(){
        rt=ncnt=__siz=0;
        addBuffer();
        t.push_back(node());
        t[0].minn=INF;//pay attention !
    }
    inline void pushup(const int x){
        t[x].siz=t[ch(x,0)].siz+t[ch(x,1)].siz+1;
        t[x].minn=Min(t[x].val,Min(t[ch(x,0)].minn,t[ch(x,1)].minn));
    }
    inline void add(const int u,const int delta){
        if(!u)return;
        t[u].val+=delta;
        t[u].delta+=delta;
        t[u].minn+=delta;
    }
    inline void revers(const int u){
        if(!u)return;
        swap(ch(u,0),ch(u,1));
        t[u].lag^=1;
    }
    inline void pushdown(const int x){
        if(t[x].delta){
            add(ch(x,0),t[x].delta);
            add(ch(x,1),t[x].delta);
            t[x].delta=0;
        }
        if(t[x].lag){
            revers(ch(x,0));
            revers(ch(x,1));
            t[x].lag=0;
        }
    }
    inline void rotate(const int x){
        const int y=t[x].ff,z=t[y].ff,k=(ch(y,1)==x);
        pushdown(y),pushdown(x);
        if(z)ch(z,ch(z,1)==y)=x;
        t[x].ff=z;
        ch(y,k)=ch(x,k^1);
        t[ch(x,k^1)].ff=y;
        ch(x,k^1)=y;
        t[y].ff=x;
        pushup(y),pushup(x);
    }
    inline void splay(const int x,const int goal=0){
        int y,z;
        pushdown(x);
        //是否需要 pushdown ?
        while(t[x].ff!=goal){
            y=t[x].ff,z=t[y].ff;
            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 int rnkFind(int rnk,const int f=0){
        int u=rt,y;
        if(!u || t[u].siz<rnk)return 0;
        while(233){
            pushdown(u);
            y=ch(u,0);
            if(rnk>t[y].siz+1){
                rnk-=t[y].siz+1;
                u=ch(u,1);
            }
            else if(rnk<=t[y].siz)u=ch(u,0);
            else return f?splay(u),u:u;
        }
    }
    inline void rnkInsert(const int rnk,const int x){
        rnkFind(rnk,1);
        int u=rt,ins=generate(),ext=ch(rt,1);

        // printf("Now new node ins == %d, u == %d\n",ins,u);

        if(u)ch(u,1)=ins;
        t[ins].ff=u;
        ch(ins,0)=0,ch(ins,1)=ext;
        if(ext)t[ext].ff=ins;
        t[ins].siz=1,t[ins].val=t[ins].minn=x;
        t[ins].lag=t[ins].delta=0;

        // printf("the new node's info :\n");
        // printf("ins == %d, lc == %d, rc == %d, ff == %d, val == %d, siz == %d, delta == %d, lag == %d\n",ins,t[ins].ch[0],t[ins].ch[1],t[ins].ff,t[ins].val,t[ins].siz,t[ins].delta,t[ins].lag);

        splay(ins);
    }
    inline void rnkDelete(const int rnk){
        int pre=rnkFind(rnk-1);
        int suf=rnkFind(rnk+1);
        splay(pre);
        splay(suf,pre);
        release(ch(suf,0));
        ch(suf,0)=0;
        splay(suf);
    }
    inline void Reverse(const int lrnk,const int rrnk){
        int l=lrnk-1,r=rrnk+1;
        int ln=rnkFind(l);
        int rn=rnkFind(r);
        splay(ln,0);
        splay(rn,ln);
        revers(ch(rn,0));
        return;
    }
    inline void modify(const int lrnk,const int rrnk,const int delta){
        int l=lrnk-1,r=rrnk+1;
        int ln=rnkFind(l);
        int rn=rnkFind(r);
        splay(ln);

        // printf("After the first splay, the tree :\n");
        // chk_tre();
        // Endl;

        splay(rn,ln);
        // printf("After the second splay, the tree :\n");
        // chk_tre();
        // Endl;

        // printf("modify : ln == %d, rn == %d\n",ln,rn);
        // printf("add node %d, delta == %d\n",ch(rn,0),delta);
        add(ch(rn,0),delta);
        splay(rn);
        return;
    }
    inline int Getminn(const int lrnk,const int rrnk){
        int l=lrnk-1,r=rrnk+1;
        int ln=rnkFind(l);
        int rn=rnkFind(r);
        splay(ln);
        splay(rn,ln);
        return t[ch(rn,0)].minn;
    }
    inline void Revolve(const int lrnk,const int rrnk,const int k){
        Reverse(rrnk-k+1,rrnk);
        Reverse(lrnk,rrnk-k);
        Reverse(lrnk,rrnk);
    }
    inline int Atrnk(const int rnk){
        // rnkFind(rnk,1);
        int u=rnkFind(rnk);
        return t[u].val;
    }
    inline void chk_tre(const int u=-1,int rnk=0){
        // if(rnk>10)return;

        if(u==-1)return chk_tre(rt);

        if(ch(u,0))chk_tre(ch(u,0),rnk);

        rnk+=t[ch(u,0)].siz;
        printf("rnk == %d\n",++rnk);
        printf("Now u == %d, lc == %d, rc == %d, ff == %d, val == %d, siz == %d, delta == %d, lag == %d\n",u,t[u].ch[0],t[u].ch[1],t[u].ff,t[u].val,t[u].siz,t[u].delta,t[u].lag);
        Endl;

        if(ch(u,1))chk_tre(ch(u,1),rnk);
    }
    inline void Writtre(const int u=-1){
        if(u==-1)return Writtre(rt);
        pushdown(u);
        if(ch(u,0))Writtre(ch(u,0));
        if(t[u].val!=-INF && t[u].val!=INF)
            printf("%d ",t[u].val);
        if(ch(u,1))Writtre(ch(u,1));
    }
}tre;

int n,m;

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    qread(n);
    tre.rnkInsert(0,-INF);
    rep(i,1,n)tre.rnkInsert(i,qread());
    tre.rnkInsert(n+1,INF);

    // tre.chk_tre();
    // Endl;

    qread(m);

    char opt[20];
    int x,y;
    while(m--){
        scanf("%s",opt+1);
        x=qread();
        if(opt[1]=='D')tre.rnkDelete(x+1);
        else y=qread();
        if(opt[1]=='A')tre.modify(x+1,y+1,qread());
        else if(opt[1]=='R' && opt[4]=='E')tre.Reverse(x+1,y+1);
        else if(opt[1]=='R')tre.Revolve(x+1,y+1,qread()%(y-x+1));
        else if(opt[1]=='I')tre.rnkInsert(x+1,y);
        else if(opt[1]=='M')writc(tre.Getminn(x+1,y+1),'\n');
        // puts("After this option, the tre : ");
        // tre.chk_tre();
        // tre.Writtre();
        // Endl;
    }
    return 0;
}
posted @ 2020-01-08 21:32  南枙向暖  阅读(235)  评论(0编辑  收藏  举报