Splay

struct Splay
{
    #define ls(x) t[x].lson
    #define rs(x) t[x].rson
    #define fa(x) t[x].pre
    #define grafa(x) t[t[x].pre].pre
    static const int __=200005;

    struct node
    {
        int pre,lson,rson;
        ll val,minn,ad;
        int siz,rev;
        void set(int p,int v,int s)
        {
            val=minn=v,siz=s;
            pre=p,lson=-1,rson=-1,
            ad=0,rev=0;
        }
        void operator+=(const node &b)
        {
            siz+=b.siz;
            minn=min(minn,b.minn);
        }
        void putrev(){rev^=1;}
        void putadd(ll v)
        {
            val+=v,minn+=v,ad+=v;
        }
    }t[__];

    int root,idx;

    Splay():root(0),idx(0){t[0].set(-1,0,0);}

    void build(int a[],int n)
    {
        rs(0)=build(0,a,1,n);
    }

    int build(int pre,int a[],int l,int r)
    {
        if(l>r)return -1;
        int m=(l+r)>>1,x=++idx;
        t[x].set(pre,a[m],1);
        ls(x)=build(x,a,l,m-1);
        rs(x)=build(x,a,m+1,r);
        if(~ls(x))t[x]+=t[ls(x)];
        if(~rs(x))t[x]+=t[rs(x)];
        return x;
    }

    void pushup(int x)
    {
        t[x].siz=(x!=0);
        t[x].minn=t[x].val;
        if(~ls(x))t[x]+=t[ls(x)];
        if(~rs(x))t[x]+=t[rs(x)];
    }

    void zig(int x)
    {
        int y=fa(x);
        if(~fa(y))
            if(ls(fa(y))==y)
                ls(fa(y))=x;
            else rs(fa(y))=x;
        fa(x)=fa(y),fa(y)=x;
        ls(y)=rs(x);
        if(~ls(y))fa(ls(y))=y;
        rs(x)=y;
        if(fa(x)==-1)root=x;
        pushup(y),pushup(x);
    }

    void zag(int x)
    {
        int y=fa(x);
        if(~fa(y))
            if(ls(fa(y))==y)
                ls(fa(y))=x;
            else rs(fa(y))=x;
        fa(x)=fa(y),fa(y)=x;
        rs(y)=ls(x);
        if(~rs(y))fa(rs(y))=y;
        ls(x)=y;
        if(fa(x)==-1)root=x;
        pushup(y),pushup(x);
    }

    void splay(int x,int wz=-1)
    {
        while(x!=wz && fa(x)!=wz)
            if(grafa(x)==wz)
                if(ls(fa(x))==x)zig(x);
                else zag(x);
            else if(ls(fa(x))==x && ls(grafa(x))==fa(x))
                zig(fa(x)),zig(x);
            else if(rs(fa(x))==x && rs(grafa(x))==fa(x))
                zag(fa(x)),zag(x);
            else if(ls(fa(x))==x && rs(grafa(x))==fa(x))
                zig(x),zag(x);
            else if(rs(fa(x))==x && ls(grafa(x))==fa(x))
                zag(x),zig(x);
    }

    void pushdown(int x)
    {
        if(t[x].rev)
        {
            swap(ls(x),rs(x));
            if(ls(x)>0)t[ls(x)].putrev();
            if(rs(x)>0)t[rs(x)].putrev();
            t[x].rev=0;
        }
        if(t[x].ad)
        {
            ll &add=t[x].ad;
            if(ls(x)>0)t[ls(x)].putadd(add);
            if(rs(x)>0)t[rs(x)].putadd(add);
            add=0;
        }
    }

    int find_node(int wz)
    {
        int x=root;
        while(~x)
        {
            pushdown(x);
            int k=(x!=0);
            if(~ls(x))
                k+=t[ls(x)].siz;
            if(k==wz)return x;
            if(wz a[r]……a[l]
    void reversal(int l,int r)
    {
        int le=find_node(l-1);
        int ri=find_node(r+1);
        splay(le),splay(ri,le);
        t[ls(rs(root))].putrev();
    }

    //min(a[l]……a[r])
    ll get_min(int l,int r)
    {
        int le=find_node(l-1);
        int ri=find_node(r+1);
        splay(le),splay(ri,le);
        return t[ls(rs(root))].minn;
    }

    //a[l]……a[r] -> a[r-k+1]……a[r]a[l]……a[r-k]
    void revolve(int l,int r,int k)
    {
        k=(k%(r-l+1)+(r-l+1))%(r-l+1);
        if(!k)return;
        int le=find_node(l-1);
        int ri=find_node(r-k+1);
        splay(le),splay(ri,le);
        int x=ls(rs(root));
        int y=find_node(r);
        ls(rs(root))=-1;
        pushup(rs(root)),pushup(root);
        splay(y);
        int z=get_min(rs(root));
        splay(z,root);
        ls(rs(root))=x,fa(x)=rs(root);
        pushup(rs(root)),pushup(root);
    }

    void print(){dfs(root);}

    void dfs(int x)
    {
        if(x==-1)return;
        dfs(ls(x));
        printf("%d: pre:%d ls:%d rs:%d\n",x,fa(x),ls(x),rs(x));
        dfs(rs(x));
    }
}T;

题目链接:普通平衡树

splay:

#define ls(x) tree[x].lson
#define rs(x) tree[x].rson
#define fa(x) tree[x].pre
#define grafa(x) tree[tree[x].pre].pre

struct node
{
    int pre,lson,rson;
    int val,cont,siz;
    node(int pre=-1,int lson=-1,int rson=-1,
         int val=0,int cont=0,int siz=0):
        pre(pre),lson(lson),rson(rson),
        val(val),cont(cont),siz(siz) {}
} tree[100005];

int root=-1,idx=0;

void pushup(int x)
{
    tree[x].siz=tree[x].cont;
    if(~ls(x))
         tree[x].siz+=tree[ls(x)].siz;
    if(~rs(x))
         tree[x].siz+=tree[rs(x)].siz;
}

void zig(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    ls(y)=rs(x);
    if(~ls(y))fa(ls(y))=y;
    rs(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void zag(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    rs(y)=ls(x);
    if(~rs(y))fa(rs(y))=y;
    ls(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void splay(int x,int wz=-1)
{
    while(x!=wz && fa(x)!=wz)
    {
        if(grafa(x)==wz)
            if(ls(fa(x))==x)zig(x);
            else zag(x);
        else if(ls(fa(x))==x && ls(grafa(x))==fa(x))
            zig(fa(x)),zig(x);
        else if(rs(fa(x))==x && rs(grafa(x))==fa(x))
            zag(fa(x)),zag(x);
        else if(ls(fa(x))==x && rs(grafa(x))==fa(x))
            zig(x),zag(x);
        else if(rs(fa(x))==x && ls(grafa(x))==fa(x))
            zag(x),zig(x);
    }
}

int get_min(int x)
{
    while(~ls(x))x=ls(x);
    return x;
}

int get_max(int x)
{
    while(~rs(x))x=rs(x);
    return x;
}

int find_node(int val)
{
    int x=root;
    while(~x)
    {
        if(tree[x].val==val)
        {
            splay(x);
            return x;
        }
        if(valtree[x].val && (y==-1 ||
        (~y && tree[x].val>tree[y].val)))
            y=x;
        if(val<=tree[x].val)x=ls(x);
        else x=rs(x);
    }
    splay(y);
    return tree[y].val;
}

int bigger(int val)
{
    int x=root,y=-1;
    while(~x)
    {
        if(val1)
    {
        tree[x].cont--;
        tree[x].siz--;
        return;
    }
    if(ls(x)==-1 && rs(x)==-1)
        root=-1;
    else if(ls(x)==-1)
    {
        root=rs(x),fa(rs(x))=-1;
        pushup(root);
    }
    else if(rs(x)==-1)
    {
        root=ls(x),fa(ls(x))=-1;
        pushup(root);
    }
    else
    {
        int y=get_min(rs(x));
        fa(ls(x))=y,ls(y)=ls(x);
        root=rs(x),fa(rs(x))=-1;
        pushup(y),pushup(root);
        splay(y);
    }
}

int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int op,val;
        scanf("%d%d",&op,&val);
        if(op==1)insrt(val);
        if(op==2)delet(val);
        if(op==3)printf("%d\n",get_rank(val));
        if(op==4)printf("%d\n",get_kth(val));
        if(op==5)printf("%d\n",smaller(val));
        if(op==6)printf("%d\n",bigger(val));
    }
    return 0;
}

题目链接:文艺平衡树

区间翻转:

#define ls(x) tree[x].lson
#define rs(x) tree[x].rson
#define fa(x) tree[x].pre
#define grafa(x) tree[tree[x].pre].pre
 
struct node
{
    int pre,lson,rson;
    int siz,lazy;
    node(int pre=-1,int siz=0):
        pre(pre),siz(siz),
        lson(-1),rson(-1),lazy(0) {}
} tree[100005];
 
int root=-1,idx=-1;
 
void pushup(int x)
{
    tree[x].siz=(x!=0);
    if(~ls(x))tree[x].siz+=tree[ls(x)].siz;
    if(~rs(x))tree[x].siz+=tree[rs(x)].siz;
}
 
inline void zig(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    ls(y)=rs(x);
    if(~ls(y))fa(ls(y))=y;
    rs(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}
 
inline void zag(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    rs(y)=ls(x);
    if(~rs(y))fa(rs(y))=y;
    ls(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}
 
void splay(int x,int wz=-1)
{
    while(x!=wz && fa(x)!=wz)
        if(grafa(x)==wz)
            if(ls(fa(x))==x)zig(x);
            else zag(x);
        else if(ls(fa(x))==x && ls(grafa(x))==fa(x))
            zig(fa(x)),zig(x);
        else if(rs(fa(x))==x && rs(grafa(x))==fa(x))
            zag(fa(x)),zag(x);
        else if(ls(fa(x))==x && rs(grafa(x))==fa(x))
            zig(x),zag(x);
        else if(rs(fa(x))==x && ls(grafa(x))==fa(x))
            zag(x),zig(x);
}
 
void insrt(int wz)
{
    int x=root,y=-1;
    while(~x)
        if(wz

题目链接:Permutation Transformer

区间翻转/合并:

#define ls(x) tree[x].lson
#define rs(x) tree[x].rson
#define fa(x) tree[x].pre
#define grafa(x) tree[tree[x].pre].pre

struct node
{
    int pre,lson,rson;
    int siz,lazy;
    node(int pre=-1,int siz=0):
        pre(pre),siz(siz),
        lson(-1),rson(-1),lazy(0) {}
} tree[100005];

int root=-1,idx=-1;

void pushup(int x)
{
    tree[x].siz=(x!=0);
    if(~ls(x))tree[x].siz+=tree[ls(x)].siz;
    if(~rs(x))tree[x].siz+=tree[rs(x)].siz;
}

inline void zig(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    ls(y)=rs(x);
    if(~ls(y))fa(ls(y))=y;
    rs(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

inline void zag(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    rs(y)=ls(x);
    if(~rs(y))fa(rs(y))=y;
    ls(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void splay(int x,int wz=-1)
{
    while(x!=wz && fa(x)!=wz)
        if(grafa(x)==wz)
            if(ls(fa(x))==x)zig(x);
            else zag(x);
        else if(ls(fa(x))==x && ls(grafa(x))==fa(x))
            zig(fa(x)),zig(x);
        else if(rs(fa(x))==x && rs(grafa(x))==fa(x))
            zag(fa(x)),zag(x);
        else if(ls(fa(x))==x && rs(grafa(x))==fa(x))
            zig(x),zag(x);
        else if(rs(fa(x))==x && ls(grafa(x))==fa(x))
            zag(x),zig(x);
}

void insrt(int wz)
{
    int x=root,y=-1;
    while(~x)
        if(wz

题目链接:Play with Chain

区间翻转/合并:

#define ls(x) tree[x].lson
#define rs(x) tree[x].rson
#define fa(x) tree[x].pre
#define grafa(x) tree[tree[x].pre].pre

struct node
{
    int pre,lson,rson;
    int siz,lazy;
    node(int pre=-1,int siz=0):
        pre(pre),siz(siz),
        lson(-1),rson(-1),lazy(0) {}
} tree[300005];

int root=-1,idx=-1;

void pushup(int x)
{
    tree[x].siz=(x!=0);
    if(~ls(x))tree[x].siz+=tree[ls(x)].siz;
    if(~rs(x))tree[x].siz+=tree[rs(x)].siz;
}

inline void zig(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    ls(y)=rs(x);
    if(~ls(y))fa(ls(y))=y;
    rs(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

inline void zag(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    rs(y)=ls(x);
    if(~rs(y))fa(rs(y))=y;
    ls(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void splay(int x,int wz=-1)
{
    while(x!=wz && fa(x)!=wz)
        if(grafa(x)==wz)
            if(ls(fa(x))==x)zig(x);
            else zag(x);
        else if(ls(fa(x))==x && ls(grafa(x))==fa(x))
            zig(fa(x)),zig(x);
        else if(rs(fa(x))==x && rs(grafa(x))==fa(x))
            zag(fa(x)),zag(x);
        else if(ls(fa(x))==x && rs(grafa(x))==fa(x))
            zig(x),zag(x);
        else if(rs(fa(x))==x && ls(grafa(x))==fa(x))
            zag(x),zig(x);
}

void insrt(int val)
{
    int x=root,y=-1;
    while(~x)
        if(val

题目链接:Splay

区间删除:

#define ls(x) tree[x].lson
#define rs(x) tree[x].rson
#define fa(x) tree[x].pre
#define grafa(x) tree[tree[x].pre].pre

struct node
{
    int pre,lson,rson;
    int val,cont,siz;
    node(int pre=-1,int lson=-1,int rson=-1,
         int val=0,int cont=0,int siz=0):
        pre(pre),lson(lson),rson(rson),
        val(val),cont(cont),siz(siz) {}
} tree[200005];

int root=-1,idx=-1;

void pushup(int x)
{
    tree[x].siz=tree[x].cont;
    if(!x)tree[x].siz=0;
    if(~ls(x))
         tree[x].siz+=tree[ls(x)].siz;
    if(~rs(x))
         tree[x].siz+=tree[rs(x)].siz;
}

void zig(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    ls(y)=rs(x);
    if(~ls(y))fa(ls(y))=y;
    rs(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void zag(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    rs(y)=ls(x);
    if(~rs(y))fa(rs(y))=y;
    ls(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void splay(int x,int wz=-1)
{
    while(x!=wz && fa(x)!=wz)
    {
        if(grafa(x)==wz)
            if(ls(fa(x))==x)zig(x);
            else zag(x);
        else if(ls(fa(x))==x && ls(grafa(x))==fa(x))
            zig(fa(x)),zig(x);
        else if(rs(fa(x))==x && rs(grafa(x))==fa(x))
            zag(fa(x)),zag(x);
        else if(ls(fa(x))==x && rs(grafa(x))==fa(x))
            zig(x),zag(x);
        else if(rs(fa(x))==x && ls(grafa(x))==fa(x))
            zag(x),zig(x);
    }
}

int get_min(int x)
{
    while(~ls(x))x=ls(x);
    return x;
}

int get_max(int x)
{
    while(~rs(x))x=rs(x);
    return x;
}

int find_node(int val)
{
    int x=root;
    while(~x)
    {
        if(tree[x].val==val)
        {
            splay(x);
            return x;
        }
        if(valtree[y].val)))
            y=x;
        if(val<=tree[x].val)x=ls(x);
        else x=rs(x);
    }
    splay(y);
    return tree[y].val;
}

int smaller(int val,int wz=-1)
{
    int x=root,y=-1;
    while(~x)
    {
        if(tree[x].valtree[y].val)))
            y=x;
        if(val<=tree[x].val)x=ls(x);
        else x=rs(x);
    }
    splay(y,wz);
    return y;
}

int bigger(int val,int wz=-1)
{
    int x=root,y=-1;
    while(~x)
    {
        if(tree[x].val>val && (y==-1 ||
        (~y && tree[x].val

题目链接:Splay

区间加/删除 询问区间和:

#define ls(x) tree[x].lson
#define rs(x) tree[x].rson
#define fa(x) tree[x].pre
#define grafa(x) tree[tree[x].pre].pre

struct node
{
    int pre,lson,rson,id,siz;
    ll val,sum,lazy;
    node(int a=-1,int b=-1,int c=-1,int d=0,
         int e=0,ll f=0,ll g=0,ll h=0):
        pre(a),lson(b),rson(c),id(d),
        siz(e),val(f),sum(g),lazy(h) {}
} tree[200005];

int root=-1,idx=-1;

void pushup(int x)
{
    tree[x].sum=tree[x].val;
    tree[x].siz=(x!=0);
    if(~ls(x))
    {
        tree[x].sum+=tree[ls(x)].sum;
        tree[x].siz+=tree[ls(x)].siz;
    }
    if(~rs(x))
    {
        tree[x].sum+=tree[rs(x)].sum;
        tree[x].siz+=tree[rs(x)].siz;
    }
}

void zig(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    ls(y)=rs(x);
    if(~ls(y))fa(ls(y))=y;
    rs(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void zag(int x)
{
    int y=fa(x);
    if(~fa(y))
        if(ls(fa(y))==y)
            ls(fa(y))=x;
        else rs(fa(y))=x;
    fa(x)=fa(y),fa(y)=x;
    rs(y)=ls(x);
    if(~rs(y))fa(rs(y))=y;
    ls(x)=y;
    if(fa(x)==-1)root=x;
    pushup(y),pushup(x);
}

void splay(int x,int wz=-1)
{
    while(x!=wz && fa(x)!=wz)
        if(grafa(x)==wz)
            if(ls(fa(x))==x)zig(x);
            else zag(x);
        else if(ls(fa(x))==x && ls(grafa(x))==fa(x))
            zig(fa(x)),zig(x);
        else if(rs(fa(x))==x && rs(grafa(x))==fa(x))
            zag(fa(x)),zag(x);
        else if(ls(fa(x))==x && rs(grafa(x))==fa(x))
            zig(x),zag(x);
        else if(rs(fa(x))==x && ls(grafa(x))==fa(x))
            zag(x),zig(x);
}

inline void pushdown(int id)
{
    if(!tree[id].lazy)return;
    ll add=tree[id].lazy;
    if(~ls(id))
    {
        tree[ls(id)].val+=add;
        tree[ls(id)].lazy+=add;
        tree[ls(id)].sum+=tree[ls(id)].siz*add;
    }
    if(~rs(id))
    {
        tree[rs(id)].val+=add;
        tree[rs(id)].lazy+=add;
        tree[rs(id)].sum+=tree[rs(id)].siz*add;
    }
    tree[id].lazy=0;
    pushup(id);
}

void insrt(int id,ll val)
{
    int x=root,y=-1;
    while(~x)
    {
        pushdown(y=x);
        if(idtree[y].id)))
            y=x;
        if(id<=tree[x].id)x=ls(x);
        else x=rs(x);
    }
    splay(y,wz);
    return y;
}

int bigger(int id,int wz=-1)
{
    int x=root,y=-1;
    while(~x)
    {
        pushdown(x);
        if(tree[x].id>=id && (y==-1 ||
        (~y && tree[x].id

你可能感兴趣的:(Splay)