POJ-2763 Housewife Wind(树链剖分)

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 9805 Accepted: 2696

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

Source


题意:有N个点和N-1条无向边构成了一棵树。给你起点S和Q次查询:①0 a让你求所在节点到a的最短距离;②1 a b 把第a条边的边权改为b
题解:树链剖分裸题,需要注意的是,要求修改的边是第a条,所以输入的数据要用数组存起来,我们在线段树中记录的边的编号是进入点u的边,
因此我们需要找到u,v中深度大的那个点,这条边就是进入那个点的边,然后再更新即可

#include<cstdio>
#include<string.h>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
struct Edge{
    int v,c,nxt;
}edge[2*maxn];
int f[maxn],p[maxn],num[maxn],son[maxn],top[maxn],deep[maxn],totp;
int head[maxn],tot,u[maxn],v[maxn],c[maxn];
int sum[maxn<<2],d[maxn];
void add(int u,int v,int c){
    edge[tot].v=v;
    edge[tot].c=c;
    edge[tot].nxt=head[u];
    head[u]=tot++;
}
void dfs1(int u,int fa,int cnt){
    f[u]=fa;
    deep[u]=cnt;
    son[u]=0;
    num[u]=1;
    int tmp=0;
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].v;
        if(v==fa) continue;
        dfs1(v,u,cnt+1);
        if(tmp<num[v]) son[u]=v,tmp=num[v];
        num[u]+=num[v];
    }
}
void dfs2(int u,int t){
    top[u]=t;
    p[u]=totp++;
    if(son[u]) dfs2(son[u],t);
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].v;
        if(v==f[u]) continue;
        if(v==son[u]){
            d[p[v]]=edge[i].c;
            continue;
        }
        dfs2(v,v);
        d[p[v]]=edge[i].c;
    }
}
void prebuild(int st){
    dfs1(st,0,1);
    dfs2(st,st);
}
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]=d[l];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUP(rt);
}
void update(int p,LL c,int l,int r,int rt){
    if(l==r){
        sum[rt]=c;
        return;
    }
    int m=(l+r)>>1;
    if(p<=m) update(p,c,lson);
    else update(p,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 m=(l+r)>>1;
    int ret=0;
    if(L<=m) ret+=query(L,R,lson);
    if(R>m) ret+=query(L,R,rson);
    return ret;
}
void solve(int u,int v){
    int f1=top[u],f2=top[v];
    int ans=0;
    while(f1!=f2){
        if(deep[f1]<deep[f2]) {
            swap(f1,f2);
            swap(u,v);
        }
        ans+=query(p[f1],p[u],1,totp-1,1);
        u=f[f1];f1=top[u];
    }
    if(u!=v){
        if(deep[u]>deep[v]) swap(u,v);
        ans+=query(p[son[u]],p[v],1,totp-1,1);
    }
    printf("%d\n",ans);
}
int main(){
    //freopen("in.txt","r",stdin);
    int n,m,st;
    while(~scanf("%d%d%d",&n,&m,&st)){
        memset(head,-1,sizeof(head));
        tot=totp=0;
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&u[i],&v[i],&c[i]);//要用数组保存
            add(u[i],v[i],c[i]);
            add(v[i],u[i],c[i]);
        }

        prebuild(st);
        build(1,totp-1,1);

        int op,a,b;
        while(m--){
            scanf("%d",&op);
            if(op){
                scanf("%d%d",&a,&b);
                int tmp=deep[u[a]]>deep[v[a]]?u[a]:v[a];//找出深度大的那个点
                update(p[tmp],b,1,totp-1,1);//更新进入深度大的点那条边
            }
            else{
                scanf("%d",&a);
                solve(st,a);
                st=a;
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值