poj 2763 Housewife Wind(树链剖分---边权化点权)

Housewife Wind

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 14220 Accepted: 3935

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

POJ Monthly--2006.02.26,zgl & twb

[Submit]   [Go Back]   [Status]   [Discuss]

【题意】

给出树,有边权。操作0:查询上一次所在的点s到u之间的距离。操作1:修改第i条边的权值为w

【分析】

以1为根做一次树链剖分,把边权转移给对应的儿子结点。

比较好的模板题。

线段树维护(有点耗时长)

树状数组维护好用些

【代码】

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long ll;
const int MAX=1e5+5;
const int INF=0x3f3f3f3f;

/*********** 图 ***********/
struct node{
    int t,next;
    ll w;
    int id;
}edge[MAX*2];
int head[MAX],cnt;
void init(int n)
{
    cnt=0; memset(head,-1,sizeof(head[0])*(n+1));
}
void addedge(int u,int v,int w,int id)
{
    edge[cnt]=node{v,head[u],w,id};
    head[u]=cnt++;
}

/************ 树链剖分 **************/
int dep[MAX],dad[MAX],siz[MAX],son[MAX],bev[MAX]; //bev[i]表示第i边对应的点
int top[MAX],rk[MAX],id[MAX];
void dfs1(int u,int pre)
{
    siz[u]=1; //u本身
    son[u]=-1;
    int maxsiz=0; //最大的儿子子树大小
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(v==pre)continue;
        dep[v]=dep[u]+1;
        dad[v]=u;
        bev[edge[i].id]=v; //边对应点
        dfs1(v,u); //注意dfs的位置
        siz[u]+=siz[v];
        if(maxsiz<siz[v])
        {
            maxsiz=siz[v];
            son[u]=v;
        }
    }
}
void dfs2(int u,int fat,int &tag) //结点u,重链顶端
{
    top[u]=fat;
    rk[u]=++tag; //时间戳
    id[tag]=u; //tag对应在树上的结点号
    if(son[u]==-1)return; //没有孩子
    dfs2(son[u],fat,tag); //优先重链dfs
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(v==dad[u]||v==son[u])continue;
        dfs2(v,v,tag);
    }
}
void cuttree(int root) //将root为根的树链剖分
{
    int tag=0;
    dep[root]=0;  //根的深度和父结点做一个初始化
    dad[root]=root;   //这是一个假设,根的父结点还是根
    dfs1(root,root);
    dfs2(root,root,tag);
}

/************* 线段树 ****************/
ll sum[MAX<<2],mark[MAX<<2];
void build(int root,int l,int r) //初始线段树
{
    mark[root]=0;
    if(l==r)return;//叶子节点
    int mid=(l+r)/2;
    build(root*2,l,mid);   //左子树的创建
    build(root*2+1,mid+1,r); //右子树
}
void nodeupdate(int root,int l,int r,ll num) //因题而异
{
    mark[root]=num;
    sum[root]=num*(r-l+1);
}
void pushdown(int root,int l,int r)//传递给两孩子
{
    if(mark[root]==0)return;
    int mid=(l+r)/2;
    nodeupdate(root*2,l,mid,mark[root]);
    nodeupdate(root*2+1,mid+1,r,mark[root]);
    mark[root]=0;
}
void pushup(int root,int l,int r)
{
    sum[root]=sum[root<<1]+sum[root<<1|1];
}
void update(int left,int right,ll num, int root,int l,int r)//修改
{
    if(left<=l&&r<=right){
        nodeupdate(root,l,r,num);
        return;
    }
    pushdown(root,l,r);
    int mid=(l+r)/2;
    if(left<=mid)
        update(left,right,num,root*2,l,mid);
    if(mid<right)
        update(left,right,num,root*2+1,mid+1,r);
    pushup(root,l,r);
}
ll Qsum(int left,int right, int root,int l,int r)//区间最大值
{
    if(left<=l&&r<=right)
        return sum[root];
    pushdown(root,l,r);
    int mid=(l+r)/2;
    ll res=0; ///赋初值!!!
    if(left<=mid)
        res+=Qsum(left,right,root*2,l,mid);//区间在左子树
    if(right>mid)
        res+=Qsum(left,right,root*2+1,mid+1,r);//在右子树
    return res;
}

/************** 树链->线段树 *********/
void modify(int u,int v,ll num,int limit) //更新,negative
{
    int fu=top[u],fv=top[v];
    while(fu!=fv) //uv不在同一条链上
    {
        if(dep[fu]>=dep[fv]) //u的深度大
        {
            update(rk[fu],rk[u],num,1,1,limit);
            u=dad[fu]; fu=top[u];
        }
        else
        {
            update(rk[fv],rk[v],num,1,1,limit);
            v=dad[fv]; fv=top[v];
        }
    } //循环结束时,fu-fv在同一链
    if(dep[u]>dep[v])swap(u,v);
    if(u!=v)update(rk[u]+1,rk[v],num,1,1,limit); //注意!边权题,lca不取
}
ll query(int u,int v, int limit) //查询u-v路径和
{
    ll res=0;
    int fu=top[u],fv=top[v];
    while(fu!=fv) //uv不在同一条链上
    {
        if(dep[fu]>=dep[fv]) //u的深度大
        {
            res+=Qsum(rk[fu],rk[u],1,1,limit);
            u=dad[fu]; fu=top[u];
        }
        else
        {
            res+=Qsum(rk[fv],rk[v],1,1,limit);
            v=dad[fv]; fv=top[v];
        }
    } //循环结束时,fu-fv在同一链
    if(dep[u]>dep[v])swap(u,v);
    if(u!=v)
        res+=Qsum(rk[u]+1,rk[v],1,1,limit); ///注意+1,lca不取
    return res;
}

/************** solve **************/
int a[MAX];
bool solve()
{
    int n,q,s,u,v,w,op;
    scanf("%d%d%d",&n,&q,&s);
    init(n); //初始图
    for(int i=1;i<n;i++) //边权
    {
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w,i);
        addedge(v,u,w,i);
    }
    cuttree(1); //树链剖分
    build(1,1,n); //建立线段树
    for(int i=1;i<n;i++)a[bev[i]]=edge[(i-1)*2].w; //边权转化为点权
    //以下变为点权做法,但是lca结点不可取!!!这是与纯点权图的区别
    for(int i=2;i<=n;i++)update(rk[i],rk[i],a[i],1,1,n);//初态
    while(q--)
    {
        scanf("%d",&op);
        if(op==0) //查询
        {
            scanf("%d",&u);
            ll ans=query(u,s,n); //注意这里是查询点之间
            s=u;
            printf("%lld\n",ans);
        }
        else //边u权值修改为w
        {
            scanf("%d%d",&u,&w);
            u=bev[u];
            modify(u,dad[u],w,n); //这里是点之间
        }
    }
}
int main()
{
    solve();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值