Lightoj 1348 Aladdin and the Return Journey

Source: Internet
Author: User

Aladdin and the Return journeytime limit:2000msmemory limit:32768kbthis problem would be judged onLightoj. Original id:1348
64-bit integer IO format: %lld Java class name: Main

Finally The great magical Lamp is in Aladdin ' s hand. Now he wanted to return home. But he didn ' t want-to-take all help from the Genie because he thought that it might is another adventure for him. All he remembered is the paths he had taken to reach there. But since he took the lamp, all the genies in the cave became angry and they were planning to attack. As Aladdin is not afraid, he wondered how many genies were there. He summoned the Genie from the lamp and asked.

Now is given a similar problem. For simplicity assume so, you is given a tree (a connected graph with no cycles) with n nodes, nodes represent places, Edges represent roads. In each node, initially there is an arbitrary number of genies. But the numbers of genies. So, you is given a tree, the number of genies in each node and several queries of the types. They is:

    1. 0 I J, it means that's the to find the total number of genies in the nodes that occur in path from node I to J (0≤i, J < N).
    2. 1 I v, it means that number of genies in node I was changed to V (0≤i < n, 0≤v≤1000).
Input

Input starts with an integer T (≤10), denoting the number of test cases.

Each case starts with a blank line. Next line contains an integer n (2≤n≤30000). The next line contains n space separated integers between 0 and , denoting the number of GE NIEs in the nodes respectively. Then there is n-1 lines each containing the integers: u v (0≤u, v < n, u≠v) meaning that there I s an edge from node u and v. Assume that the edges form a valid tree. Next line contains an integer q (1≤q≤105) followed by Q lines each containing a query as described a Bove.

Output

For each case, print the case number in a. Then for each query 0 i J, print the total number of genies in the nodes so occur in path i to j .

Sample Input

1

4

10 20 30 40

0 1

1 2

1 3

3

0 2 3

1 1 100

0 2 3

Output for Sample Input

Case 1:

90

170

Problem solving: tree-chain split

1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =30010;4 structArc {5     intTo,next;6Arcintx =0,inty =-1) {7to =x;8Next =y;9     }Ten} e[maxn<<1]; One intHEAD[MAXN],FA[MAXN],TOP[MAXN],DEP[MAXN]; A intSiz[maxn],son[maxn],loc[maxn],clk,tot; - intC[MAXN]; - voidAddintUintv) { theE[tot] =arc (V,head[u]); -Head[u] = tot++; - } - voidUpdateintIintval) { +      while(I <=CLK) { -C[i] + =Val; +  Ai + = i&-i; at     } - } - intSumintIintRET =0) { -      while(I >0) { -RET + =C[i]; -I-= i&-i; in         //cout<<ret<< "++++" <<endl; -     } to     returnret; + } - voidFindheavyedge (intUintFatherintdepth) { theFa[u] =father; *Dep[u] =depth; $Siz[u] =1;Panax NotoginsengSon[u] =-1; -      for(inti = Head[u]; ~i; i =E[i].next) { the         if(e[i].to = = father)Continue; +Findheavyedge (e[i].to,u,depth +1); ASiz[u] + =siz[e[i].to]; the         if(Son[u] = =-1|| Siz[e[i].to] >Siz[son[u]]) +Son[u] =e[i].to; -     } $ } $ voidConnectheavyedge (intUintancestor) { -Top[u] =ancestor; -Loc[u] = + +CLK; the     if(Son[u]! =-1) Connectheavyedge (son[u],ancestor); -      for(inti = Head[u]; ~i; i =E[i].next) {Wuyi         if(e[i].to = = Fa[u] | | son[u] = = e[i].to)Continue; the Connectheavyedge (e[i].to,e[i].to); -     } Wu } - intSolveintUintVintRET =0) { About      while(Top[u]! =Top[v]) { $         if(Dep[top[u]) <Dep[top[v]) swap (U,V); -RET + = SUM (loc[u])-SUM (loc[top[u]]-1); -U =Fa[top[u]]; -     } A     if(Dep[u] >Dep[v]) swap (U,V); +RET + = SUM (loc[v])-SUM (loc[u]-1); the     returnret; - } $ intVAL[MAXN]; the intMain () { the     intKase,n,m,u,v,op,cs =1; thescanf"%d",&Kase); the      while(kase--) { -scanf"%d",&n); in          for(inti =1; I <= N; ++i) scanf ("%d", Val +i); theCLK = tot =0; thememset (head,-1,sizeofhead); About          for(inti =1; I < n; ++i) { thescanf"%d%d",&u,&v); theAdd (u+1, v+1); theAdd (v+1, u+1); +         } -Findheavyedge (1,0,0); theConnectheavyedge (1,1);BayiMemset (c,0,sizeofc); the          for(inti =1; I <= N; ++i) the Update (loc[i],val[i]); -scanf"%d",&m); -printf"Case %d:\n", cs++); the          while(m--) { thescanf"%d%d%d",&op,&u,&v); the             if(OP) { the                 intTMP = SUM (loc[u+1])-SUM (loc[u+1]-1); -Update (loc[u+1],-tmp +v); the}Elseprintf"%d\n", Solve (u+1, v+1)); the         } the     }94     return 0; the}
View Code

Lightoj 1348 Aladdin and the Return Journey

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.