Splay学习笔记

· · 个人记录

简介

Splay Tree(伸展树/自适应查找树)是一种自平衡二叉排序树,可以在均摊O(log_2n)的时间内完成基于 Splay(伸展)操作的修改与查询。
严格上来说,Splay树并不算是一棵高度平衡的树,但是它通过伸展操作(Splay操作)来保持结构的高效性。
"Splay"是将一个特定的点转到特定的位置
一般用Splay(x,T)代表将元素x旋转到Splay树T的根结点处。
Splay操作的实现还是基于旋转。
不过这个旋转不是为了使树变得平衡,而是为了改变节点的深度。
同时,Splay具有“自适应性”
大概就是说Splay会根据操作的特点调整树结构,使得操作尽可能高效
可以去了解了解Splay的动态最优性猜想,是个著名的open problem。

优缺点

缺点

优点

代码实现

维护名次树

#include <bits/stdc++.h>
using namespace std;

inline int read()
{
    int x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int MAXN = 300005;
const int INF = 0x3f3f3f3f;
int ch[MAXN][2],fa[MAXN],val[MAXN];
int cnt[MAXN],size[MAXN];
int n,sz,root;

inline void pushup(int x)
    {size[x]=size[ch[x][0]]+size[ch[x][1]]+cnt[x];}

inline void rotate(int x)
{
    int y=fa[x],z=fa[y];
    int k=ch[y][1]==x,w=ch[x][k^1];
    fa[x]=z,fa[y]=x,fa[w]=y;
    ch[x][k^1]=y,ch[y][k]=w;
    ch[z][ch[z][1]==y]=x;
    pushup(y); pushup(x);
}

inline void splay(int x,int t)
{
    while(fa[x]!=t)
    {
        int y=fa[x],z=fa[y];
        if(z!=t)
            rotate(ch[y][1]==x^ch[z][1]==y ? x : y);
        rotate(x);
    }
    if(t==0) root=x;
}

inline void insert(int x)
{
    int p=root,pre=0;
    while(val[p]!=x && p)
        pre=p,p=ch[p][val[p]<x];
    if(p) cnt[p]++;
    else
    {
        p=++sz; cnt[p]=size[p]=1;
        if(pre) ch[pre][val[pre]<x]=p;
        ch[p][0]=ch[p][1]=0;
        fa[p]=pre; val[p]=x;
    }
    splay(p,0);
}

inline void find(int x)
{
    int p=root;
    while(ch[p][val[p]<x] && val[p]!=x)
        p=ch[p][val[p]<x];
    splay(p,0);
}

inline int query_rnk(int x)
    {find(x); return size[ch[root][0]];}

inline int query_num(int x)
{
    int p=root; x++;
    while(true)
    {
        if(ch[p][0] && x<=size[ch[p][0]])
            p=ch[p][0];
        else if(x>size[ch[p][0]]+cnt[p])
            x-=size[ch[p][0]]+cnt[p],p=ch[p][1];
        else return val[p];
    }
}

inline int query_pre(int x)
{
    find(x);
    if(val[root]<x) return root;
    int p=ch[root][0];
    while(ch[p][1]) p=ch[p][1];
    return p;
}

inline int query_nxt(int x)
{
    find(x);
    if(val[root]>x) return root;
    int p=ch[root][1];
    while(ch[p][0]) p=ch[p][0];
    return p;
}

inline void del(int x)
{
    int pre=query_pre(x),nxt=query_nxt(x);
    splay(pre,0); splay(nxt,root);
    int p=ch[nxt][0];
    if(cnt[p]>1) cnt[p]--,splay(p,0);
    else ch[nxt][0]=0,pushup(nxt),pushup(root);
}

int main(int argc, char const *argv[])
{
    int opt,x; n=read();
    insert(INF),insert(-INF);
    for(int i = 1; i <= n; ++i)
    {
        opt=read(),x=read();
        if(opt==1) insert(x);
        if(opt==2) del(x);
        if(opt==3) printf("%d\n",query_rnk(x));
        if(opt==4) printf("%d\n",query_num(x));
        if(opt==5) printf("%d\n",val[query_pre(x)]);
        if(opt==6) printf("%d\n",val[query_nxt(x)]);
    }
    return 0;
}

维护序列反转

#include <bits/stdc++.h>
using namespace std;

inline int read()
{
    int x=0; int f=1; char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

const int MAXN = 100005;
int fa[MAXN],ch[MAXN][2],id[MAXN],size[MAXN];
bool rev[MAXN];
int n,m,sz,root;

inline void pushup(int p)
{
    int l=ch[p][0],r=ch[p][1];
    size[p]=size[l]+size[r]+1;
}

inline void pushdown(int p)
{
    int l=ch[p][0],r=ch[p][1];
    if(rev[p])
    {
        swap(ch[p][0],ch[p][1]);
        rev[l]^=1; rev[r]^=1;
        rev[p]=0;
    }
}

inline void rotate(int x,int &k)
{
    int y=fa[x],z=fa[y],l,r;
    if(ch[y][0]==x) l=0;
    else l=1; r=l^1;
    if(y==k) k=x;
    else
    {
        if(ch[z][0]==y) ch[z][0]=x;
        else ch[z][1]=x;
    }
    fa[x]=z; fa[y]=x;
    fa[ch[x][r]]=y;
    ch[y][l]=ch[x][r]; ch[x][r]=y;
    pushup(y); pushup(x);
}

inline void splay(int x,int &k)
{
    while(x!=k)
    {
        int y=fa[x],z=fa[y];
        if(y!=k)
        {
            if(ch[y][0]==x^ch[z][0]==y)
                rotate(x,k);
            else rotate(y,k);
        }
        rotate(x,k);
    }
}

int find(int k,int rank)
{
    pushdown(k);
    int l=ch[k][0],r=ch[k][1];
    if(size[l]+1==rank) return k;
    else if(size[l]>=rank)  return find(l,rank);
    else  return find(r,rank-size[l]-1);
}

void rever(int l,int r)
{
    int x=find(root,l),y=find(root,r+2);
    splay(x,root); splay(y,ch[x][1]);
    int z=ch[y][0]; rev[z]^=1;
}

void build(int l,int r,int f)
{
    if(l>r) return;
    int cur=id[l],last=id[f];
    if(l==r)
    {
        fa[cur]=last; size[cur]=1;
        if(l<f)ch[last][0]=cur;
        else ch[last][1]=cur;
        return;
    }
    int mid=(l+r)>>1;
    cur=id[mid];
    build(l,mid-1,mid);
    build(mid+1,r,mid);
    fa[cur]=last; pushup(mid);
    if(mid<f) ch[last][0]=cur;
    else ch[last][1]=cur;
}

int main(int argc, char const *argv[])
{
    n=read(); m=read();
    for(int i=1; i<=n+2; i++)
        id[i]=++sz;
    build(1,n+2,0);
    root=n+3>>1;
    for(int i=1; i<=m; i++)
    {
        int l=read(),r=read();
        rever(l,r);
    }
    for(int i=2; i<=n+1; i++)
        printf("%d ",find(root,i)-1);
    return 0;
}