萌新刚学LCT,WA求助
查看原帖
萌新刚学LCT,WA求助
551861
strcmp楼主2022/4/2 23:55
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 4e5 + 10;
struct node {
	int ch[2], fa;
	ll val, sum, max;
	bool tage;
}spl[N];
#define ls(x) (spl[x].ch[0])
#define rs(x) (spl[x].ch[1])
#define fa(x) (spl[x].fa)
#define val(x) (spl[x].val)
#define sum(x) (spl[x].sum)
#define m(x) (spl[x].max)
#define indnt(x) (x == rs(fa(x)))
#define ntrt(x) (x == ls(fa(x)) || x == rs(fa(x)))
#define rvs(x) swap(ls(x), rs(x)), spl[x].tage ^= 1
inline void upd(int x){
	sum(x) = sum(ls(x)) + sum(rs(x)) + val(x);
	m(x) = max(val(x), max(m(ls(x)), m(rs(x))));
}
inline void pushdw(int x) {
	if (spl[x].tage) {
		if (ls(x))rvs(ls(x));
		if (rs(x))rvs(rs(x));
		spl[x].tage = 0;
	}
}
void pushall(int x) {
	if (ntrt(x))pushall(fa(x));
	pushdw(x);
}
inline void rtt(int x) {
	int f = fa(x), ff = fa(f), d = indnt(x);
	spl[f].ch[d] = spl[x].ch[d ^ 1];
	fa(spl[x].ch[d ^ 1]) = f;
	fa(x) = ff;
	if (ntrt(f))spl[ff].ch[indnt(f)] = x;
	spl[x].ch[d ^ 1] = f; fa(f) = x;
	upd(f), upd(x);
}
inline void splaying(int x) {
	pushall(x);
	int f, ff;
	while (ntrt(x)) {
		f = fa(x), ff = fa(f);
		if (ntrt(f))indnt(x) ^ indnt(f) ? rtt(x) : rtt(f);
		rtt(x);
	}
}
inline void access(int x) {
	for (int y = 0; x; x = fa(y = x)) {
		splaying(x);
		rs(x) = y;
		upd(x);
	}
}
inline void makert(int x) {
	access(x);
	splaying(x);
	rvs(x);
}
inline int findrt(int x) {
	access(x);
	splaying(x);
	while (ls(x))pushdw(x = ls(x));
	splaying(x);
	return x;
}
inline void link(int x, int y) {
	makert(x);
	if (findrt(y) != x)fa(x) = y;
}
inline void cut(int x, int y) {
	makert(x); access(y); splaying(y);
	if (ls(y) != x || rs(x))return;
	fa(x) = ls(y) = 0;
	upd(x);
}
inline void split(int x, int y) {
	makert(x);
	access(y);
	splaying(y);
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(); cout.tie();
	int n, m, x, y; string q;
	cin >> n;
	for(int i = 1;i<n;i++)cin>>x>>y,link(x,y);
	for (int i = 1; i <= n; i++)cin >> spl[i].val;
	spl[0].max = -99999999;
	cin>>m;
	while (m--) {
		cin>>q>>x>>y;
		if(q=="CHANGE"){
			splaying(x);
			spl[x].val = y;
			upd(x);
		}
		else if(q=="QMAX")split(x,y), cout<<spl[y].max<<endl;
		else split(x,y), cout<<spl[y].sum<<endl;
	}
	return 0;
}
2022/4/2 23:55
加载中...