You were given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3 ... N-1.
We'll ask you to perfrom some instructions of the following form:
- Change i ti : Change the cost of the i-th edge to Ti
Or
- QUERY a B : Ask for the maximum edge cost on the path from Node A to Node B
Input
The first line of input contains an integer T, the number of test cases (T <= 20). T test cases follow.
For each test case:
- The first line there are an integer n (n <= 10000),
- In the next N-1 lines, the i-th line describes the i-th edge:a line with three integers a b c denotes a N Edge between a, b of cost C (C<= 1000000),
- The next lines contain instructions "Change i ti" or "QUERY a B",
- The end of each test case was signified by the string "done".
There is one blank line between successive tests.
Output
For each "QUERY" operation, the write one integer representing its result.
Example
Input:131 2 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONEOutput:13
Template questions
2 Types of operation:
1. Change the Benquan of section I to V
2. Query path U,v the largest Benquan
Note: It is a side-right, so the U,v LCA cannot be included in the query.
Because the Benquan represented by LCA is not in the path of u,v
Tree chain split + segment tree (single point update, interval query)
The line tree doesn't even have to be lazy.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineLL Long Long#defineDebug printf ("Eeeeeeeeeeeeeeeeeeeee")#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intmaxn=1e4+Ten;Const intinf=0x3f3f3f3f;intDEP[MAXN];intSON[MAXN];intSIZ[MAXN];intTOP[MAXN];intFA[MAXN];intCHG[MAXN];intREV[MAXN];intCOST[MAXN];inte[maxn][3];structedge{intTo,next;}; Edge EDGE[MAXN<<1];intHEAD[MAXN];inttot;intma[maxn<<2];voidinit () {memset (head,-1,sizeofhead); Tot=0;}voidAddedge (intUintv) {edge[tot].to=v; Edge[tot].next=Head[u]; Head[u]=tot++;}voidSolveint );intMain () {inttest; scanf ("%d",&test); while(test--){ intN; Init (); scanf ("%d",&N); for(intI=1; i<n;i++) {scanf (" %d%d%d", &e[i][0],&e[i][1],&e[i][2]); Addedge (e[i][0],e[i][1]); Addedge (e[i][1],e[i][0]); } solve (n); } return 0;}voidDfs0 (intUintpre) {Fa[u]=Pre; Siz[u]=1; Dep[u]=dep[pre]+1; for(intI=head[u];~i;i=Edge[i].next) { intv=edge[i].to; if(v==pre)Continue; Dfs0 (V,u); Siz[u]+=Siz[v]; if(son[u]==-1|| Siz[v]>Siz[son[u]]) Son[u]=v; }}voidDFS1 (intUintTP) {Top[u]=TP; Chg[u]=++tot; Rev[tot]=T; if(son[u]==-1) return ; DFS1 (SON[U],TP); for(intI=head[u];~i;i=Edge[i].next) { intv=edge[i].to; if(V==fa[u] | | v==Son[u])Continue; DFS1 (V,V); }}voidPushup (intRT) {Ma[rt]=max (ma[rt<<1],ma[rt<<1|1]);}voidBuildintLintRintRT) { if(l==R) {Ma[rt]=Cost[rev[l]]; return ; } intM= (l+r) >>1; Build (Lson); Build (Rson); Pushup (RT);}voidUpdateintPintAddintLintRintRT) { if(l==R) {Ma[rt]=add; return ; } intM= (l+r) >>1; if(p<=m) update (P,add,lson); ElseUpdate (P,add,rson); Pushup (RT);}intQueryintLintRintLintRintRT) { if(L<=l && r>=R) { returnMa[rt]; } intM= (l+r) >>1; intret=-inf; if(l<=m) Ret=Max (Ret,query (L,r,lson)); if(r>m) Ret=Max (Ret,query (L,r,rson)); returnret;}intQuery_path (intUintv) { intret=-inf; while(top[u]!=Top[v]) { if(dep[top[u]]<Dep[top[v]) swap (U,V); RET=max (Ret,query (Chg[top[u]],chg[u),1Tot1)); U=Fa[top[u]]; } if(dep[u]<Dep[v]) swap (U,V); RET=max (Ret,query (chg[v]+1, Chg[u],1Tot1)); returnret;}voidSolveintN) {memset (DEP,0,sizeofDEP); memset (son,-1,sizeofson); memset (Cost,0,sizeofCost ); Dfs0 (1,1); Tot=0; DFS1 (1,1); for(intI=1; i<n;i++){ if(dep[e[i][0]]>dep[e[i][1]]) swap (e[i][0],e[i][1]); cost[e[i][1]]=e[i][2]; } Build (1Tot1); Charstr[ the]; while(SCANF ("%s", str)) { if(str[0]=='D') Break; intu,v; scanf ("%d%d",&u,&v); if(str[0]=='Q') {printf ("%d\n", Query_path (u,v)); } Else{update (chg[e[u][1]],v,1Tot1); } } return ;}
Spoj Query on a tree chain split water problem