动态树裸题
把边建成一个点就好了
取反,打个标记
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> using namespace std; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++; } inline void read(int &x) { char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b; } inline void read(char *s) { char c=nc(); int len=0; for (;!(c>='a' && c<='z' || c>='A' && c<='Z');c=nc()); for (;c>='a' && c<='z' || c>='A' && c<='Z';s[++len]=c,c=nc()); s[++len]=0; } int n; struct Splay{ struct node{ int size,idx,rev,fbs; int val,maxv,minv; int sum,minimum,maximum; node *p,*ch[2],*fat; inline void setc(node *c,int d) { ch[d]=c; c->p=this; } inline bool dir() { return p->ch[1]==this; } inline void change(){ fbs^=1; swap(minimum,maximum); minimum=-minimum; maximum=-maximum; sum=-sum; if (idx>n) val=-val,minv=-minv,maxv=-maxv; } inline void update() { size=ch[0]->size+ch[1]->size+1; sum=ch[0]->sum+ch[1]->sum+val; minimum=min(minv,min(ch[0]->minimum,ch[1]->minimum)); maximum=max(maxv,max(ch[0]->maximum,ch[1]->maximum)); } inline void reverse() { rev^=1; swap(ch[0],ch[1]); } inline void pushdown(node *null){ if (rev){ if (ch[0]!=null) ch[0]->reverse(); if (ch[1]!=null) ch[1]->reverse(); rev=0; } if (fbs) { if (ch[0]!=null) ch[0]->change(); if (ch[1]!=null) ch[1]->change(); fbs=0; } } }*null,Mem[60005]; Splay() { null=Mem; null->p=null->ch[0]=null->ch[1]=null->fat=null; null->sum=0; null->size=0; null->maximum=-1<<30; null->minimum=1<<30; } inline void rot(node *x){ if (x==null || x->p==null) return ; bool d=x->dir(); node *p=x->p; if (p->p!=null) p->p->setc(x,p->dir()); else x->p=null; p->setc(x->ch[d^1],d); x->setc(p,d^1); p->update(); x->update(); swap(x->fat,p->fat); } inline void splay(node *x){ static node *sta[60005]; int pnt=0; node *y=x; while (y!=null) sta[++pnt]=y,y=y->p; for (int i=pnt;i;i--) sta[i]->pushdown(null); while (x->p!=null) if (x->p->p==null) rot(x); else x->dir()==x->p->dir()?(rot(x->p),rot(x)):(rot(x),rot(x)); } inline node *Access(node *x){ node *y=null; while (x!=null) { splay(x); x->ch[1]->p=null; x->ch[1]->fat=x; x->setc(y,1); y->fat=null; x->update(); y=x; x=x->fat; } return y; } inline void Link(node *x,node *y){ if (Jud(x,y)) return; Access(x)->reverse(); splay(x); x->fat=y; Access(x); } inline void Cut(node *x){ Access(x); splay(x); x->ch[0]->p=null; x->ch[0]=null; x->fat=null; x->update(); } inline void Cut(node *x,node *y){ Access(x)->reverse(); Cut(y); } inline node *Root(node *x){ Access(x); splay(x); node *y=x; while (y->ch[0]!=null) y=y->ch[0]; return y; } inline bool Jud(node *x,node *y){ return Root(x)==Root(y); } inline void Change(node *x,node *y){ Access(x)->reverse(); Access(y)->change(); } inline void Change(node *x,int r){ Access(x); splay(x); x->val=x->maxv=x->minv=r; x->update(); } inline int Sum(node *x,node *y){ Access(x)->reverse(); return Access(y)->sum; } inline int Max(node *x,node *y){ Access(x)->reverse(); return Access(y)->maximum; } inline int Min(node *x,node *y){ Access(x)->reverse(); return Access(y)->minimum; } }LCT; Splay::node *pos[60005]; inline void Init(int n){ for (int i=1;i<2*n;i++){ pos[i]=LCT.Mem+i; pos[i]->p=pos[i]->ch[0]=pos[i]->ch[1]=pos[i]->fat=LCT.null; pos[i]->val=0; pos[i]->maxv=-1<<30; pos[i]->minv=1<<30; pos[i]->maximum=-1<<30; pos[i]->minimum=1<<30; pos[i]->sum=0; pos[i]->idx=i; pos[i]->size=1; } } int main() { int u,v,w,Q; char order[15]={0}; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); Init(n); for (int i=1;i<n;i++) { read(u); read(v); read(w); pos[n+i]->val=pos[n+i]->maxv=pos[n+i]->minv=w; LCT.Link(pos[i+n],pos[++u]); LCT.Link(pos[i+n],pos[++v]); } read(Q); while (Q--) { read(order); read(u); read(v); if (!strcmp(order+1,"MAX")) { printf("%d\n",LCT.Max(pos[++u],pos[++v])); } else if (!strcmp(order+1,"MIN")) { printf("%d\n",LCT.Min(pos[++u],pos[++v])); } else if (!strcmp(order+1,"SUM")) { printf("%d\n",LCT.Sum(pos[++u],pos[++v])); } else if (!strcmp(order+1,"C")) { LCT.Change(pos[u+n],v); } else if (!strcmp(order+1,"N")) { LCT.Change(pos[++u],pos[++v]); } } return 0; }