救命,样例都RE,找了一天错误了
查看原帖
救命,样例都RE,找了一天错误了
165375
Fast_Gai_Tle楼主2021/8/28 10:54
#include <bits/stdc++.h>
#define re register
#define N 100005 
using namespace std;
struct LinkCutTree{
	int top, a[N], w[N], fa[N], rev[N], sta[N], ch[N][2];
	void clear(){
		top = 0;
		memset(a, 0, sizeof(a));
		memset(w, 0, sizeof(w));
		memset(fa, 0, sizeof(fa));
		memset(ch, 0, sizeof(ch));
		memset(sta, 0, sizeof(sta));
		memset(rev, false, sizeof(rev));
	}
	void pushup(int x){ w[x] = w[ch[x][0]] ^ w[ch[x][1]] ^ a[x]; }
	void modify(int x){ rev[x] ^= 1; swap(ch[x][0], ch[x][1]); }
	bool isroot(int x){ return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
	void pushdown(int x){
		if(rev[x]){
			if(ch[x][0]) modify(ch[x][0]);
			if(ch[x][1]) modify(ch[x][1]);
			rev[x] = 0;
		}
	}
	void rotate(int x){
		int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
		if(!isroot(y)) ch[z][ch[z][1] == y] = x; ch[y][k] = w; ch[x][k ^ 1] = y;
		if(w) f[w] = y; f[y] = x; f[x] = z;
		pushup(y); pushup(x);
	}
	void splay(int x){
		sta[top = 1] = x;
		for(re int i = x; !isroot(i); i = fa[i]) sta[++top] = fa[i];
		while(top) pushdown(sta[top--]);
		while(!isroot(x)){
			int y = fa[x], z = fa[y];
			if(!isroot(z)) rotate(((ch[z][0] == y) ^ (ch[y][0] == x)) ? x : y);
			rotate(x);
		}
		pushup(x);
	}
	void access(int x){ for(re int i = 0; x; x = fa[i = x]) splay(x), ch[x][1] = i, pushup(x); }
	void makeroot(int x){ access(x); splay(x); modify(x); }
	int find(int x){ access(x); splay(x); while(ch[x][0]) pushdown(x), x = ch[x][0]; splay(x); return x; }
	void split(int x, int y){ makeroot(x); access(y); splay(y); }
	void cut(int x, int y){
		makeroot(x);
		if(find(y) != x || fa[y] != x || ch[y][0]) return ;
		fa[y] = ch[x][1] = 0; pushup(x);
	}
	void link(int x, int y){ makeroot(x); if(find(y) != x) fa[x] = y; }
}t;
int n, m;
int main(){
	scanf("%d%d", &n, &m);
	for(re int i = 1; i <= n; ++i) scanf("%d", &t.a[i]);
	while(m--){
		int op, x, y; scanf("%d%d%d", &op, &x, &y);
		switch(op){
			case 0:{ t.split(x, y); printf("%d\n", t.w[y]); break; }
			case 1:{ t.link(x, y); break; }
			case 2:{ t.cut(x, y); break; }
			case 3:{ t.splay(x); t.a[x] = y; break; }
		}
		for(re int i = 1; i <= n; ++i) cout << t.fa[i] << " "; cout << endl;
	}
	return 0;
}
2021/8/28 10:54
加载中...