bzoj1984 month under "Mao Jingxiu"

Source: Internet
Author: User

Description

Caterpillar after a timely deformation, and finally escaped the robbery, left the vegetable garden. Caterpillar through the mountains and rivers, through hardships, and finally came to a small Shaoxing campus. Climb, crawl, climb, crawl ~ ~ Caterpillar climbed to a small "Mao Jingxiu" below, found that the tree has his favorite Mao Maogo ~ ~ ~ "Mao Jingxiu" on there are N nodes and N-1 branches, but there is no Mao Maogo on the node, Mao Maogo are long on the branches. But this "Mao Jingxiu" has magical magic, he can change the number of hairs on the branches of the tree:? Change K W: Changes the number of hairs on section K branches to W.? Cover u v w: Change the number of hairs on the branch between node U and node v to W.? Add U v w: increases the number of hairs on the branch between node U and Node v by W. Since the caterpillar is greedy, he will have the following questions:? Max U V: asks the maximum number of hairs on the branch between the node U and Node v.

Input

The first line is a positive integer n. Next N-1 line, three positive integers per line ui,vi and WI, section i+1 describes article I branches. Indicates that the branch I is connected to the node UI and Node VI, the branches have wi a fluffy fruit. Next is the action and the inquiry, ending with "Stop".

Output

For each inquiry operation of the caterpillar, output an answer.

Tree chain split + segment tree maintenance interval max, interval overlay marker, interval increment marker

#include <cstdio>Charbuf[10000005],*ptr=buf-1; inlineintinput () {intx=0, c=*++ptr,f=1;  while(c> $|| c< -){if(c=='-') f=-1; c=*++ptr;}  while(c> -&&c< -) x=x*Ten+c- -, c=*++ptr; returnx*F;} InlineintGetop () {intc=*++ptr;  while(c< -) c=*++ptr; return*++ptr;}Const intn=100050;intmx[n*2],a[n*2],lc[n*2],rc[n*2],p=0, N;BOOLcov[n*2];intes[n*2],enx[n*2],ev[n*2],e0[n],v0[n],ep=2;intL,r,ans;intfa[n],dep[n],son[n],sz[n],top[n],v[n],id[n],ei[n][2],idr[n],idp=1; inlineintMaxintAintb) {returnA>b?a:b;}voidDwnintW) {    if(A[w]) {if(Lc[w]) {A[lc[w]]+=A[w]; MX[LC[W]]+=A[w]; A[RC[W]]+=A[w]; MX[RC[W]]+=A[w]; } A[w]=0; }    if(Cov[w]) {if(Lc[w]) {Cov[lc[w]]=cov[rc[w]]=1; MX[LC[W]]=mx[rc[w]]=Mx[w]; } Cov[w]=0; }}inlinevoidUpintW) {Mx[w]=Max (mx[lc[w]],mx[rc[w]);}intBuildintLintR) {    intu=++p; if(l==R) {Mx[u]=V0[idr[l]]; returnu; }    intM=l+r>>1; Lc[u]=build (L,M); Rc[u]=build (m+1, R);    Up (U); returnu;}voidGetmax (intWintLintR) {    if(l<=l&&r<=R) {ans=Max (ans,mx[w]); return;    } dwn (W); intM=l+r>>1; if(l<=M) Getmax (lc[w],l,m); if(r>m) Getmax (rc[w],m+1, R);}voidChgintWintLintR) {    if(l<=l&&r<=R) {A[w]=0; COV[W]=1; MX[W]=ans; return;    } dwn (W); intM=l+r>>1; if(l<=M) Chg (lc[w],l,m); if(r>m) Chg (rc[w],m+1, R); Up (w);}voidInc (intWintLintR) {    if(l<=l&&r<=R) {        if(!cov[w]) a[w]+=ans; MX[W]+=ans; return;    } dwn (W); intM=l+r>>1; if(l<=M) Inc (LC[W],L,M); if(R&GT;M) Inc (rc[w],m+1, R); Up (w);}voidF1 (intWintPA) {Fa[w]=PA; DEP[W]=dep[pa]+1; SZ[W]=1;  for(intI=e0[w];i;i=Enx[i]) {        intu=Es[i]; if(U==PA)Continue; V0[u]=Ev[i];        F1 (U,W); SZ[W]+=Sz[u]; if(Sz[u]>sz[son[w]]) son[w]=u; }}voidF2 (intWintTP) {Top[w]=TP; IDR[ID[W]=idp++]=W; if(Son[w]) F2 (SON[W],TP);  for(intI=e0[w];i;i=Enx[i]) {        intu=Es[i]; if(u!=fa[w]&&u!=son[w]) F2 (u,u); }}voidQmaxintXinty) {ans=0; inta=top[x],b=Top[y],c;  while(a!=b) {        if(Dep[a]<dep[b]) c=a,a=b,b=c,c=x,x=y,y=C; L=id[a];r=Id[x]; Getmax (1,1, N); X=fa[a];a=Top[x]; }    if(Dep[x]<dep[y]) l=id[x]+1, r=Id[y]; Elsel=id[y]+1, r=Id[x]; if(l<=r) Getmax (1,1, N); printf ("%d\n", ans);}voidAddintXinty) {    inta=top[x],b=Top[y],c;  while(a!=b) {        if(Dep[a]<dep[b]) c=a,a=b,b=c,c=x,x=y,y=C; L=id[a];r=Id[x]; Inc (1,1, N); X=fa[a];a=Top[x]; }    if(Dep[x]<dep[y]) l=id[x]+1, r=Id[y]; Elsel=id[y]+1, r=Id[x]; if(L&LT;=R) Inc (1,1, n);}voidChangeintXinty) {    inta=top[x],b=Top[y],c;  while(a!=b) {        if(Dep[a]<dep[b]) c=a,a=b,b=c,c=x,x=y,y=C; L=id[a];r=Id[x]; CHG (1,1, N); X=fa[a];a=Top[x]; }    if(Dep[x]<dep[y]) l=id[x]+1, r=Id[y]; Elsel=id[y]+1, r=Id[x]; if(l<=r) Chg (1,1, n);}intMain () {fread (buf,1,10000000, stdin); N=input ();  for(intI=1; i<n;i++){        intA=input (), B=input (), c=input (); ES[EP]=b;enx[ep]=e0[a];ev[ep]=c;e0[a]=ep++; ES[EP]=a;enx[ep]=e0[b];ev[ep]=c;e0[b]=ep++; ei[i][0]=A; ei[i][1]=C; } F1 (1,0); F2 (1,1); Build (1, N);  while(1){        intop=Getop (); if(op=='T') Break; if(op=='a'){            intA=input (), b=input ();        Qmax (A, b); }Else if(op=='h'){            intA=input (), b=input (); L=r= (dep[ei[a][0]]>dep[ei[a][1]]?id[ei[a][0]]:id[ei[a][1]]); Ans=b; CHG (1,1, N); }Else if(op=='D'){            intA=input (), B=input (), c=input (); Ans=C;        Add (A, b); }Else{            intA=input (), B=input (), c=input (); Ans=C;        Change (A, b); }    }    return 0;}

bzoj1984 month under "Mao Jingxiu"

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.