[POJ 2763] Housewife Wind

Housewife Wind
 

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 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 <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <iterator>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x7fffffff
#define ll long long
#define N 100010

struct Edge2
{
    int a,b,c;
}s[N<<1];

struct Edge
{
    int to,next;
}edge[N<<1];
int head[N],tot;

int num[N];
int pos;
int fa[N];
int son[N];
int p[N];
int fp[N];
int deep[N];
int size[N];
int top[N];
int n,q,st;

void init()
{
    tot=0;
    pos=1;
    memset(head,-1,sizeof(head));
    memset(son,-1,sizeof(son));
}
void add(int x,int y)
{
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot++;
}
void dfs1(int now,int pre,int d)
{
    deep[now]=d;
    fa[now]=pre;
    size[now]=1;
    for(int i=head[now];i!=-1;i=edge[i].next)
    {
        int next=edge[i].to;
        if(next!=pre)
        {
            dfs1(next,now,d+1);
            size[now]+=size[next];
            if(son[now]==-1 || size[next]>size[son[now]])
            {
                son[now]=next;
            }
        }
    }
}
void dfs2(int now,int tp)
{
    top[now]=tp;
    p[now]=pos++;
    fp[p[now]]=now;
    if(son[now]==-1) return;
    dfs2(son[now],tp);
    for(int i=head[now];i!=-1;i=edge[i].next)
    {
        int next=edge[i].to;
        if(next!=son[now]&&next!=fa[now])
        {
            dfs2(next,next);
        }
    }
}
int sum[N<<2];
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=num[fp[l]];
        return;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt);
}
void update(int l,int r,int rt,int pos,int val)
{
    if(l==r)
    {
        sum[rt]=val;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m) update(l,m,rt<<1,pos,val);
    else update(m+1,r,rt<<1|1,pos,val);
    pushup(rt);
}
int query(int l,int r,int rt,int L,int R)
{
    if(L==l && R==r)
    {
        return sum[rt];
    }
    int m=(l+r)>>1;
    if(R<=m) return query(l,m,rt<<1,L,R);
    else if(L>m) return query(m+1,r,rt<<1|1,L,R);
    else return query(l,m,rt<<1,L,m)+query(m+1,r,rt<<1|1,m+1,R);
}
int convert(int pos)
{
    int a=s[pos].a;
    int b=s[pos].b;
    if(deep[a]>deep[b]) return a;
    return b;
}
void pre_solve()
{
    memset(num,-1,sizeof(num));
    for(int i=1;i<n;i++)
    {
        num[convert(i)]=s[i].c;
    }
}
int change(int x,int y)
{
    int ans=0;
    int f1=top[x];
    int f2=top[y];
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(x,y);
            swap(f1,f2);
        }
       ans+=query(1,n,1,p[f1],p[x]);
        x=fa[f1];
        f1=top[x];
    }
    if(x==y) return ans;
    if(deep[x]>deep[y]) swap(x,y);
    ans+=query(1,n,1,p[x]+1,p[y]);
    return ans;
}
int main()
{
    while(scanf("%d%d%d",&n,&q,&st)!=EOF)
    {
        init();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&s[i].a,&s[i].b,&s[i].c);
            add(s[i].a,s[i].b);
            add(s[i].b,s[i].a);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        pre_solve();
        build(1,n,1);
        while(q--)
        {
            int op,a,b;
            scanf("%d",&op);
            if(op==0)        //查询s-->a的最短路
            {
                scanf("%d",&a);
                //cout<<"st-->a:"<<st<<' '<<a<<endl;
                printf("%d\n",change(st,a));
                st=a;
            }
            else             //将第a条边的权变为b
            {
                scanf("%d%d",&a,&b);
                a=convert(a);
                update(1,n,1,p[a],b);
            }
        }
    }
    return 0;
}

 

posted @ 2014-11-24 14:17  哈特13  阅读(221)  评论(0编辑  收藏  举报