POJ 2763 Housewife Wind

Source: Internet
Author: User

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 5798 Accepted: 1455

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 are changed to W. is on her. 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, edge right, single point update, interval query


#include <cstdio>#include<queue>#include<algorithm>#include<iostream>#include<cstring>using namespacestd;#defineRoot 1,n,1#defineLR rt<<1#defineRR rt<<1|1#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1typedefLong LongLL;Const intn=100100;intn,q,s;inteh[n],et[n<<2],nxt[n<<2],w[n<<2],tot=0;intPos,p[n],top[n],rnk[n],dep[n],fa[n],son[n],siz[n];structnode{intU,v,w;} E[n<<2];voidinit () {memset (Eh,-1,sizeof(EH)); memset (son,-1,sizeof(son)); Tot=0; POS=0;}voidAddedge (intUintv) {Et[tot]=v;nxt[tot]=eh[u];eh[u]=tot++; Et[tot]=u;nxt[tot]=eh[v];eh[v]=tot++;}voidDFS1 (intUintFatherintd) {Dep[u]=D; Fa[u]=father; Siz[u]=1;  for(intI=eh[u];~i;i=Nxt[i]) {        intv=Et[i]; if(V! =father) {DFS1 (v,u,d+1); Siz[u]+=Siz[v]; if(son[u]==-1|| SIZ[V] >Siz[son[u]]) Son[u]=v; }    }}voidDFS2 (intUintTP) {Top[u]=TP; P[u]= ++POS; rnk[P[u]]=u; if(Son[u] = =-1)return ;    DFS2 (Son[u], TP);  for(intI=eh[u]; ~i; i=Nxt[i]) {        intv=Et[i]; if(v! = Son[u] && v!=Fa[u])        {DFS2 (v,v); }    }}intsum[n<<2 ];voidUp (intRT) {Sum[rt]=sum[lr]+SUM[RR];}voidBuildintLintRintRT) {    if(L = =R) {Sum[rt]=0; return ; }    intM= (l+r) >>1;    Build (Lson);    Build (Rson); Up (RT);}voidUpdateintLintRintRtintXintv) {    if(L = =R) {Sum[rt]=v; return ; }    intM= (l+r) >>1; if(x<=m) update (LSON,X,V); if(x>m) update (RSON,X,V); Up (RT);}intQueryintLintRintRtintLintR) {    intres=0; if(l<=l&&r<=R) {        returnSum[rt]; }    intM= (l+r) >>1; if(L <=m) Res+=query (LSON,L,R); if(R >m) Res+=query (RSON,L,R); returnRes;}intQ (intUintv) {    intres=0; intF1=top[u],f2=Top[v];  while(F1! =F2) {        if(Dep[f1] <Dep[f2])            {swap (F1,F2);        Swap (U,V); } Res+=query (Root,p[f1],p[u]); U=FA[F1]; F1=Top[u]; }    if(U==V)returnRes; if(dep[u]>Dep[v]) swap (U,V); Res+=query (Root,p[son[u]],p[v]); returnRes;}intrun () {intu,v,w; intop;  while(~SCANF ("%d%d%d",&n,&q,&s))        {init ();  for(intI=0; i<n-1;++i) {scanf ("%d%d%d",&e[i].u,&e[i].v,&E[I].W);            Addedge (E[I].U,E[I].V); } DFS1 (1,0,0); DFS2 (1,1);        Build (root);  for(intI=0; i<n-1;++i) {if(Dep[e[i].u] >DEP[E[I].V]) swap (E[I].U,E[I].V);        Update (ROOT,P[E[I].V],E[I].W); }         while(q--) {scanf ("%d",&op); if( !op) {scanf ("%d",&u); printf ("%d\n", Q (s,u)); S=u; }            Else{scanf ("%d%d",&v,&W); Update (root,p[e[v-1].v],w); }        }    }    return 0;}intMain () {//freopen ("In.txt", "R", stdin);Ios::sync_with_stdio (0); returnrun ();}

POJ 2763 Housewife Wind

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.