一眼树链剖分或 \(LCT\),由于在学后者所以就写了。
取反操作相当于把 \(min,max\) 取反后交换,所以要维护 \(min,max,val\)。
时间复杂度 \(O(m\log n)\)。
#include<bits/stdc++.h>
#define fa(x) lct[x].fa
#define fl(x) lct[x].fl
#define mx(x) lct[x].mx
#define mn(x) lct[x].mn
#define id(x) lct[x].id
#define val(x) lct[x].val
#define sn(x,i) lct[x].sn[i]
#define inf 1000000000
using namespace std;
const int N=20005;
struct node{int sn[2],fa,fl,mx,mn,val,id;
}lct[N];int n,m,tp,st[N];
int check(int x){return sn(fa(x),0)!=x&&sn(fa(x),1)!=x;
}int chksn(int x){return sn(fa(x),1)==x;
}void push_up(int x){mx(x)=max({mx(sn(x,0)),mx(sn(x,1)),x>n?val(x):-inf});mn(x)=min({mn(sn(x,0)),mn(sn(x,1)),x>n?val(x):inf});
}void push_down(int x){if(!x) return;if(fl(x)){fl(sn(x,0))^=1,fl(sn(x,1))^=1;swap(sn(x,0),sn(x,1)),fl(x)=0;}if(!id(x)) return;id(x)=0,swap(mx(sn(x,0)),mn(sn(x,0)));swap(mx(sn(x,1)),mn(sn(x,1)));id(sn(x,0))^=1,id(sn(x,1))^=1;val(sn(x,0))=-val(sn(x,0));val(sn(x,1))=-val(sn(x,1));mx(sn(x,0))=-mx(sn(x,0));mn(sn(x,0))=-mn(sn(x,0));mx(sn(x,1))=-mx(sn(x,1));mn(sn(x,1))=-mn(sn(x,1));
}void rotate(int x){int y=fa(x),z=fa(y),k=chksn(x);if(!check(y))sn(z,chksn(y))=x;fa(x)=z,fa(y)=x,fa(sn(x,1-k))=y;sn(y,k)=sn(x,1-k),sn(x,1-k)=y;push_up(y);
}void splay(int x){st[tp=1]=x;for(int i=x;!check(i);i=fa(i)) st[++tp]=fa(i);while(tp) push_down(st[tp--]);while(!check(x)){int y=fa(x),z=fa(y);if(!check(y))rotate(chksn(x)!=chksn(y)?x:y);rotate(x);}push_up(x);
}void access(int x){for(int i=0;x;i=x,x=fa(x))splay(x),sn(x,1)=i,push_up(x);
}void mk(int x){access(x),splay(x),fl(x)^=1;
}void split(int x,int y){mk(x),access(y),splay(y);
}void link(int x,int y){mk(x),access(y),fa(x)=y;
}int main(){ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);cin>>n,mx(n)=-inf,mn(n)=inf;mx(0)=-inf,mn(0)=inf;for(int i=1;i<n;i++){mx(i)=-inf,mn(i)=inf;int x,y,z;cin>>x>>y>>z;mx(n+i)=mn(n+i)=val(n+i)=z;link(x,n+i),link(n+i,y);}while(1){string s;int x,y;cin>>s;if(s=="DONE") break;cin>>x>>y;if(s=="NEGATE"){split(x,y),id(y)^=1;swap(mx(y),mn(y)),val(y)=-val(y);mx(y)=-mx(y),mn(y)=-mn(y);}if(s=="CHANGE")mk(n+x),mn(n+x)=mx(n+x)=val(n+x)=y;if(s=="QUERY") split(x,y),cout<<mx(y)<<"\n";}return 0;
}