跳到正文

个人记录

[遗产]-不需要的知识点

Zofia约 39 分钟
原文
文章信息与版本

文章信息

发布
2019年11月17日
收录
2026年8月3日
更新
2026年11月14日
篇幅
约 39 分钟

版本

  1. v1当前

不需要看的知识点

不深究!

Splay

关于伸展树(Splay),它死了

普通平衡树

C++
#include<bits/stdc++.h>
#define root tree[0].ch[1]
using namespace std;

int n;
struct node{
    int v,fa,rec,size;
    int ch[2];
}tree[1000001];
int ni;

inline void Connect(int id,int fid,int son){
    tree[fid].ch[son]=id;
    tree[id].fa=fid;
}

inline void update(int p){
    tree[p].size=tree[tree[p].ch[0]].size+tree[tree[p].ch[1]].size+tree[p].rec;
}

inline void Rotate(int x){
    int y=tree[x].fa,z=tree[y].fa;
    int p,q;
    if(x==tree[y].ch[1]){p=1;}
    else{p=0;}
    if(y==tree[z].ch[1]){q=1;}
    else{q=0;}
    Connect(tree[x].ch[p^1],y,p);
    Connect(y,x,p^1);
    Connect(x,z,q);update(y);update(x);
}

inline void Splay(int x,int to){
    int father=tree[to].fa;
    while(tree[x].fa!=father){
        int y=tree[x].fa,z=tree[y].fa;
        int p,q;
        if(x==tree[y].ch[1]){p=1;}
        else{p=0;}
        if(y==tree[z].ch[1]){q=1;}
        else{q=0;}
        if(to==y){Rotate(x);}//到任意点to;
        else if(p==q){
            Rotate(y);Rotate(x);
        }else{
            Rotate(x);Rotate(x);
        }
    }
}

inline int find(int v){
    int p=root;
    while(p!=0){
        if(tree[p].v==v){Splay(p,root);return p;}
        if(tree[p].v<v){p=tree[p].ch[1];}
        else{p=tree[p].ch[0];}
    }return 0;
}

inline void Delet(int v){
    int id=find(v);if(id==0){return;}
    if(tree[id].rec>1){tree[id].rec--;tree[id].size--;}
    else if(id!=0){
        if(tree[id].ch[0]==0&&tree[id].ch[1]==0){
            root=0;
        }else if(tree[id].ch[0]&&tree[id].ch[1]==0){
            Connect(tree[id].ch[0],0,1);
        }else if(tree[id].ch[1]&&tree[id].ch[0]==0){
            Connect(tree[id].ch[1],0,1);
        }else{
            int maxson=tree[id].ch[0];
            while(tree[maxson].ch[1]!=0){
                tree[maxson].size--;
                maxson=tree[maxson].ch[1];
            }Splay(maxson,root);
            Connect(tree[id].ch[1],maxson,1);
            Connect(maxson,0,1);//接到根上;
            update(maxson);
        }
    }
}

inline void addnode(int fa,int v){
    tree[++ni].v=v;
    tree[ni].fa=fa;
    tree[ni].rec++;
    tree[ni].size++;
}

inline void Build(int v){
    int p=root,nodeid;
    if(root==0){addnode(0,v);root=ni;return;}
    while(p!=0){
        tree[p].size++;
        if(v==tree[p].v){nodeid=p;tree[p].rec++;break;}
        if(v<tree[p].v){
            if(tree[p].ch[0]==0){
                addnode(p,v);nodeid=tree[p].ch[0]=ni;break;
            }else{p=tree[p].ch[0];}
        }else if(v>tree[p].v){
            if(tree[p].ch[1]==0){
                addnode(p,v);nodeid=tree[p].ch[1]=ni;break;
            }else{p=tree[p].ch[1];}
        }
    }Splay(nodeid,root);
}

inline int Levelquery(int v){
    int id=find(v);
    return tree[tree[id].ch[0]].size+1;
}

inline int Valquery(int i){
    int p=root;
    while(p!=0){
        int size=tree[tree[p].ch[0]].size;
        if(size<i&&size+tree[p].rec>=i){
            Splay(p,root);return tree[p].v;
        }else if(size+tree[p].rec<i){
            i-=size+tree[p].rec;p=tree[p].ch[1];
        }else{
            p=tree[p].ch[0];
        }
    }return 0;
}

inline int Frontsearch(int v){
    int p=root,re=0x80000000;
    while(p!=0){
        if(tree[p].v>=v){
            p=tree[p].ch[0];
        }else{
            re=max(re,tree[p].v);
            p=tree[p].ch[1];
        }
    }return re;
}

inline int Backsearch(int v){
    int p=root,re=0x7fffffff;
    while(p!=0){
        if(tree[p].v>v){
            re=min(re,tree[p].v);
            p=tree[p].ch[0];
        }else{
            p=tree[p].ch[1];
        }
    }return re;
}

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){f=-1;}ch=getchar();
    }while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
    }return x*f;
}

int main(){
    n=read();
    for(int i=1;i<=n;i++){
        int f=read(),x=read();
        if(f==1){Build(x);}
        else if(f==2){Delet(x);}
        else if(f==3){printf("%d\n",Levelquery(x));}
        else if(f==4){printf("%d\n",Valquery(x));}
        else if(f==5){printf("%d\n",Frontsearch(x));}
        else{printf("%d\n",Backsearch(x));}
    }return 0;
}

文艺平衡树

区间反转:下标满足二叉搜索树性质

树上节点存的是下标

第i位就是第i名

原理:反转[i,j],就把i-1提到根,把j+1提到根的右儿子

为了提高效率,我们给根的右儿子的左儿子打上标记

1558883150_1_.png

微信截图_20190526230709.png

微信截图_20190526230838.png

微信截图_20190526230947.png

微信截图_20190526231039.png

微信截图_20190526231126.png

微信截图_20190526231213.png

微信截图_20190526231256.png

微信截图_20190526231340.png

微信截图_20190526231416.png

微信截图_20190526231449.png

微信截图_20190526231526.png

微信截图_20190526231558.png

C++
#include<bits/stdc++.h>
#define root tree[0].ch[1]
using namespace std;

int n,m;
struct node{
    int v,fa,rec,size,c;
    int ch[2];
}tree[1000001];
int ni;

inline void Pushdown(int p){
    if(tree[p].c){
        tree[tree[p].ch[0]].c^=1;
        tree[tree[p].ch[1]].c^=1;
        swap(tree[p].ch[0],tree[p].ch[1]);
        tree[p].c=0;
    }
}

inline void Connect(int id,int fid,int son){
    tree[fid].ch[son]=id;
    tree[id].fa=fid;
}

inline void update(int p){
    tree[p].size=tree[tree[p].ch[0]].size+tree[tree[p].ch[1]].size+tree[p].rec;
}

inline void Rotate(int x){
    int y=tree[x].fa,z=tree[y].fa;
    int p,q;
    if(x==tree[y].ch[1]){p=1;}
    else{p=0;}
    if(y==tree[z].ch[1]){q=1;}
    else{q=0;}
    Connect(tree[x].ch[p^1],y,p);
    Connect(y,x,p^1);
    Connect(x,z,q);update(y);update(x);
}

inline void Splay(int x,int to){
    int father=tree[to].fa;
    while(tree[x].fa!=father){
        int y=tree[x].fa,z=tree[y].fa;
        int p,q;
        if(x==tree[y].ch[1]){p=1;}
        else{p=0;}
        if(y==tree[z].ch[1]){q=1;}
        else{q=0;}
        if(to==y){Rotate(x);}//到任意点to;
        else if(p==q){
            Rotate(y);Rotate(x);
        }else{
            Rotate(x);Rotate(x);
        }
    }
}

inline int find(int i){
    int p=root;
    while(p!=0){
        Pushdown(p);//记得下放;
        int size=tree[tree[p].ch[0]].size;
        if(size<i&&size+tree[p].rec>=i){
            return p;
        }else if(size+tree[p].rec<i){
            i-=size+tree[p].rec;p=tree[p].ch[1];
        }else{
            p=tree[p].ch[0];
        }
    }return 0;
}

inline void addnode(int fa,int v){
    tree[++ni].v=v;
    tree[ni].fa=fa;
    tree[ni].rec++;
    tree[ni].size++;
}

inline void Insert(int v){
    int p=root,nodeid;
    if(root==0){addnode(0,v);root=ni;return;}
    while(p!=0){
        tree[p].size++;
        if(v==tree[p].v){nodeid=p;tree[p].rec++;break;}
        if(v<tree[p].v){
            if(tree[p].ch[0]==0){
                addnode(p,v);nodeid=tree[p].ch[0]=ni;break;
            }else{p=tree[p].ch[0];}
        }else if(v>tree[p].v){
            if(tree[p].ch[1]==0){
                addnode(p,v);nodeid=tree[p].ch[1]=ni;break;
            }else{p=tree[p].ch[1];}
        }
    }Splay(nodeid,root);
}

inline void BackTurn(int l,int r){
    l=find(l);Splay(l,root);
    r=find(r+2);Splay(r,tree[root].ch[1]);
    tree[tree[tree[root].ch[1]].ch[0]].c^=1;
}

inline void Result(int p){
    Pushdown(p);//记得下放;
    if(tree[p].ch[0]){Result(tree[p].ch[0]);}
    if(tree[p].v>1&&tree[p].v<n+2){printf("%d ",tree[p].v-1);}
    if(tree[p].ch[1]){Result(tree[p].ch[1]);}
}

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){f=-1;}ch=getchar();
    }while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
    }return x*f;
}

int main(){
    n=read();m=read();
    for(int i=1;i<=n+2;i++){Insert(i);}//从2开始,以防整个区间反转;
    for(int i=1;i<=m;i++){
        int l=read(),r=read();BackTurn(l,r);
    }Result(root);
    return 0;
}

主席树

可持久化值域线段树

C++
//保留了(下标历史顺序)历史状态的值域线段树:
#include<bits/stdc++.h>
using namespace std;

int n,m,lel;

class HistoricSegtree{
	
	private:

		struct node{
			int ch[2];
			int v;
		}tree[10000001];
		int ni;
		int root[10000001];

		inline int Insert(int p,int l,int r,int v){
			int newp=++ni;tree[newp].v=tree[p].v+1;
			tree[newp].ch[0]=tree[p].ch[0];tree[newp].ch[1]=tree[p].ch[1];
			if(l==r){return newp;}
			int mid=(l+r)/2;
			if(v<=mid){tree[newp].ch[0]=Insert(tree[p].ch[0],l,mid,v);}
			else{tree[newp].ch[1]=Insert(tree[p].ch[1],mid+1,r,v);}
			return newp;
		}
		
		inline int Build(int p,int l,int r){
			if(p==0){
				p=++ni;
			}tree[p].v=0;
			if(l!=r){
				int mid=(l+r)/2;
				tree[p].ch[0]=Build(tree[p].ch[0],l,mid);
				tree[p].ch[1]=Build(tree[p].ch[1],mid+1,r);
			}return p;
		}
		
	public:

		inline void Init(int p,int l,int r){Build(p,l,r);root[0]=1;}

		inline void Push(int i,int end,int v){root[i]=Insert(root[i-1],1,end,v);}

		inline int Query(int l,int r,int rank){
			int root1=root[l-1],root2=root[r];
			int p=1,q=lel;
			while(p!=q){
				int mid=(p+q)/2;
				int delta=tree[tree[root2].ch[0]].v-tree[tree[root1].ch[0]].v;
				if(delta>=rank){
					q=mid;
					root2=tree[root2].ch[0];root1=tree[root1].ch[0];
				}else{
					rank-=delta;p=mid+1;
					root2=tree[root2].ch[1];root1=tree[root1].ch[1];
				}
			}return p;
		}

}s;

struct build{
	int v,loc;
	inline bool operator <(const build x){
		return v<x.v;
	}
}a[10000001];
int b[10000001],refl[10000001];

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){f=-1;}ch=getchar();
    }while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
    }return x*f;
}

int main(){
	n=read();m=read();
	for(int i=1;i<=n;i++){a[i].v=read();a[i].loc=i;}
	sort(a+1,a+1+n);
	int last=a[1].v;b[a[1].loc]=++lel;refl[lel]=last;
	for(int i=2;i<=n;i++){
		if(a[i].v==last){b[a[i].loc]=lel;}
		else{b[a[i].loc]=++lel;refl[lel]=last=a[i].v;}
	}s.Init(1,1,lel);
	for(int i=1;i<=n;i++){
		s.Push(i,lel,b[i]);
	}for(int i=1;i<=m;i++){
		int l=read(),r=read(),k=read();
		printf("%d\n",refl[s.Query(l,r,k)]);
	}return 0;
}

可持久化普通线段树

C++
//可持续化数组就是主席树的另一种用法:套用标准线段树;
//为了防止你不知道,我又存了一遍;
#include<bits/stdc++.h>
using namespace std;

int n,m;
int a[10000001];

class HistoricSegtree{
	
	public : int a[10000001];

	private :

		struct node{
			int ch[2];
			int v;
		}tree[50000001];
		int ni,ti;
		int root[10000001];

		inline int Build(int p,int l,int r){
			if(l>r){return 0;}
			p=++ni;
			if(l==r){tree[p].v=a[l];return p;}//维护数组下标,是标准线段树;
			int mid=(l+r)>>1;
			tree[p].ch[0]=Build(tree[p].ch[0],l,mid);
			tree[p].ch[1]=Build(tree[p].ch[1],mid+1,r);
			return p;
		}

		inline int Update(int p,int l,int r,int q,int v){
			int newp=++ni;tree[newp]=tree[p];
			if(l==r){tree[newp].v=v;return newp;}
			int mid=(l+r)/2;
			if(q<=mid){tree[newp].ch[0]=Update(tree[p].ch[0],l,mid,q,v);}
			else{tree[newp].ch[1]=Update(tree[p].ch[1],mid+1,r,q,v);}
			return newp;
		}

		inline int Query(int p,int l,int r,int q){
			if(l==r){return tree[p].v;}
			int mid=(l+r)/2;
			if(q<=mid){return Query(tree[p].ch[0],l,mid,q);}
			else{return Query(tree[p].ch[1],mid+1,r,q);}
		}

	public : 

		inline void Init(int l,int r){
			root[0]=Build(1,l,r);
		}

		inline void Change(int sc,int l,int r,int loc,int val){
			root[++ti]=Update(root[sc],l,r,loc,val);
		}

		inline int Search(int sc,int l,int r,int loc){
			int re=Query(root[sc],l,r,loc);
			root[++ti]=root[sc];
			return re;
		}
}s;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){f=-1;}ch=getchar();
    }while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
    }return x*f;
}

int main(){
	n=read();m=read();
	for(int i=1;i<=n;i++){s.a[i]=read();}
	s.Init(1,n);
	for(int i=1;i<=m;i++){
		int v=read(),f=read(),loc=read();
		if(f==1){
			int val=read();
			s.Change(v,1,n,loc,val);
		}else{
			printf("%d\n",s.Search(v,1,n,loc));
		}
	}return 0;
}

可持久化并查集

C++
//普通的路径压缩合并在树上很容易就被卡掉了,而且你在树上这样搞很容易爆,效率也低;

//按秩合并,是可持久化并查集中常用的合并方式.其实也就是一种类似于启发式合并的方式,
//每一次合并时选择一个深度小的点向深度大的合并.这样就可以保证并查集的高度不会增长的太快,保证高度尽量均衡
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;

int n,m;

class HistoricSet{

	public :

		struct inside{
			int ch[2];
			int v,dep;
		}tree[50000001];
		int ni;
		int root[10000001];//方便按秩合并

	private :

		inline int Build(int p,int l,int r){
			if(l>r){return 0;}
			p=++ni;
			if(l==r){tree[p].v=l;return p;}//维护数组下标,是标准线段树;
			int mid=(l+r)>>1;
			tree[p].ch[0]=Build(tree[p].ch[0],l,mid);
			tree[p].ch[1]=Build(tree[p].ch[1],mid+1,r);
			return p;
		}

		inline int Merge(int p,int l,int r,int loc,int v){
			int newp=++ni;
			tree[newp].ch[0]=tree[p].ch[0];tree[newp].ch[1]=tree[p].ch[1];
			if(l==r){
				tree[newp].v=v;
				tree[newp].dep=tree[p].dep;
				return newp;
			}int mid=(l+r)>>1;
			if(loc<=mid){tree[newp].ch[0]=Merge(tree[p].ch[0],l,mid,loc,v);}
			else{tree[newp].ch[1]=Merge(tree[p].ch[1],mid+1,r,loc,v);}
			return newp;
		}

		inline void Update(int p,int l,int r,int loc){
			if(l==r){tree[p].dep++;return;}
			int mid=(l+r)>>1;
			if(loc<=mid){Update(tree[p].ch[0],l,mid,loc);}
			else{Update(tree[p].ch[1],mid+1,r,loc);}
		}

		inline int Query(int p,int l,int r,int loc){
			if(l==r){return p;}
			int mid=(l+r)>>1;
			if(loc<=mid){return Query(tree[p].ch[0],l,mid,loc);}
			else{return Query(tree[p].ch[1],mid+1,r,loc);}
		}

	public :

		inline void Separate(int l,int r){
			root[0]=Build(root[0],l,r);//debug(0);
		}

		inline int find(int p,int loc){
			int now=Query(p,1,n,loc);
			if(tree[now].v!=loc){return find(p,tree[now].v);}
			return now;
		}

		inline void unionn(int id,int x,int y){
			int posx=find(root[id-1],x),posy=find(root[id-1],y);
			if(tree[posx].v!=tree[posy].v){
				if(tree[posx].dep>tree[posy].dep){swap(posx,posy);}
				root[id]=Merge(root[id-1],1,n,tree[posx].v,tree[posy].v);
				if(tree[posx].dep==tree[posy].dep){Update(root[id],1,n,tree[posy].v);}
				//因为不可能出现深度相同的两个点,所以要把其中一个点深度+1,
				//由于是深度小的合到深度大的上,所以把深度小的增加深度
			}else{root[id]=root[id-1];}
		}

		inline void debug(int p){
			for(int i=1;i<=n;i++){cout<<"----"<<i<<" "<<tree[Query(p,1,n,i)].v<<endl;}
		}
}s;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){f=-1;}ch=getchar();
    }while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
    }return x*f;
}

int main(){
	n=read();m=read();
	s.Separate(1,n);
	for(int i=1;i<=m;i++){
		int f=read();
		if(f==1){
			int a=read(),b=read();
			s.unionn(i,a,b);
		}else if(f==2){
			s.root[i]=s.root[read()];
		}else{
			int a=read(),b=read();
			s.root[i]=s.root[i-1];//没有更改也要至少存一下把版本根传递下去;
			int posx=s.find(s.root[i],a),posy=s.find(s.root[i],b);
			if(s.tree[posx].v!=s.tree[posy].v){puts("0");}
			else{puts("1");}
		}//s.debug(i);
	}return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;

// * 树(真实树)指本来的树(真实的树);

int n,m;
struct node{
	int s,v,fa,c;//v:树上的结点值,fa:父亲,c:区间反转的懒标记,s:子树累计值;
	int ch[2];
}tree[10000001];

//我们按照深度把原树维护成多株平衡树;
//我们要将原森林剖分成很多个二叉树并且用splay来维护它;

// * 用虚边和实边把各株树连接起来形成的一大棵树我们称为原树;

//实边是真实有效的双向边,既指向父亲也指向儿子;
//虚边是存在而表示连通性的边,只是为了把各株平衡树的根连接起来,只指向父亲,其父亲的左右儿子都不是该结点;
//但是事实上我们是根据深度来维护的,所以中序遍历才是树上链的顺序;

// * 根指原树的根;

#define lc tree[x].ch[0]
#define rc tree[x].ch[1]

//区间操作:剖出一棵只含有区间结点的平衡树;

//1.如何在原树上从x到y剖出一棵平衡树?

//Split(x,y):

//我们考虑从根到x剖出一棵平衡树;
//再把x变成根 ( 在树上随便改变根区间信息不会变 );
//再从根(x)到y剖出一棵平衡树;
//最后为了维护懒标记,记得把y splay上去;

//{

	//如何从根剖出一棵平衡树?

	//access(x);

	//我们自下而上的剖,因为虚边只指向父亲;
	//考虑区间内的结点x,它的父亲一定比它深度小,那我们就把x Splay到平衡树的根,把右儿子断成虚边;
	//然后一直把左儿子Splay到根,接着到另一棵平衡树,一直上去,得到目标平衡树;
	//当然也一定要把结点信息上推上去;

inline void pushup(int x){
	tree[x].s=tree[lc].s^tree[rc].s^tree[x].v;
}

	//{

		//那么单棵平衡树内的splay有怎么写呐?

		//首先我们要判断x是否是根;

inline bool notroot(int x){
	return x==tree[tree[x].fa].ch[0]||x==tree[tree[x].fa].ch[1];
}

		//和原来的splay的区别:判断是否平衡树根,而不是超级根(不存在了);
		//同时由于之后的makeroot操作要区间反转一下,会树上的深度反转,而且其使用了懒标记加速,所以要下放懒标记;

inline void reverse(int x){//区间反转;
	swap(lc,rc);tree[x].c^=1;
}

inline void pushdown(int x){//下放懒标记;
	if(tree[x].c){
		if(lc){reverse(lc);}
		if(rc){reverse(rc);}
		tree[x].c=0;
	}
}

inline void connect(int id,int fid,int which){
	tree[id].fa=fid;
	tree[fid].ch[which]=id;
}

inline void rotate(int x){
	int y=tree[x].fa,z=tree[y].fa;
	int xy=(x!=tree[y].ch[0]);
	int zy=(y!=tree[z].ch[0]);
	if(notroot(y)){
		connect(x,z,zy);	
	}else{tree[x].fa=z;}
	connect(tree[x].ch[xy^1],y,xy);
	connect(y,x,xy^1);
	pushup(y);
	pushup(x);
}

int stk[10000001];

inline void splay(int x){
	int now=x,pos=0;
	while(notroot(now)){//从下找到上再从上往下下推懒标记;
		stk[++pos]=now;
		now=tree[now].fa;
	}pushdown(now);//记得把原树根pushdown;
	while(pos){
		pushdown(stk[pos--]);
	}while(notroot(x)){//splay;
		int y=tree[x].fa,z=tree[y].fa;
		int xy=(tree[y].ch[0]!=x);
		int yz=(tree[z].ch[0]!=y);
		if(!notroot(y)){rotate(x);}
		else if(xy==yz){rotate(y);rotate(x);}
		else{rotate(x);rotate(x);}
	}
}

	//access():

inline void access(int x){
	for(int y=0;x;y=x,x=tree[x].fa){
		splay(x);
		rc=y;
		pushup(x);
	}
}


	//那么我们怎么把x旋到原根?

	//makeroot():考虑到我们剖出的是一条链,所以直接把区间反转一下就好了;
	//也就是把剖出的平衡树的x提到根,为了维护深度再反转一下区间;

inline void makeroot(int x){//换根;
	access(x);
	splay(x);
	reverse(x);
}

	//那么split操作就完成了;

	//}

//}


inline void split(int x,int y){
	makeroot(x);
	access(y);
	splay(y);
}

//现在开始实现LinkCutTree的第一个功能 : Link;

//我们知道,不能直接把c和e变成父子关系;
//但如果c能变成它所在树的根,那就可以把c变成e的子树;

//那就splay一遍好了,但是如果属于一棵原树(可能是把2棵树连起来,那就有2棵原树),那就不能这样了;

//所以我们需要找找根;

//inline int findroot(int x){//找根,在真实树中;
//	while(tree[x].fa){x=tree[x].fa;}			<- false!
//	return x;
//}

//但问题还没有这么简单,因为我们是按深度维护的,最上面的平衡树的最小才是真实树的根;

//所以我们把原树的根到结点N打通,在把N splay到根去,找到最左边的就行;

//注意过程中懒标记的pushdown;

//最后再把原树的原根还原(维护懒标记);

inline int findroot(int x){
	access(x);splay(x);
	while(lc){
		pushdown(x);
		x=lc;
	}splay(x);
	return x;
}

inline void link(int x,int y){
	makeroot(x);
	if(findroot(y)!=x){tree[x].fa=y;}
}


//接下来是cut操作:

//消灭父子关系时一定满足以下条件:
//1.x和y在一个原树里(不在一个树里面往哪儿切啊);
//2.split之后x是y的左儿子;
//3.x的右儿子是空的(保证了中序遍历中y紧跟在x的后面,即深度相邻);
//(x的权值(深度)只比y小1,而x又正好是直接连着y的,所以我们无法再找到 >x 而又 <y 的整数了);

//直接说就是不在一棵树上不能砍,有共同的父亲,没有直接联系不能砍;

//首先我们先把x,y之间的那条边用split(x,y)拎出来,
//因为x,y是相邻的,所以y的左儿子一定是x,将它们的父子关系消灭掉即可;

//如何切断(C,G):C<G

//1.把结点C makeroot 变成原树的根,一定有fa[G]=C,否则不再一棵树内;

//2.判断C和G之间是否为有意义的边:fa[G]=C,ch[1]=G;

//3.如果有意义,就把双向的关系都断了;

//4.切断后更新G的积累权值;

inline void cut(int x,int y){
	makeroot(x);
	if(findroot(y)==x&&tree[y].fa==x&&!tree[y].ch[0]){
		tree[y].fa=tree[x].ch[1]=0;
		pushup(x);
	}
}

//单点修改:

//直接旋到根再改就行了;

inline void update(int x,int v){
	splay(x);tree[x].v=v;
}


//区间查询:

//直接把区间剖出来,顶上的就是答案;

inline int query(int x,int y){
	split(x,y);
	return tree[y].s;
}

inline int read(){
	int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){f=-1;}ch=getchar();
    }while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
    }return x*f;
}

int main(){
	//freopen("testdata.in","r",stdin);
	//freopen("out.txt","w",stdout); 
	n=read();m=read();
	for(int i=1;i<=n;i++){tree[i].v=read();}
	for(int i=1;i<=m;i++){
		int opt=read(),x=read(),y=read();
		if(opt==0){
			printf("%d\n",query(x,y));
		}else if(opt==1){
			link(x,y);
		}else if(opt==2){
			cut(x,y);
		}else{
			update(x,y);
		}
	}return 0;
}
原文
存档评论1 条抓取于