TLE???
查看原帖
TLE???
185006
551969898tjy楼主2020/6/18 23:49
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
struct Node{
	int to[2],fa,val,sum,lazy,size;
	Node(){
		size=lazy=to[0]=to[1]=fa=val=sum=0;
	}
}t[maxn];
inline void maintain(int x){
	t[x].sum=t[x].val^t[t[x].to[0]].sum^t[t[x].to[1]].sum;
	t[x].size=t[t[x].to[0]].size + t[t[x].to[1]].size + 1;
}
inline void rev(int x){
	if(!x)return;
	swap(t[x].to[0],t[x].to[1]);
	t[x].lazy^=1;
}
inline void down(int x){
	if(t[x].lazy){
		rev(t[x].to[0]);
		rev(t[x].to[1]);
		t[x].lazy=0;
	}
}
inline int get(int x){return (t[t[x].fa].to[0]==x || t[t[x].fa].to[1] ==x ) ? t[t[x].fa].to[1]==x : -1 ;}
/*inline int get(int x){
	if(x!=t[t[x].fa].to[1]&&x!=t[t[x].fa].to[0])return -1;
	else return x==t[t[x].fa].to[1];
}*/
inline void rotate(int x){
	int f=t[x].fa;
	int gf=t[f].fa;
	bool b=get(x);
	if(get(f)!=-1)t[gf].to[get(f)]=x;
	t[x].fa=gf;
	if(t[x].to[b^1])t[t[x].to[b^1]].fa=f;
	t[f].to[b]=t[x].to[b^1];
	t[x].to[b^1]=f;
	t[f].fa=x;
	maintain(f);//maintain(x);
}
inline void splay(int x){
	stack<int >sta;	while(sta.size())sta.pop();
	for(int fx=x;;fx=t[fx].fa){
		sta.push(fx);
		if(get(fx)==-1)break;
		if(sta.size()>maxn){
			printf("GGGGG");
			exit(0);
		}
	}
	while(sta.size()){
		down(sta.top());
		sta.pop();
	}
	while(get(x)!=-1){
		if(get(t[x].fa)!=-1 && get(t[x].fa)==get(x))rotate(t[x].fa);
		else rotate(x);
	}
	maintain(x);
}
inline void access(int x){
	for(int fx=0;x;fx=x,x=t[x].fa){
		splay(x);
		t[x].to[1]=fx;
		maintain(x);//must
	}
}
inline void makeroot(int x){
	access(x);//²»Ò»¶¨ÊǸù 
	splay(x);
	rev(x);
}
inline int findroot(int x){
	access(x);
	splay(x);
	while(t[x].to[0]){
		down(x);//must
	//	down(t[x].to[0]);
		x=t[x].to[0];
	}
	splay(x);
	return x;
}
inline void cut(int x,int y){
	makeroot(x);
	if(findroot(y)!=x || t[x].size>2)return;
	t[y].fa=t[x].to[1]=0;//update y and x must!!!
	maintain(x);
	//GG??
	/*makeroot( x);
	if(findroot(y)!=x)return;
	access(y);splay(y);
	if(t[y].to[0]!=x || t[x].to[1])return;
	t[y].to[0]=t[x].fa=0;maintain(y);*/
}
inline void link(int x,int y){
	makeroot(x);
	if(findroot(y)==x)return;
	t[x].fa=y;
}
inline void split(int x,int y){
	makeroot(x);
	access(y);
	splay(y);
}
int n,m;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&t[i].val);
		t[i].sum=t[i].val;
		t[i].size=1;
	}
	for(int i=1,opt,x,y;i<=m;i++){
		scanf("%d%d%d",&opt,&x,&y);
		if(opt==0){
			split(x,y);
			printf("%d\n",t[y].sum);
		}
		else if(opt==1){
			link(x,y);
		}
		else if(opt==2){
			cut(x,y);
		}
		else {
			splay(x);
			t[x].val=y;
			maintain(x);
		}
	}
	return 0;
}

为什么第一个点会TLE呢,我觉得没什么死循环的地方。。。谢谢各位dalao指正

2020/6/18 23:49
加载中...