POJ 2763 housewife Wind (tree chain split) (segment tree single point modification)

Source: Internet
Author: User

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 10378 Accepted: 2886

Description

After their royal wedding, Jiajia and wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There is some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village are so special, we can reach any and huts starting from an arbitrary hut. If Each road cannot was walked along twice, then the route between every pair is unique.

Since Jiajia earned enough, Wind became a housewife. Their children loved to go to other kids, then make a simple call to wind: ' Mummy, take Me home! '

At different times, the time needed to walk along a road could be different. For example, Wind takes 5 minutes on a road normally, but could take the minutes if there is a lovely little dog to play with , or take 3 minutes if there is some unknown strange smell surrounding the road.

Wind loves his children, so she would like to tell her children the exact time she'll spend on the roads. Can you help her?

Input

The first line contains three integers n, q, S. There was n huts in XX Village, q messages to process, and wind was currently in Hut S. N < 100001, q < 100001.

The following n-1 lines each contains three integers a, B and W. That means there was a road directly connecting hut A and B, time required is W. 1<=w<= 10000.

The following q lines is one of the following and the types:

Message a:0 u
A kid in Hut U calls wind. She should go to hut u from hers current position.
Message B:1 i W
The time required for I-th Road was changed to W. Note that the time change would not be happen when wind was on her road. The changed can only happen if wind was staying somewhere, waiting to take the next kid.

Output

For each message A, print an integer X, the time required to take the next child.

Sample Input

3 3 11 2 12 3 20 21 2 30 3

Sample Output

13

Tree chain split water problem. It is recommended to use C + + on POJ and g++ may time out.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineMet (b) memset (a,b,sizeof a)#definePB Push_back#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespaceStd;typedefLong Longll;Const intn=2e5+ -;Const intm=n*n+Ten;intDep[n],siz[n],fa[n],id[n],son[n],val[n],top[n];//Top recent heavy chain parent nodeintnum,s,m,n,q;intsum[n*2],tre[2*N];vector<int>V[n];structTree {intX,y,val; voidRead () {scanf ("%d%d%d",&x,&y,&val); }}e[n];voidDFS1 (intUintFintd) {Dep[u]=D; Siz[u]=1; Son[u]=0; Fa[u]=F;  for(inti =0; I < v[u].size (); i++) {        intFF =V[u][i]; if(ff = = f)Continue; DFS1 (FF, u, D+1); Siz[u]+=SIZ[FF]; if(Siz[son[u]) <SIZ[FF]) Son[u]=FF; }}voidDFS2 (intUintTP) {Top[u]=TP; Id[u]= ++num; if(Son[u]) DFS2 (Son[u], TP);  for(inti =0; I < v[u].size (); i++) {        intFF =V[u][i]; if(ff = = Fa[u] | | ff = = Son[u])Continue;    DFS2 (FF, FF); }}inlinevoidPushplus (intRT) {Sum[rt]=sum[rt*2]+sum[rt*2+1];}voidBuild (intLintRintRT) {    if(l==R) {Sum[rt]=Val[l]; return; }    intM= (l+r) >>1;    Build (Lson);    Build (Rson);    Pushplus (RT); //printf ("rt=%d sum[rt]=%d\n", Rt,sum[rt]);}voidUpdate (intPintAddintLintRintRT) {    if(l==R) {Sum[rt]=add; return; }    intM= (r+l) >>1; if(p<=m)    Update (P,add,lson); ElseUpdate (P,add,rson); Pushplus (RT);}intQuery (intLintRintLintRintRT) {    if(L&LT;=L&AMP;&AMP;R&LT;=R)returnSum[rt]; intM= (l+r) >>1; intans=0; if(l<=m) ans+=Query (L,r,lson); if(r>m) ans+=Query (L,r,rson); returnans;}intYougth (intUintv) {intTP1 = Top[u], TP2 =Top[v]; intAns =0;  while(TP1! =TP2) {        if(DEP[TP1] <DEP[TP2])            {Swap (TP1, TP2);        Swap (U, v); } ans+ = Query (ID[TP1], Id[u],1N1); U=FA[TP1]; TP1=Top[u]; }    if(U = = v)returnans; if(Dep[u] >Dep[v]) Swap (U, v); Ans+ = Query (Id[son[u], id[v],1N1); returnans;}voidClear (intN) { for(intI=1; i<=n; i++) V[i].clear ();}intMain () {intu,vv,w; scanf ("%d%d%d",&n,&q,&s);  for(intI=1; i<n; i++) {e[i].read ();        V[e[i].x].push_back (E[I].Y);    V[e[i].y].push_back (e[i].x); } num=0; DFS1 (1,0,1); DFS2 (1,1);  for(inti =1; I < n; i++) {        if(Dep[e[i].x] <dep[e[i].y]) swap (e[i].x, E[I].Y); Val[id[e[i].x]]=E[i].val; } Build (1Num1);  while(q--) {        intx; scanf ("%d",&x); if(!x) {scanf ("%d",&u); printf ("%d\n", Yougth (s,u)); S=u; }        Else{scanf ("%d%d",&u,&vv); Update (ID[E[U].X],VV,1N1);    }} Clear (n); return 0;}


POJ 2763 housewife Wind (tree chain split) (segment tree single point modification)

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.