【BZOJ】【2157】旅游

时间:2022-03-07 07:38:00

LCT

  直到动手写拆边为点的时候才发现根本不会写……去orz了一下Hzwer(话说这题应该也用不着LCT吧……下次再换种姿势写一遍好了)

 /**************************************************************
Problem: 2157
User: Tunix
Language: C++
Result: Accepted
Time:668 ms
Memory:2600 kb
****************************************************************/ //BZOJ 2157
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>''){ if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
return v*=sign;
}
/******************tamplate*********************/
const int N=,INF=~0u>>;
int n,m,cnt;
int ed[N];
int c[N][],fa[N],v[N],sum[N],mn[N],mx[N];
bool rev[N],Not[N];
#define L c[x][0]
#define R c[x][1]
bool not_root(int x){
return c[fa[x]][]==x || c[fa[x]][]==x;
}
void rever(int x){
sum[x]=-sum[x]; v[x]=-v[x];
swap(mn[x],mx[x]);
mn[x]=-mn[x];mx[x]=-mx[x];
Not[x]^=;
}
void Push_up(int x){
mx[x]=max(mx[L],mx[R]);
mn[x]=min(mn[L],mn[R]);
if (x>n){
mx[x]=max(mx[x],v[x]);
mn[x]=min(mn[x],v[x]);
}
sum[x]=sum[L]+sum[R]+v[x];
}
void Push_down(int x){
if (Not[x]){
Not[x]^=;
if (L) rever(L);
if (R) rever(R);
}
if (rev[x]){
rev[x]^=; rev[L]^=; rev[R]^=;
swap(L,R);
}
}
void rotate(int x){
int y=fa[x],z=fa[y],l=c[y][]==x,r=l^;
if (not_root(y)) c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][r]]=y;
c[y][l]=c[x][r]; c[x][r]=y;
Push_up(y);
}
void preview(int x){
if (not_root(x)) preview(fa[x]);
Push_down(x);
}
void splay(int x){
int y;
for(preview(x);not_root(x);rotate(x))
not_root(y=fa[x]) ? rotate(c[y][]==x^c[fa[y]][]==y ? x : y),:;
Push_up(x);
}
void access(int x,int las=){
for(;x;splay(x),c[x][]=las,las=x,x=fa[x]);
}
void makeroot(int x){
access(x),splay(x),rev[x]^=;
}
void link(int x,int y){
makeroot(x),fa[x]=y;
}
void query(int x,int y){
makeroot(x),access(y),splay(y);
}
int main(){
n=getint();
F(i,,n) mn[i]=INF,mx[i]=-INF;
int id=n;
F(i,,n-){
int x=getint()+,y=getint()+,w=getint();
ed[i]=++id;
link(x,id); link(y,id);
v[id]=sum[id]=mx[id]=mn[id]=w;
}
m=getint();
char cmd[]; int x,y;
F(i,,m){
scanf("%s",cmd);
x=getint(); y=getint();
if (cmd[]=='C'){
splay(ed[x]),v[ed[x]]=y,Push_up(ed[x]);
}
else if(cmd[]=='N')
query(x+,y+),rever(y+);
else if(cmd[]=='S')
query(x+,y+),printf("%d\n",sum[y+]);
else if(cmd[]=='A')
query(x+,y+),printf("%d\n",mx[y+]);
else query(x+,y+),printf("%d\n",mn[y+]);
}
return ;
}