Poj2763--housewife Wind (tree chain split + line tree)

Source: Internet
Author: User

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 6898 Accepted: 1742

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 of template problems, do a good job segment tree, is a simple single-point update, the region query

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define MAXN 110000# Define Lmin 1#define rmax n#define root lmin,rmax,1#define lson L, (l+r)/2,rt<<1#define Rson (l+r)/2+1,r,rt<<    1|1#define now l,r,rt#define int_now int l,int r,int rtstruct node{int u, V, W; int next;} Edge[maxn<<1];int HEAD[MAXN], VIS[MAXN], CNT;//storage tree, VIS tag node int belong[maxn];//Record sub-node (u,v) in the side of the I-bar vint P[MAXN];// The length of the branch of the child node is v int cl[maxn<<2], S[MAXN];//the line tree array cl,s record the value of a twig of 1 to N, provided to CL to establish a line segment tree int NUM[MAXN];//The number of nodes in a subtree with node u as the root of int dep [MAXN];//The depth of the node u, the depth of the root is 1int TOP[MAXN];//u is located at the top node of the heavy chain int SON[MAXN], FA[MAXN];//u's heavy son node, U's father node, int w[maxn], step;//Record section    Point V with the father's edge at the position of the segment tree int n, m, t; void Add (int u,int v,int w) {edge[cnt].u = u;    EDGE[CNT].V = v;    EDGE[CNT].W = W;    Edge[cnt].next = Head[u];    Head[u] = cnt++;    edge[cnt].u = v;    EDGE[CNT].V = u;    EDGE[CNT].W = W;    Edge[cnt].next = Head[v];    HEAD[V] = cnt++; return;} void Dfs1 (int u) {int I, V, max1 =-1, k =-1;    Num[u] = 1;        for (i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v;        if (Vis[v]) continue;        Belong[i/2+1] = v;        VIS[V] = 1;        DEP[V] = Dep[u] + 1;        FA[V] = u;        P[V] = EDGE[I].W;        DFS1 (v);            if (Num[v] > max1) {max1 = Num[v];        K = v;    }} Son[u] = k; return;}    void dfs2 (int u) {if (son[u] = =-1) return;    int I, V = son[u];    VIS[V] = 1;    W[V] = step;    s[step++] = P[v];    TOP[V] = Top[u];    DFS2 (v);        for (i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v;        if (Vis[v]) continue;        VIS[V] = 1;        W[V] = step;        s[step++] = P[v];        TOP[V] = v;    DFS2 (v); } return;    void Dfs () {memset (vis,0,sizeof (VIS));    VIS[1] = 1;    DEP[1] = 1;    FA[1] =-1;    DFS1 (1);    memset (vis,0,sizeof (VIS));    VIS[1] = 1;    Step = 1; TOP[1] = 1;    DFS2 (1); return;}    void Push_up (Int_now) {Cl[rt] = cl[rt<<1] + cl[rt<<1|1]; return;}    void Build (Int_now) {Cl[rt] = 0;        if (l! = r) {build (Lson);        Build (Rson);    PUSH_UP (now);    } else Cl[rt] = s[l]; return;}        void update (int in,int x,int_now) {if (L = = in && r = =) {Cl[rt] = x;    return;    } if (in <= (L+R)/2) update (In,x,lson);    else update (In,x,rson);    PUSH_UP (now); return;}    int query (int ll,int rr,int_now) {if (ll > R | | RR < L) return 0;    if (ll <= l && RR >= R) return CL[RT]; return query (Ll,rr,lson) +query (Ll,rr,rson);}    void Solve (int u,int v) {int temp, F1, f2, ans = 0;            while (U = v) {if (Dep[u] < dep[v]) {temp = U;            U = V;        v = temp;        } f1 = Top[u];        F2 = Top[v]; if (f1 = = F2) {ans + = query (w[sOn[v]],w[u],root);        v = u;            } else if (Dep[f1] >= dep[f2]) {ans + = query (w[f1],w[u],root);        U = fa[f1];            } else {ans + = query (w[f2],w[v],root);        v = fa[f2];    }} printf ("%d\n", ans); return;}    int main () {int I, j, K;    int u, v;        while (scanf ("%d%d", &n, &m, &t)! = EOF) {cnt = 0;        memset (head,-1,sizeof (head));            for (i = 1; i < n; i++) {scanf ("%d%d%d", &u, &v, &k);        Add (u,v,k);        } dfs ();        n--;        Build (root);        i = t;            while (m--) {scanf ("%d", &k);                if (k = = 0) {scanf ("%d", &j);                Solve (I,J);            i = j;                } else {scanf ("%d%d", &j, &k);                v = belong[j]; UpDate (w[v],k,root); }}} return 0;}


Poj2763--housewife Wind (tree chain split + line tree)

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.