1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intmaxn=100000+Ten, inf=-1u>>1;7 intv[maxn],maxv[maxn],minv[maxn],sumv[maxn],ch[maxn][2],pre[maxn],top[maxn],flip[maxn],n,q;8InlineintRead () {9 intx=0, sig=1;CharCh=GetChar ();Ten while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} One while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); A returnx*=Sig; - } -InlinevoidWriteintx) { the if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; - for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } + voidMaintain (into) { - intlc=ch[o][0],rc=ch[o][1]; +maxv[o]=Max (MAXV[LC],MAXV[RC]); Aminv[o]=min (minv[lc],minv[rc]); atsumv[o]=sumv[lc]+SUMV[RC]; - if(V[o]) { -sumv[o]+=V[o]; -maxv[o]=Max (Maxv[o],v[o]); -minv[o]=min (Minv[o],v[o]); -}return; in } - voidPushdown (into) { to if(Flip[o]) { +flip[ch[o][0]]^=1; -flip[ch[o][1]]^=1; theSwap (ch[o][0],ch[o][1]); *flip[o]=0; $}return;Panax Notoginseng } - voidRotateintXintd) { the pushdown (x); + inty=pre[x],z=Pre[y]; Ach[y][d^1]=CH[X][D];p re[ch[x][d]]=y; thech[z][ch[z][1]==y]=x;pre[x]=Z; +ch[x][d]=y;pre[y]=x; -Maintain (y);return; $ } $ voidSplay (intx) { - intrt=x; - while(Pre[rt]) rt=Pre[rt]; the if(x!=RT) { -top[x]=top[rt];top[rt]=0;Wuyi while(Pre[x]) { the pushdown (pre[x]); -Rotate (x,ch[pre[x]][0]==x); Wu } maintain (x); -}Elsepushdown (x); About return; $ } - voidAccessintx) { - inty=0; - while(x) { A splay (x); +top[ch[x][1]]=x;pre[ch[x][1]]=0; thech[x][1]=y; -top[y]=0;p re[y]=x; $ maintain (x); they=x;x=Top[x]; the}return; the } the voidMakeroot (intx) { -Access (x); splay (x); flip[x]^=1;return; in } the voidLinkintUintv) { theMakeroot (u); top[u]=v;return; About } the voidQueryintXinty) { the if(x==y) { thePuts"Error");return; + } - makeroot (x); Access (y); splay (y) ; theWrite (Maxv[y]);p Utchar (' '); write (Minv[y]);p Utchar (' '); write (Sumv[y]);p Utchar ('\ n');Bayi return; the } the voidUpdateintPosintCV) { -Splay (POS); v[pos]=CV; -Maintain (POS);return; the } the voidinit () { themaxv[0]=-inf;minv[0]=inf;sumv[0]=0; then=read (); - for(intI=1; i<n;i++){ the intA=read (), B=read (), c=read (); thev[i+n]=C; theLink (a,i+n); Link (i+n,b);94 } theq=read (); the while(q--) the {98 intTp=read (), A=read (), b=read (); About if(TP) query (a, b); - ElseUpdate (A +n,b);101 }102 return ;103 }104 intMain () {init ();return 0;}
Dynamic Tree LCT