萌新求助样例没过的模板 LCT
查看原帖
萌新求助样例没过的模板 LCT
350270
CatFromMars楼主2024/7/9 09:29

如题,实际上是照着题解打的,刚刚学,很多地方理解不清,烦请各位巨佬斧正 qaq

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;

int n, m, a[N + 10];

struct node {
	int son[2], fa, cxr, rev;
} T[N + 10];
void push_up(int x) {
	T[x].cxr = T[T[x].son[0]].cxr ^ T[T[x].son[1]].cxr ^ a[x];
}
void push_down(int x) {
	if(T[x].rev) {
		T[T[x].son[0]].rev ^= 1;
		T[T[x].son[1]].rev ^= 1;
		swap(T[x].son[0], T[x].son[1]);
		T[x].rev = 0;
	}
}
bool lor(int x) {
	return (T[T[x].fa].son[1] == x);
}
bool isrt(int x) {
	return ((T[T[x].fa].son[0] != x) && (T[T[x].fa].son[1] != x)) || !x;
}
void connect(int x, int fa, int opt) {
	T[fa].son[opt] = x;
	T[x].fa = fa;
}
void rotate(int x) {
	int y = T[x].fa, r = T[y].fa;
	push_down(r), push_down(x);
	int xlr = lor(x), ylr = lor(y);

	int B = T[x].son[xlr ^ 1];
	connect(B, y, xlr);
	connect(y, x, xlr ^ 1);
	if(!isrt(y))
		connect(x, r, ylr);
	push_up(y), push_up(x);
}
void splay(int x) {
	while(!isrt(x)) {
		if(isrt(T[x].fa)) rotate(x);
		else if(lor(x) == lor(T[x].fa)) rotate(T[x].fa), rotate(x);
		else rotate(x), rotate(x);
	}
}

void acces(int x) {
	for(int pre = 0; x; pre = x, x = T[x].fa) {
		splay(x), T[x].son[1] = pre;
		push_up(x);
	}
}
void makert(int x) {
	acces(x);
	splay(x);
	T[x].rev ^= 1;
	push_down(x); //try to cancel this
}
int findrt(int x) {
	acces(x), splay(x);
	int now = x;
	while(1) {
		push_down(now);
		if(!T[now].son[0]) break;
		now = T[now].son[0];
	}
	splay(now);
	return now;
}
void split(int x, int y) {
	makert(x);
	acces(y);
	splay(y);	
}
void link(int x, int y) {
	makert(x);
	if(findrt(x) != findrt(y))
		T[x].fa = y;
}
void cut(int x, int y) {
	split(x, y);
	if(findrt(x) == findrt(y) && T[y].son[0] == x && !T[x].son[1]) {
		T[x].fa = T[y].son[0] = 0;
		push_up(y);
	}
}
void change(int x, int val) {
	splay(x);
	a[x] = val;
	push_up(x);
}
int query(int u, int v) {
	split(u, v);
	return T[v].cxr;
}

int main() {
	cin >> n >> m;
	for(int p = 1; p <= n; p++) cin >> a[p];

	for(int p = 1, opt, x, y; p <= m; p++) {
		cin >> opt >> x >> y;
		if(!opt) cout << query(x, y) << endl;
		else if(opt == 1) link(x, y);
		else if(opt == 2) cut(x, y);
		else if(opt == 3) change(x, y);
	}
}
2024/7/9 09:29
加载中...