跳到正文

个人记录

Splay(伸展树)

Refined_heart约 24 分钟
原文
文章信息与版本

文章信息

发布
2018年12月23日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 24 分钟
获赞
11

版本

  1. v1当前
C++
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> 
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,all;
int root,tot;
inline int read(){
    register int x=0,t=1;
    register char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-'){t=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*t;
}
struct SPLAY{
    struct tree{
        int son[2];//1 right 2 left
        int val,siz;// val 权值 siz 长度 
        int fa,numb;//排名,父节点 
    }tr[400000];
    inline int chk(int x,int y){//看看是左孩子还是右孩子 
        return tr[x].val>tr[y].val;
    }
    inline void pushup(int x){//维护树状结构 
        tr[x].numb=tr[tr[x].son[1]].numb+tr[tr[x].son[0]].numb+tr[x].siz;
        return;
    }//新节点的编号是左孩子加右孩子再加它本身的长度 
    inline void rotate(int x){//旋转操作 
        int y=tr[x].fa,z=tr[y].fa;
        int hh=chk(x,y);
        tr[x].fa=z;tr[z].son[chk(y,z)]=x;
        tr[y].son[hh]=tr[x].son[hh^1];
        tr[tr[x].son[hh^1]].fa=y;
        tr[x].son[hh^1]=y;tr[y].fa=x;
        pushup(y);pushup(x);
        return;
    }
    inline void splay(int now,int will){//将now旋转到will 
        while(tr[now].fa!=will){
            int x=tr[now].fa,y=tr[x].fa;
            if(y!=will){
                if(chk(now,x)==chk(x,y)) rotate(x);
                else rotate(now);
            }
            rotate(now);
        }
        if(!will) root=now;
        return;
    }
    inline void build(int x){//相当于insert,插入 
        int ls=root,f=0;all++;
        while(ls&&tr[ls].val!=x){
            f=ls;
            ls=tr[ls].son[x>tr[ls].val];
        }
        if(tr[ls].val==x) tr[ls].siz++;
        else{
            ls=++tot;
            if(f!=0) tr[f].son[x>tr[f].val]=ls;
            tr[ls].son[0]=tr[ls].son[1]=0;
            tr[ls].siz=1;tr[ls].numb=1;
            tr[ls].val=x;tr[ls].fa=f;
        }
        splay(ls,0);
        return;
    }
    inline void findx(int x){ //找x的值 
        int ls=root;
        if(!ls) return;
        while(tr[ls].son[x>tr[ls].val]&&x!=tr[ls].val) ls=tr[ls].son[x>tr[ls].val];
        splay(ls,0);
        return;
    }
    inline int rankx(int x){//x的排名 
        findx(x);
        if(tr[root].val<x) return tr[tr[root].son[0]].numb+tr[root].siz;
        return tr[tr[root].son[0]].numb;
    }
    inline int xth(int x){//第x小的数 
        int ls=root;
        if(x>all) return 0;
        while(1){
            int mid=tr[tr[ls].son[0]].numb+tr[ls].siz;
            if(x<=tr[tr[ls].son[0]].numb) ls=tr[ls].son[0];
            else if(x>mid){
                x-=mid;
                ls=tr[ls].son[1];
            }
            else return tr[ls].val;
        }
    }
    inline int pre(int x){//找前继结点 
        findx(x);
        if(tr[root].val<x) return root;
        int to=tr[root].son[0];
        while(tr[to].son[1]) to=tr[to].son[1];
        return to;
    }
    inline int succ(int x){//找后继结点 
        findx(x);
        if(tr[root].val>x) return root;
        int to=tr[root].son[1];
        while(tr[to].son[0]) to=tr[to].son[0];
        return to;
    }
    inline void pop(int x){//删除操作 
        int ff=pre(x),rs=succ(x);
        splay(ff,0);splay(rs,ff);
        int to=tr[rs].son[0];
        if(tr[to].siz>1){
            tr[to].siz--;
            splay(to,0);
        }
        else tr[rs].son[0]=0;
        return;
    }
}play;
int main(){
    play.build(2147483647);play.build(-2147483647);//初始化 
    int opt,x;
    n=read();
    while(n--){
        opt=read();x=read();
        if(opt==1) play.build(x);//insert 
        if(opt==2) play.pop(x);//pop
        if(opt==3) printf("%d\n",play.rankx(x));//rank
        if(opt==4) printf("%d\n",play.xth(x+1));//find rank(x)'s val
        if(opt==5) printf("%d\n",play.tr[play.pre(x)].val);//inforntof 
        if(opt==6) printf("%d\n",play.tr[play.succ(x)].val);//behind
    }
    return 0;
}

这是正确的模板,感谢@Xiao_Mi

Splay的讲解以后补上……

再加一个模板,感谢@千年之狐_天才

C++
#include<cstdio>
#include<iostream>
using namespace std;
#define inf (1<<30)
int root,n,m;
struct node{
	int siz,son[2],fa,tag;
}tr[200000];
#define lc tr[x].son[0]
#define rc tr[x].son[1]
inline void pushup(int x){
	tr[x].siz=tr[lc].siz+tr[rc].siz+1;
}
inline void pushdown(int x){
	if(tr[x].tag){
		tr[lc].tag^=1;
		tr[rc].tag^=1;
		tr[x].tag=0;
		swap(lc,rc);
	}
}
/*inline void rotate(int x){
	int y=tr[x].fa,z=tr[y].fa;
	int s=tr[y].son[1]==x;
	tr[x].fa=z;
	tr[z].son[check(y,z)]=x;
	tr[y].son[s]=tr[x].son[s^1];
	tr[tr[x].son[s^1]].fa=y;
	tr[x].son[s^1]=y;
	tr[y].fa=x;
	pushup(y);
	pushup(x);
}
inline void splay(int u,int goal){
	while(tr[u].fa!=goal){
		int x=tr[u].fa,y=tr[x].fa;
		if(y!=goal){
			pushdown(y);
		}pushdown(x);
		pushdown(u);
		if(y!=goal){
			if(check(u,x)==check(x,y))rotate(x);
			else rotate(u);
		}
		rotate(u);
	}if(!goal)root=u;
}*/
inline void rotate(int x){
	int y=tr[x].fa;
	int z=tr[y].fa;
	int k=tr[y].son[1]==x;
	tr[z].son[tr[z].son[1]==y]=x;
	tr[x].fa=z;
	tr[y].son[k]=tr[x].son[k^1];
	tr[tr[x].son[k^1]].fa=y;
	tr[x].son[k^1]=y;
	tr[y].fa=x;
	pushup(y);pushup(x);
}
inline void splay(int x,int goal){
	while(tr[x].fa!=goal){
		int y=tr[x].fa,z=tr[y].fa;
		if(z!=goal)pushdown(z);
		pushdown(y);
		pushdown(x);
		if(z!=goal){
			(tr[z].son[0]==y)^(tr[y].son[0]==x)?rotate(x):rotate(y);
		}
		rotate(x);
	}
	if(!goal)root=x;
}
inline void insert(int x){
	/*int x=root,fa=0;
	if(!root){
		root=++id;
		tr[id].val=val;
		tr[id].siz=tr[id].num=1;
		return;
	}while(x){
		fa=x;
		if(val>tr[x].val)x=rc;
		else if(val==tr[x].val){
			splay(x,0);
			tr[x].num++;
			tr[x].siz++;
			return;
		}else x=lc;
	}id++;
	tr[id].val=val;
	tr[id].fa=fa;
	tr[fa].son[val>tr[fa].val]=id;
	tr[id].num=tr[id].siz=1;
	splay(id,0);*/
	int u=root,fa=0;
	while(u){
		fa=u;
		u=tr[u].son[x>u];
	}if(fa)tr[fa].son[x>fa]=x;
	tr[x].son[0]=tr[x].son[1]=0;
	tr[x].fa=fa;
	tr[x].siz=1;
	splay(x,0);
}
/*inline int pre(int val){
	int x=root,ans=-inf;
	while(x){
		if(tr[x].val<val){
			ans=max(ans,tr[x].val);
			x=rc;
		}else{
			x=lc;
		}
	}return ans;
}
inline int last(int val){
	int x=root,ans=inf;
	while(x){
		if(val>tr[x].val)x=rc;
		else{
			ans=min(tr[x].val,ans);
			x=lc;
		}
	}
}
inline void del(int val){
	int x=root;
	while(x){
		if(val>tr[x].val)x=rc;
		else if(val==tr[x].val){
			splay(x,0);
			tr[x].num--;
			if(!tr[x].num){
				int u=tr[x].son[0];
				while(tr[u].son[1])u=tr[u].son[1];
				splay(u,x);
				root=u;
				tr[u].son[1]=tr[x].son[1];
				tr[u].fa=0;
				tr[tr[x].son[1]].fa=u;
				tr[x].son[0]=tr[x].son[1]=0;
				pushup(u);
				return;
			}
		}else x=lc;
	}
}
inline int get_rk(int val){
	int x=root,ans=0;
	while(x){
		if(val<tr[x].val)x=lc;
		else{
			ans+=tr[lc].siz;
			if(val==tr[x].val){
				splay(x,0);
				return ans;
			}else ans+=tr[x].num,x=rc;
		}
	}return -1;
}
inline int rk_x(int k){
	int x=root;
	while(x){
		if(tr[lc].siz>=k)x=lc;
		else{
			k-=tr[lc].siz+tr[x].num;
			if(k<=0){
				splay(x,0);
				return tr[x].val;
			}x=rc;
		}
	}return -1;
}*/
inline int kth(int k){
	int u=root;
	while(727){
		pushdown(u);
		int g=tr[u].son[0];
		if(tr[g].siz>=k)u=g;
		else{
			k-=tr[g].siz+1;
			if(!k)return u;
			u=tr[u].son[1];
		}
	}
}
inline void change(int x,int y){
	int ll=kth(x),rl=kth(y+2);
	splay(ll,0);
	splay(rl,ll);
	tr[tr[rl].son[0]].tag^=1;
}
inline void write(int x){
	pushdown(x);
	if(lc)write(lc);
	if(x<=n+1&&x>1)printf("%d ",x-1);
	if(rc)write(rc);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n+2;++i)insert(i);
	while(m--){
		int l,r;
		scanf("%d%d",&l,&r);
		change(l,r);
	}write(root);
	return 0;
}

感谢@千年之狐_天才 添加指针splaysplay模板一个~

C++
#include<cstdio>//181ms O2
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
const int inf=(1<<30);
struct node{
	node *ch[2],*fa;
	int siz,v,num;
}*rt,*null;
inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+(ch^48);ch=getchar();}return s*w;}
inline void pushup(node *x){x->siz=x->ch[0]->siz+x->ch[1]->siz+x->num;}
inline void rotate(node *x){
	node *y=x->fa,*z=y->fa;int k=y->ch[1]==x;
	z->ch[z->ch[1]==y]=x;x->fa=z;
	y->ch[k]=x->ch[k^1];x->ch[k^1]->fa=y;
	x->ch[k^1]=y;y->fa=x;pushup(y),pushup(x);
}inline void splay(node *x,node *goal){
	node *y,*z;
	while(x->fa!=goal){
		y=x->fa;z=y->fa;
		if(z!=goal)(z->ch[1]==y)^(y->ch[1]==x)?rotate(x):rotate(y);
		rotate(x);
	}if(goal==null)rt=x;
}inline node* new_(){node *x=new node;x->ch[0]=x->ch[1]=x->fa=null;x->siz=x->num=1;return x;}
inline void Insert(int v){
	node *x=rt;
	if(x==null){rt=new_();rt->v=v;return;}
	node *fa=null;
	while(x!=null){
		fa=x;if(v!=x->v)x=x->ch[v>=x->v];
		else{splay(x,null);x->siz++;x->num++;return;}
	}x=new_();fa->ch[v>=fa->v]=x;
	x->fa=fa;x->v=v;
	splay(x,null);
}inline void del(node *x){
	splay(x,null);x->siz--;x->num--;int k;
	if(x->num)return;
	if(x->ch[0]!=null)k=0;
	else if(x->ch[1]!=null)k=1;
	else{rt=null;delete(x);return;}
	node *y=x->ch[k];
	while(y->ch[k^1]!=null)y=y->ch[k^1];
	splay(y,x);y->ch[k^1]=x->ch[k^1];
	x->ch[k^1]->fa=y;y->fa=null;rt=y;
	delete(x);
}inline void Del(int v){
	node *x=rt;
	while(x!=null){
		if(v!=x->v)x=x->ch[v>=x->v];
		else{del(x);return;}
	}
}inline int rank(int v){
	node *x=rt;
	while(x!=null){
		if(v>x->v)x=x->ch[1];
		else if(v<x->v)x=x->ch[0];
		else{splay(x,null);return x->ch[0]->siz+1;}
	}return 0;
}inline int kth(int k){
	node *x=rt;int c;
	while(x!=null){c=x->ch[0]->siz;
		if(c>=k)x=x->ch[0];
		else{
			k-=c+x->num;
			if(k<=0){splay(x,null);return x->v;}
			x=x->ch[1];
		}
	}return 0;
}inline int min_(int a,int b){return (a<b?a:b);}
inline int max_(int a,int b){return (a>b?a:b);}
inline int pre(int v){
	node *x=rt;int ans=-inf;
	while(x!=null){
		if(x->v<v){ans=max_(ans,x->v);x=x->ch[1];}
		else x=x->ch[0];
	}return ans;
}
inline int last(int v){
	node *x=rt;int ans=inf;
	while(x!=null){
		if(x->v>v){ans=min_(ans,x->v);x=x->ch[0];}
		else x=x->ch[1];
	}return ans;
}
int n,opt,x;
int main(){
	null=new node ;null->siz=0;rt=null;n=read();
	for(register int i=1;i<=n;i++){
		opt=read(),x=read();
		if(opt==1)Insert(x);
		else if(opt==2)Del(x);
		else if(opt==3)printf("%d\n",rank(x));
		else if(opt==4)printf("%d\n",kth(x));
		else if(opt==5)printf("%d\n",pre(x));
		else printf("%d\n",last(x));
	}
	return 0;
}

以下是对splaysplay的大体总结……

作为一种常(du)(du)用(liu)(liu)的数据结构,它的核心代码不多甚至可以说很少。实际上它只是为了解决一个问题:当TreeTree是一条链的时候(万恶的出题人)我们为了维护查询的时间复杂度,需要一个操作,来改变这duliuduliu的情况,于是,我们的rotaterotate操作横空出世!

它的目的是throughthrough旋转,使得树中的结点大小关系不变,而结构更加平衡。也就是说,通过这个操作,我们还是可以将万恶的出题人卡你的O(n)O(n)复杂度卡回去(卡出题人总是心情舒畅……

但是,我们发现,事情并非那么简单。如果你只旋转一次,那么有可能会发生旋转完依旧存在链的情况。于是,splaysplay操作顺势而生。它们合起来操作,就可以避免此情况的发生。

我们在研究rotaterotate操作之后,发现,如果当前点和目标点位于一条线上时,我们应该先rotaterotate当前点的fatherfather,然后在rotaterotate当前点。否则,一直旋转当前点就好了。最后再特判一下,完成!

最后简单说一下,在反转区间的时候,用一个标记来记录翻转区间,然后在insertinsert的时候要+2+2,“[l,r][l,r]则将l−1l-1旋转到根,r+1r+1旋转到根的右节点 所以这里用到[0,n+1][0,n+1],全部加1所以是[1,n+2][1,n+2] 那么就是l旋转到根,r+2旋转到根的根节点”by−−wjrby--wjr

那我们每次操作的时候下传一下标记,最后特别写一个writewrite函数来输出就好了。

下面给出write,rotate,splay,pushdown′sCode:write,rotate,splay,pushdown's Code:

C++
inline void pushdown(int x){//区间翻转,左右节点互换即可,并将标记下沉 
	if(t[x].lazy){
		t[t[x].ch[0]].lazy^=1;
		t[t[x].ch[1]].lazy^=1;
		t[x].lazy=0;
		swap(t[x].ch[0],t[x].ch[1]);
	}
}
inline void rotate(int x){
	int y=t[x].fa;
	int z=t[y].fa;
	int k=t[y].ch[1]==x;
	t[z].ch[t[z].ch[1]==y]=x;
	t[x].fa=z;
	t[y].ch[k]=t[x].ch[k^1];
	t[t[x].ch[k^1]].fa=y;
	t[x].ch[k^1]=y;
	t[y].fa=x;
	pushup(y);pushup(x);
}
inline void splay(int x,int goal){
	while(t[x].fa!=goal){
		int y=t[x].fa,z=t[y].fa;
		if(z!=goal)pushdown(z);
		pushdown(y);
		pushdown(x);
		if(z!=goal){
			(t[z].ch[0]==y)^(t[y].ch[0]==x)?rotate(x):rotate(y);
		}
		rotate(x);
	}
	if(!goal)root=x;
}
inline void write(int x){
	pushdown(x);
	if(t[x].ch[0])write(t[x].ch[0]);
	if(x<=n+1&&x>1)printf("%d ",x-1);
	if(t[x].ch[1])write(t[x].ch[1]);
}

这些是文艺平衡树的代码,最上面一个是普通平衡树的代码。

至此。

LookLook up up to to thethe skysky laughlaugh gogo out,myout,my generationgeneration isis notnot thethe artemisiaartemisia people.people.

——仰天大笑出门去,我辈岂是蓬蒿人。

原文
存档评论6 条抓取于