POJ - 2763 Housewife Wind (树链剖分 + 线段树单点更新,区间求和)

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

题目链接:http://poj.org/problem?id=2763

题目大意:有n个点,q次查询,起点s,有n-1条边 u,v,w, 查询有两种:0 x  求从起点s到点x最短路径上的边权和

1 x z  把第x条边的权值改为z

思路:和Query on a tree有点类似,把边权变成点权,线段树维护区间和,单点更新

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N=100005;
int first[N],n,s,w[N],tot,cnt,sum[N<<2];
int d[N],fa[N],siz[N],son[N],top[N],id[N],rk[N];
struct node
{
    int u,v,w,nex;
}e[N<<1];
void adde(int u,int v,int w)
{
    e[tot].u=u,e[tot].v=v;
    e[tot].w=w;
    e[tot].nex=first[u];
    first[u]=tot++;
}
void init()
{
    memset(first,-1,sizeof(first));
    memset(son,0,sizeof(son));
    tot=cnt=0;
}
void dfs1(int u,int pre,int depth) //处理 d,fa,siz,son 数组
{
    d[u]=depth;
    fa[u]=pre;
    siz[u]=1;
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v==pre) continue;
        dfs1(v,u,depth+1);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]])
            son[u]=v;
    }
}
void dfs2(int u,int t) //处理 top id rk wt 数组
{
    top[u]=t;
    id[u]=++cnt;
    rk[cnt]=u;
    if(!son[u]) return;
    dfs2(son[u],t);
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v!=son[u]&&v!=fa[u])
            dfs2(v,v);
    }
}
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]=w[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int k,int c,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=c;
        return;
    }
    int mid=(l+r)>>1;
    if(k<=mid) update(k,c,lson);
    else update(k,c,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return sum[rt];
    int mid=(l+r)>>1,ans=0;
    if(L<=mid) ans+=query(L,R,lson);
    if(R>mid) ans+=query(L,R,rson);
    return ans;
}
int quesum(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(d[top[x]]<d[top[y]]) swap(x,y);
        ans+=query(id[top[x]],id[x],1,n,1);
        x=fa[top[x]];
    }
    if(x!=y)
    {
        if(d[x]>d[y]) swap(x,y);
        ans+=query(id[son[x]],id[y],1,n,1);
    }
    return ans;
}
int main()
{
    int q;
    while(~scanf("%d%d%d",&n,&q,&s))
    {
        init();
        int x,y,z,op;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            adde(x,y,z);
            adde(y,x,z);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        for(int i=0;i<tot;i+=2)
        {
            if(d[e[i].u]<d[e[i].v])
                swap(e[i].u,e[i].v);
            w[id[e[i].u]]=e[i].w;
        }
        build(1,n,1);
        while(q--)
        {
            scanf("%d",&op);
            if(op==0)
            {
                scanf("%d",&x);
                int ans=quesum(s,x);
                s=x;
                printf("%d\n",ans);
            }
            else if(op==1)
            {
                scanf("%d%d",&x,&z);
                update(id[e[x*2-2].u],z,1,n,1);
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值