91分求助(并不是前面那位dalao的问题)
查看原帖
91分求助(并不是前面那位dalao的问题)
192374
wenkaijie楼主2021/1/22 23:02
# include <stdio.h>
# include <iostream>
# include <string.h>
# include <string>
# include <algorithm>
# include <math.h>
# include <queue>
# include <deque>
# include <climits>
# include <stack>
using namespace std;
int n,m;
struct link_cut_tree
{
	int num[100001],fa[100001],son[100001][2],tag[100001],ans[100001];
	bool isroot(int x)
	{
		return son[fa[x]][0]!=x && son[fa[x]][1]!=x;
	}
	void pushup(int x)
	{
		ans[x]=num[x]^ans[son[x][0]]^ans[son[x][1]];
	}
	void addtag(int x)
	{
		swap(son[x][0],son[x][1]);
		tag[x]^=1;
	}
	void pushdown(int x)
	{
		if(tag[x])
		{
			if(son[x][0])
				addtag(son[x][0]);
			if(son[x][1])
				addtag(son[x][1]);
			tag[x]=0;
		}
	}
	void pushall(int x)
	{
		if(!isroot(x))
			pushall(fa[x]);
		pushdown(x);
	}
	void rotate(int x)
	{
		int y=fa[x],z=fa[y],op=(son[y][1]==x);
		if(!isroot(y))
			son[z][son[z][1]==y]=x;
		fa[y]=x;
		fa[x]=z;
		son[y][op]=son[x][!op];
		if(son[y][op])
			fa[son[y][op]]=y;
		son[x][!op]=y;
		pushup(y);
	}
	void splay(int x)
	{
		int y,z;
		pushall(x);
		while(!isroot(x))
		{
			y=fa[x],z=fa[y];
			if(!isroot(y))
				if(son[z][1]==y ^ son[y][1]==x)
					rotate(x);
				else
					rotate(y);
			rotate(x); 
		}
		pushup(x);
	}
	void access(int x)
	{
		for(int y=0;x;y=x,x=fa[x])
		{
			splay(x);
			son[x][1]=y;
			pushup(x);
		}
	}
	void makeroot(int x)
	{
		access(x);
		splay(x);
		addtag(x);
	}
	int findroot(int x)
	{
		access(x);
		splay(x);
		while(son[x][0])
		{
			pushdown(x);
			x=son[x][0];
		}
		splay(x);
		return x;
	}
	void split(int x,int y)
	{
		makeroot(x);
		access(y);
		splay(y);
	}
	void link(int x,int y)
	{
		makeroot(x);
		if(findroot(y)!=x)
			fa[x]=y;
	}
	void cut(int x,int y)
	{
		makeroot(x);
		if(findroot(y)==x && fa[y]==x && !son[y][0])
		{
			fa[y]=0;
			son[x][1]=0;
			pushup(x);
		}
	}
}T;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",&T.num[i]);
	for(int i=1;i<=m;i++)
	{
		int op,x,y;
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)
		{
			T.split(x,y);
			printf("%d\n",T.ans[y]);
		}
		else if(op==1)
			T.link(x,y);
		else if(op==2)
			T.cut(x,y);
		else
			T.num[x]=y;
	}
	return 0;
}

2021/1/22 23:02
加载中...