"POJ3237" Tree
Description
You is given a tree with N nodes. The tree ' s nodes is numbered 1 through n and its edges is numbered 1 through N ? 1. Each edge was associated with a weight. Then you is to execute a series of instructions on the tree. The instructions can be one of the following forms:
CHANGE i v |
Change the weight of the i-th edge to v |
NEGATE a b |
Negate the weight of every edge on the path from a to b |
QUERY a b |
Find the maximum weight of edges on the path from a to b |
Input
The input contains multiple test cases. The first line of input contains an integer t (T ≤20), the number of test cases. Then follow the test cases.
Each test case was preceded by a empty line. The first nonempty line of its contains n (n ≤10,000). The next N ? 1 lines each contains three integers a, b and C, describing an edge connecting nodes a and b with weight c. The edges is numbered in the order they appear in the input. Below them is the instructions, each sticking to the specification above. A lines with the word " DONE
" ends the test case.
Output
For each " QUERY
" instruction, the output of the result on a separate line.
Sample Input
131 2 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONE
Sample Output
13
Problem: Tree cut + line tree naked, where the opposite number of operations is to exchange the maximum and minimum value of the opposite number of the difference is good
#include <stdio.h>#include<string.h>#include<iostream>#defineLson x<<1#defineRson x<<1|1using namespaceStd;typedefLong Longll;Const intmaxn=10010;intN,cnt,tot;intsm[maxn<<2],sn[maxn<<2],tag[maxn<<2];intto[maxn<<1],next[maxn<<1],val[maxn<<1],V[MAXN],U[MAXN],FA[MAXN],HEAD[MAXN];intDEEP[MAXN],SIZE[MAXN],SON[MAXN],P[MAXN],TOP[MAXN];Charstr[Ten];intReadin () {intret=0, sig=1;CharGC; while(gc<'0'|| Gc>'9') sig= (gc=='-')?-1:1, gc=GetChar (); while(gc>='0'&&gc<='9') ret=ret*Ten+gc-'0', gc=GetChar (); returnret*Sig;}voidAddintAintBintc) {to[cnt]=b; VAL[CNT]=C; NEXT[CNT]=Head[a]; Head[a]=cnt++;}voidDFS1 (intx) {Size[x]=1; for(inti=head[x];i!=-1; i=Next[i]) { if(to[i]!=Fa[x]) {Fa[to[i]]=x; U[to[i]]=Val[i]; Deep[to[i]]=deep[x]+1; DFS1 (To[i]); SIZE[X]+=Size[to[i]]; if(Size[to[i]]>size[son[x]]) son[x]=To[i]; } }}voidDFS2 (intXintTP) {Top[x]=TP; P[X]=++tot; V[P[X]]=U[x]; if(Son[x]) DFS2 (SON[X],TP); for(inti=head[x];i!=-1; i=Next[i])if(to[i]!=son[x]&&to[i]!=fa[x]) DFS2 (To[i],to[i]);}voidPushup (intx) {Sm[x]=Max (Sm[lson],sm[rson]); SN[X]=min (Sn[lson],sn[rson]);}voidPushdown (intx) { if(Tag[x]) {swap (Sm[lson],sn[lson]), swap (Sm[rson],sn[rson]); Sm[lson]=-sm[lson],sn[lson]=-sn[lson],sm[rson]=-sm[rson],sn[rson]=-Sn[rson]; Tag[lson]^=1, tag[rson]^=1; TAG[X]=0; }}voidBuildintLintRintx) { if(l==R) {Sm[x]=sn[x]=V[l]; return ; } intMid=l+r>>1; Build (L,mid,lson), Build (mid+1, R,rson); Pushup (x);}voidUpdata (intLintRintXintAintb) { if(l==R) {Sm[x]=sn[x]=b; return ; } pushdown (x); intMid=l+r>>1; if(a<=mid) Updata (L,MID,LSON,A,B); ElseUpdata (mid+1, r,rson,a,b); Pushup (x);}voidUpgate (intLintRintXintAintb) { if(a<=l&&r<=b) {tag[x]^=1; Swap (sm[x],sn[x]); SM[X]=-sm[x],sn[x]=-Sn[x]; return ; } pushdown (x); intMid=l+r>>1; if(b<=mid) Upgate (L,MID,LSON,A,B); Else if(A>mid) Upgate (mid+1, r,rson,a,b); ElseUpgate (L,mid,lson,a,b), Upgate (mid+1, r,rson,a,b); Pushup (x);}intQueryintLintRintXintAintb) { if(a<=l&&r<=b)returnSm[x]; Pushdown (x); intMid=l+r>>1; if(B<=mid)returnquery (L,MID,LSON,A,B); if(A>mid)returnQuery (mid+1, r,rson,a,b); returnMax (Query (l,mid,lson,a,b), query (mid+1, R,rson,a,b));}voidChange () {intX=readin (), y=Readin (); Updata (1N1, Max (p[to[x*2-2]],p[to[x*2-1]]), y);}voidfan () {intX=readin (), y=Readin (); while(top[x]!=Top[y]) { if(deep[top[x]]<Deep[top[y]]) swap (x, y); Upgate (1N1, p[top[x]],p[x]); X=Fa[top[x]]; } if(deep[x]>Deep[y]) swap (x, y); if(x!=y) Upgate (1N1, p[x]+1, P[y]);}voidGetmax () {intX=readin (), Y=readin (), ans=1<< to; while(top[x]!=Top[y]) { if(deep[top[x]]<Deep[top[y]]) swap (x, y); Ans=max (Ans,query (1N1, p[top[x]],p[x])); X=Fa[top[x]]; } if(deep[x]>Deep[y]) swap (x, y); if(x!=y) Ans=max (Ans,query (1N1, p[x]+1, P[y])); printf ("%d\n", ans);}voidWork () {n=Readin (); memset (Head,-1,sizeof(head)); memset (son,0,sizeof(son)); memset (FA,0,sizeof(FA)); memset (Tag,0,sizeof(tag)); CNT=tot=0; intI,a,b,c; for(i=1; i<n;i++) {a=readin (), B=readin (), c=Readin (); Add (A,b,c), add (B,A,C); } deep[1]=1; DFS1 (1); DFS2 (1,1); Build (1N1); while(SCANF ("%s", str)) { Switch(str[0]) { Case 'C': Change (); Break; Case 'N': Fan (); Break; Case 'Q': Getmax (); Break; Case 'D':return ; } }}intMain () {intT; scanf ("%d",&t); while(t--) work (); return 0;}
"POJ3237" Tree chain split + segment Trees