这种题感觉LCT比树链剖分好写…
依旧SB了一些地方:
1.这题由于有取反标记的存在,update一个节点的时候它的子节点可能不是真实值!!!所以要先clear它的子节点!!!
2.splay()又有一些地方手滑了,下次一定要注意检查splay().
3.对于树中不表示边的节点,要注意不能让它的点权影响到更新,同时NIL->mx=-INF,NIL->mn=INF即可保证维护不出错(多想想).
AC code:
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=100010;
const int INF=1<<29;
int n,m;
struct nod{
bool rev,con;
int num,val,sum,mx,mn;
nod *pp,*pr,*ch[2];
}*NIL,poi[N];
struct LCT{
LCT(){
NIL=&poi[n<<1];NIL->mx=-INF;NIL->mn=INF;
for(int i=0;i<n;i++) poi[i]=newnod(i,0);
}
nod newnod(int num,int val){
nod t;
t.rev=t.con=0;
t.num=num;t.val=t.sum=t.mx=t.mn=val;t.pp=t.pr=t.ch[0]=t.ch[1]=NIL;
return t;
}
void update(nod *x){
if(x==NIL) return ;
x->sum=x->ch[0]->sum+x->ch[1]->sum+x->val;
x->mx=max(x->ch[0]->mx,x->ch[1]->mx);
x->mn=min(x->ch[0]->mn,x->ch[1]->mn);
if(x->num>=n) x->mx=max(x->val,x->mx),x->mn=min(x->val,x->mn);
}
void clear(nod *x){
if(x==NIL) return ;
if(x->rev){
swap(x->ch[0],x->ch[1]);
x->rev=0;x->ch[0]->rev^=1;x->ch[1]->rev^=1;
}
if(x->con){
x->val=-x->val;x->sum=-x->sum;
x->con=0;x->ch[0]->con^=1;x->ch[1]->con^=1;
x->mx=-x->mx;x->mn=-x->mn;swap(x->mx,x->mn);
}
}
void rotate(nod *x,bool t){
nod *y=x->pr,*z=y->pr,*b=x->ch[t^1];
b->pr=y;y->pr=x;x->pr=z;y->ch[t]=b;x->ch[t^1]=y;
if(z->ch[0]==y) z->ch[0]=x;
if(z->ch[1]==y) z->ch[1]=x;
clear(y->ch[0]);clear(y->ch[1]);clear(x->ch[t]);
update(y);update(x);
}
void splay(nod *x){
int cnt=0;
nod *i;
nod *stk[N];
for(i=x;i->pr!=NIL;i=i->pr) stk[++cnt]=i;
x->pp=i->pp;stk[++cnt]=i;
for(int i=cnt;i>0;i--) clear(stk[i]);
while(x->pr!=NIL){
nod *y=x->pr,*z=y->pr;
if(z==NIL&&y->ch[0]==x) rotate(x,0);
else if(z==NIL&&y->ch[1]==x) rotate(x,1);
else if(z->ch[0]==y&&y->ch[0]==x) {rotate(y,0);rotate(x,0);}
else if(z->ch[1]==y&&y->ch[1]==x) {rotate(y,1);rotate(x,1);}
else if(z->ch[1]==y&&y->ch[0]==x) {rotate(x,0);rotate(x,1);}
else {rotate(x,1);rotate(x,0);}
}
}
void access(nod *x){
for(nod *u=x,*v=NIL;u!=NIL;v=u,u=u->pp){
splay(u);u->ch[1]->pr=NIL;u->ch[1]->pp=u;u->ch[1]=v;v->pr=u;
clear(u->ch[0]);clear(u->ch[1]);update(u);
}
}
void makeroot(nod *x){
access(x);splay(x);x->rev^=1;
}
void link(nod *x,nod *y){
makeroot(x);x->pp=y;
}
nod* getpath(nod *x,nod *y){
makeroot(x);access(y);splay(y);
return y;
}
void work1(int x,int y){
nod *t=getpath(&poi[x],&poi[x]);
t->val=y;clear(t->ch[0]);clear(t->ch[1]);update(t);
}
void work2(int x,int y){
nod *t=getpath(&poi[x],&poi[y]);
t->con^=1;
}
void work3(int x,int y){
nod *t=getpath(&poi[x],&poi[y]);
printf("%d\n",t->sum);
}
void work4(int x,int y){
nod *t=getpath(&poi[x],&poi[y]);
printf("%d\n",t->mx);
}
void work5(int x,int y){
nod *t=getpath(&poi[x],&poi[y]);
printf("%d\n",t->mn);
}
};
int main(){
scanf("%d",&n);
LCT T;
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
poi[n+i-1]=T.newnod(n+i-1,w);
T.link(&poi[n+i-1],&poi[u]);T.link(&poi[v],&poi[n+i-1]);
}
scanf("%d",&m);
for(int i=1;i<=m;i++){
int x,y;
char s[11];
scanf("%s%d%d",s,&x,&y);
if(s[0]=='C') T.work1(n+x-1,y);
else if(s[0]=='N') T.work2(x,y);
else if(s[0]=='S') T.work3(x,y);
else if(s[0]=='M'&&s[1]=='A') T.work4(x,y);
else T.work5(x,y);
}
return 0;
}