挂样例2过不了的LCT,求助&玄关
查看原帖
挂样例2过不了的LCT,求助&玄关
160150
WxjzKK楼主2023/11/4 15:36
//【模板】Link-Cut Tree,LCT 
#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
inline void read(int &n){
	int s=0,t=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') t=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		s=(s<<3)+(s<<1)+(c^48);c=getchar();
	}
	n=s*t;
}
inline void put(int n){
	if(n<0){
		putchar('-');n=-n;
	}
	if(n<10){
		putchar(n+48);return;
	}
	put(n/10);
	putchar(n%10+48);
}
struct node{
	int fa,son[2],val,sum,tag;
}t[MAXN];
inline void update(int rt){
	swap(t[rt].son[0],t[rt].son[1]);t[rt].tag^=1;
}
inline void pushup(int rt){
	t[rt].sum=t[t[rt].son[0]].sum^t[t[rt].son[1]].sum^t[rt].val;
}
inline void pushdown(int rt){
	if(t[rt].tag){
		if(t[rt].son[0]) update(t[rt].son[0]);
		if(t[rt].son[1]) update(t[rt].son[1]);
		t[rt].tag=0;
	}
}
inline bool checkroot(int rt){
	return t[t[rt].fa].son[0]==rt||t[t[rt].fa].son[1]==rt;
}
inline int wson(int rt){
	return t[t[rt].fa].son[1]==rt;
}
inline void rotate(int rt){
	int fa=t[rt].fa,gfa=t[fa].fa,ws=wson(rt),wsf=wson(fa);
	if(checkroot(fa)) t[gfa].son[wsf]=rt;
	t[fa].son[ws]=t[rt].son[ws^1];
	t[t[fa].son[ws]].fa=fa;
	t[rt].son[ws^1]=fa;
	t[fa].fa=rt;t[rt].fa=gfa;
	pushup(fa);pushup(rt);
}
inline void Splay(int rt){
	stack<int>s;int tmp=rt;
	while(checkroot(tmp)) s.push(tmp=t[tmp].fa);
	while(!s.empty()){
		pushdown(s.top());s.pop();
	}
	for(register int fa=t[rt].fa;checkroot(rt);rotate(rt),fa=t[rt].fa)
		if(checkroot(fa)) rotate(wson(rt)==wson(fa)?fa:rt);
	pushup(rt);
}
inline void access(int rt){
	for(register int son=0;rt;son=rt,rt=t[rt].fa){
		Splay(rt);t[rt].son[1]=son;pushup(rt);
	}
}
inline void makeroot(int rt){
	access(rt);Splay(rt);update(rt);
}
inline int findroot(int rt){
	access(rt);Splay(rt);
	while(t[rt].son[0]){
		pushdown(rt);rt=t[rt].son[0];
	}
	Splay(rt);return rt;
}
inline void split(int x,int y){
	makeroot(x);access(y);Splay(y);
}
inline void link(int x,int y){
	makeroot(x);
	if(findroot(y)!=x) t[x].fa=y;
}
inline void cut(int x,int y){
	makeroot(x);
	if(findroot(y)==x&&t[y].fa==x&&!t[y].son[0]){
		t[y].fa=t[x].son[1]=0;pushup(x);
	}
}
int main(){
	int n,m,op,x,y;
	read(n);read(m);
	for(register int i=1;i<=n;++i){
		read(t[i].val);t[i].sum=t[i].val;
	}
	for(register int i=1;i<=m;++i){
		read(op);read(x);read(y);
		if(op==0){
			split(x,y);put(t[y].sum);putchar('\n');
		}
		else if(op==1) link(x,y);
		else if(op==2) cut(x,y);
		else{
			Splay(x);t[x].val=y;
		}
	}
	return 0;
}

2023/11/4 15:36
加载中...