[hdu4010]: Query on the Trees

Source: Internet
Author: User

It's probably the worst tune ever made LCT. Because there is a lack of a condition when judging whether it is lawful to delete the edge. Tune Up all Night >_< ....

By the template problem teaches a man qaq.

1#include <cstdio>2#include <iostream>3#include <cstring>4 using namespacestd;5 Const intmaxn=300233;6 structzs{7     intToo,pre;8}e[maxn<<1];intTOT,LAST[MAXN],DL[MAXN];9 intch[maxn][2],fa[maxn],mxv[maxn],v[maxn],add[maxn],st[maxn],top;Ten BOOLREV[MAXN]; One inti,j,k,n,m,x,y,w; A  - intRaCharRx; -InlineintRead () { theRx=getchar (), ra=0; -      while(rx<'0'|| Rx>'9') rx=GetChar (); -      while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnRA; - } +  -InlinevoidInsertintAintb) { +e[++tot].too=b,e[tot].pre=last[a],last[a]=tot; Ae[++tot].too=a,e[tot].pre=last[b],last[b]=tot; at } -  -InlineintMaxintAintb) {returnA>b?a:b;} -InlineBOOLISRT (intx) { -     returnch[fa[x]][0]!=x&&ch[fa[x]][1]!=x; - } inInlinevoidPushdown (intx) { -     intl=ch[x][0],r=ch[x][1]; to     if(Rev[x]) { +Swap (ch[x][0],ch[x][1]), -rev[l]^=1, rev[r]^=1, rev[x]=0; the     } *     if(Add[x]) { $         if(l) add[l]+=add[x],mxv[l]+=add[x],v[l]+=Add[x];Panax Notoginseng         if(r) add[r]+=add[x],mxv[r]+=add[x],v[r]+=Add[x]; -add[x]=0; the     } + } AInlinevoidUpdintx) { theMxv[x]=max (mxv[ch[x][0]],mxv[ch[x][1]]); +     if(Mxv[x]<v[x]) mxv[x]=V[x]; - } $InlinevoidRotateintx) { $     intf=fa[x],gfa=fa[f],l=ch[f][1]==x,r=l^1; -     if(!ISRT (f)) ch[gfa][ch[gfa][1]==f]=x; -fa[ch[f][l]=ch[x][r]]=f,fa[fa[ch[x][r]=f]=x]=GFA, the upd (f); - }WuyiInlinevoidSplay (intx) { the     intf=X,gfa; -      for(st[top=1]=f;! ISRT (f);) st[++top]= (f=fa[f]); Wu      while(top) Pushdown (st[top--]); -      while(!isrt (x)) { AboutF=fa[x],gfa=Fa[f]; $         if(!isrt (f)) -Rotate (((ch[f][1]==x) ^ (ch[gfa][1]==f))?x:f); - rotate (x); -     } A upd (x); + } theInlinevoidAccessintx) { -      for(intRc=0; x;rc=x,x=Fa[x]) $Splay (x), ch[x][1]=rc,upd (x); the } theInlinevoidMakert (intx) { theAccess (x), splay (x), rev[x]^=1; the } -InlinevoidLinkintXinty) { inMakert (x), fa[x]=y; the } theInlinevoidCutintXinty) { AboutMakert (x), Access (y), splay (y), fa[ch[y][0]]=0, ch[y][0]=0, upd (y); the } theInlineintGETFA (intx) { the      for(Access (x), splay (x); ch[x][0];x=ch[x][0]); +     returnx; - } the Bayi intMain () { themxv[0]=-200023333;BOOLfirst=1; the      while(SCANF ("%d", &n) = =1){ -         if(!First ) -              for(i=0; i<=n;i++) rev[i]=add[i]=ch[i][0]=ch[i][1]=last[i]=0; the         Elsefirst=0; thetot=0; the          the          for(i=1; i<n;i++) -X=read (), y=read (), insert (x, y); the          for(i=1; i<=n;i++) v[i]=mxv[i]=read (); the         intL=0, r=1, now;dl[1]=1; fa[1]=0; the         94          while(l<R) { thenow=dl[++l]; the              for(I=last[now];i;i=e[i].pre)if(e[i].too!=Fa[now]) thefa[dl[++r]=e[i].too]=Now ;98         } About          -M=read ();CharID;101          while(m--){102              for(Id=getchar ();id<'0'|| Id>'9'; id=GetChar ());103             if(id=='3') w=read ();104X=read (), Y=read ();BOOLSm= (GETFA (x) = =GETFA (y)); the             if(id=='1')106                 if(SM) puts ("-1");107                 Elselink (x, y);108             if(id=='2')109                 if(!sm| | X==y) puts ("-1"); the                 ElseCut (x, y);111             if(id=='3') the                 if(!SM) puts ("-1");113                 ElseMakert (x), Access (y), splay (y), add[y]+=w,mxv[y]+=w,v[y]+=W; the             if(id=='4') the                 if(!SM) puts ("-1"); the                 ElseMakert (x), Access (y), splay (y), printf ("%d\n", Mxv[y]);117         }118Puts"");119     } -     return 0;121}
View Code

[hdu4010]: Query on the Trees

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.