LCT boom0 求助
查看原帖
LCT boom0 求助
482728
Engulf楼主2022/4/19 23:11

能过样例,交上去 WA ×\times 9 & TLE ×\times 1

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

const int N = 2e5+10;
int n,m;
struct LCT{
	int son[2],fa,val;
	int sum,mx,mn,rev,neg;
}tr[N<<2];
#define ls(x) (tr[x].son[0])
#define rs(x) (tr[x].son[1])
#define fa(x) (tr[x].fa)
inline void pushup(int x){
	tr[x].sum=tr[ls(x)].sum+tr[rs(x)].sum+tr[x].val;
	//tr[x].mx=x>n?tr[x].val:-0x3f3f3f3f;
	//tr[x].mn=x>n?tr[x].val:0x3f3f3f3f;
	tr[x].mx=max({tr[x].val,tr[ls(x)].mx,tr[rs(x)].mx});
	tr[x].mn=min({tr[x].val,tr[ls(x)].mn,tr[rs(x)].mn});
}
inline bool isroot(int x){return !(ls(fa(x))==x || rs(fa(x))==x);}
inline void reverse(int x){swap(ls(x),rs(x));tr[x].rev^=1;}
inline void negative(int x){
	tr[x].val=-tr[x].val;
	tr[x].sum=-tr[x].sum;
	tr[x].mx=-tr[x].mx;
	tr[x].mn=-tr[x].mn;
	swap(tr[x].mx,tr[x].mn);
	tr[x].neg^=1;
}
void pushdown(int x){
	if(tr[x].neg){
		if(ls(x))negative(ls(x));
		if(rs(x))negative(rs(x));
		tr[x].neg=0;
	}
	if(tr[x].rev){
		if(ls(x))reverse(ls(x));
		if(rs(x))reverse(rs(x));
		tr[x].rev=0;
	}
}
void pushall(int x){
	if(!isroot(x))pushall(fa(x));
	pushdown(x);
}
void rotate(int x){
	int y=fa(x),z=fa(y);
	int k=rs(y)==x;
	if(!isroot(y))tr[z].son[rs(z)==y]=x;
	fa(x)=z;
	tr[y].son[k]=tr[x].son[k^1],fa(tr[x].son[k^1])=y;
	tr[x].son[k^1]=y,fa(y)=x;
	pushup(y);pushup(x);
}
void splay(int x){
	pushall(x);
	while(!isroot(x)){
		int y=fa(x),z=fa(y);
		if(!isroot(y)){
			if((rs(z)==y) ^ (rs(y)==x))rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
}
void access(int x){
	for(int y=0;x;x=fa(y=x)){
		splay(x);rs(x)=y;pushup(x);
	}
}
void makeroot(int x){
	access(x);splay(x);reverse(x);
}
int findroot(int x){
	access(x);splay(x);
	while(ls(x)){
		pushdown(x);x=ls(x);
	}
	splay(x);
	return x;
}
void link(int x,int y){
	makeroot(x);
	if(findroot(y)==x)return;
	fa(x)=y;
}
void split(int x,int y){
	makeroot(x);access(y);splay(y);
}

int main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	cin>>n;
	for(int i=1,x,y,w;i<n;i++){
		cin>>x>>y>>w;
		x++;y++;
		tr[i+n].val=tr[i+n].mx=tr[i+n].mn=w;
		link(x,i+n);link(i+n,y);
	}
	cin>>m;
	while(m--){
		string s;int x,y;
		cin>>s>>x>>y;x++;y++;
		if(s=="C"){
			x--;y--;splay(x+n);tr[x+n].val=y;
		}if(s=="N"){
			split(x,y);negative(y);
		}if(s=="SUM"){
			split(x,y);cout<<tr[y].sum<<endl;
		}if(s=="MAX"){
			split(x,y);cout<<tr[y].mx<<endl;
		}if(s=="MIN"){
			split(x,y);cout<<tr[y].mn<<endl;
		}
	}
	return 0;
}
2022/4/19 23:11
加载中...