Live2d Test Env

POJ2763 Housewife Wind(树剖+线段树)

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 are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique. 

Since Jiajia earned enough money, 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 may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 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 her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her? 

Input

The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001. 

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

The following q lines each is one of the following two types: 

Message A: 0 u 
A kid in hut u calls Wind. She should go to hut u from her current position. 
Message B: 1 i w 
 The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is 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 1
1 2 1
2 3 2
0 2
1 2 3
0 3

Sample Output

1
3

题意:

给一棵树,边之间有权值,给初始起点,然后两种操作,第一种:求起点到终点的权值和,而且这次的终点作为下次的起点。第二中,修改树上边的权值。

思路:

比较常规的树剖,就不说了。

 

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=200010;
int Laxt[maxn],Next[maxn],To[maxn],e[maxn][3],cnt;
int opt,n,q,S,T;
struct TreeCut
{
    int dpt[maxn],top[maxn],son[maxn],fa[maxn],sz[maxn],tot;
    int tid[maxn],Rank[maxn],tim;
    int Sum[maxn<<2];
    void init()
    {
        cnt=1; tim=0;
        memset(Laxt,0,sizeof(Laxt));
        memset(Sum,0,sizeof(Sum));
    }
    void add_edge(int u,int v)
    {
        Next[++cnt]=Laxt[u];
        Laxt[u]=cnt; To[cnt]=v;
    }
    void dfs1(int u,int pre)
    {
        fa[u]=pre;dpt[u]=dpt[pre]+1;sz[u]=1;son[u]=0;
        for(int i=Laxt[u];i;i=Next[i]){
            int v=To[i]; if(v==pre) continue;
            dfs1(v,u);sz[u]+=sz[v];
            if(!son[u]||sz[v]>sz[son[u]]) son[u]=v;
        }
    }
    void dfs2(int u,int Top)
    {
        top[u]=Top; tid[u]=tim++;Rank[tid[u]]=u;
        if(!son[u]) return ;  dfs2(son[u],Top);
        for(int i=Laxt[u];i;i=Next[i])
           if(To[i]!=fa[u]&&To[i]!=son[u])  dfs2(To[i],To[i]);
    }
    void update(int Now,int L,int R,int pos,int val)
    {
        if(L==R){ Sum[Now]=val; return; }
        int Mid=(L+R)>>1;
        if(Mid>=pos) update(Now<<1,L,Mid,pos,val);
        else update(Now<<1|1,Mid+1,R,pos,val);
        Sum[Now]=Sum[Now<<1]+Sum[Now<<1|1];
    }    
    int getsum(int Now,int L,int R,int l,int r)
    {
        if(L>=l&&R<=r) return Sum[Now];
        int Mid=(L+R)>>1;
        if(r<=Mid) return getsum(Now<<1,L,Mid,l,r);
        if(l>Mid)  return getsum(Now<<1|1,Mid+1,R,l,r);
        else return getsum(Now<<1,L,Mid,l,Mid)+getsum(Now<<1|1,Mid+1,R,Mid+1,r);
    }
    void Make_Tree()
    {
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]);
            add_edge(e[i][0],e[i][1]);add_edge(e[i][1],e[i][0]);
        }  dfs1(1,0);  dfs2(1,1);
        for(int i=1;i<n;i++){
            if(dpt[e[i][1]]<dpt[e[i][0]])  swap(e[i][1],e[i][0]);
            update(1,1,n-1,tid[e[i][1]],e[i][2]);
        }
    }
    int query(int u,int v)
    {
        int f1=top[u],f2=top[v],ans=0;
        while(f1!=f2){
           if(dpt[f1]<dpt[f2]) swap(f1,f2),swap(u,v);
           ans+=getsum(1,1,n-1,tid[f1],tid[u]);
           u=fa[f1]; f1=top[u];
        }
        if(u!=v){
            if(dpt[u]>dpt[v]) swap(u,v);
            ans+=getsum(1,1,n-1,tid[son[u]],tid[v]);
        } printf("%d\n",ans);
    }
    void Query()
    {
        while(q--) { scanf("%d",&opt);
            if(opt==0){
                scanf("%d",&T);
                query(S,T); S=T;
            } else  {  int x,y; 
                scanf("%d%d",&x,&y);
                update(1,1,n-1,tid[e[x][1]],y);
            }
        }
    }
}Tc;
int main()
{
    while(~scanf("%d%d%d",&n,&q,&S)){
         Tc.init();
         Tc.Make_Tree();
         Tc.Query();
    }  return 0;
}

 

posted @ 2017-12-20 17:30  nimphy  阅读(320)  评论(0编辑  收藏  举报