萌新求助
查看原帖
萌新求助
125454
CLCA_楼主2020/9/11 21:21

Rt,Link-Cut-Tree挂了

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define N 100005
using namespace std;
struct node{
	int fa,son[2],rev,sum,val;
}a[N];
#define ls a[x].son[0]
#define rs a[x].son[1]
void updata(int x){
	a[x].sum=a[ls].sum^a[rs].sum^a[x].val;
}
void pushup(int x){
	swap(ls,rs);a[x].rev^=1;
}
void down(int x){
	if(a[x].rev)pushup(ls),pushup(rs),a[x].rev=0;
}
#define rt(x) (a[a[x].fa].son[1]!=x&&a[a[x].fa].son[0]!=x)
#define son(x) (a[a[x].fa].son[1]==x)
void rotate(int x){
	int y=a[x].fa;int z=a[y].fa;
	int l=son(x),r=son(y);
	a[y].fa=x;a[x].fa=z;a[a[x].son[l^1]].fa=y;
	if(!rt(y))a[z].son[r]=x;
	a[y].son[l]=a[x].son[l^1];
	a[x].son[l^1]=y;updata(y);updata(x);
}
int sta[N],top;
void clean(int x){
	sta[++top]=x;
	while(!rt(x))x=a[x].fa,sta[++top]=x;
	while(top)down(sta[top--]);
}
void splay(int x){
	clean(x);
	while(!rt(x)){
		if(rt(a[x].fa))rotate(x);
		else if(son(x)!=son(a[x].fa))rotate(x),rotate(x);
		else rotate(a[x].fa),rotate(x);
	}updata(x);
}
void access(int x){
	for(int y=0;x;y=x,x=a[x].fa)splay(x),rs=y,updata(x);
}
void mkrt(int x){
	access(x);splay(x);pushup(x);
}
int fdrt(int x){
	access(x);splay(x);
	while(ls)down(x),x=ls;
	return splay(x),x;
}
void path(int x,int y){
	mkrt(x);access(y);splay(y);
}
void link(int x,int y){
	mkrt(x);
	if(fdrt(y)!=x)a[x].fa=y;
}
void cut(int x,int y){
	if(fdrt(x)!=fdrt(y))return;
	path(x,y);
	if(a[x].fa!=y||rs)return;
	a[x].fa=0;a[y].son[0]=0;
	updata(y);
}
int n,m;
int main(){
	scanf("%d%d",&n,&m);
	rep(i,1,n)scanf("%d",&a[i].val),a[i].sum=a[i].val;
	int op,x,y;
	while(m--){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)path(x,y),printf("%d\n",a[y].sum);
		else if(op==1)link(x,y);
		else if(op==2)cut(x,y);
		else splay(x),a[x].val=y,updata(x);
	}
	return 0;
} 
2020/9/11 21:21
加载中...