zoukankan      html  css  js  c++  java
  • poj 2763 Housewife Wind

    Housewife Wind
    Time Limit: 4000MS   Memory Limit: 65536K
         

    http://poj.org/problem?id=2763

    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个点,q个操作,人的当前位置s(位于哪一个点)
    接下来n-1行 a,b,w,表示点a到点b之间有一天权值为w的边
    接下来q行
    0,a 输出a到s路径上的边权和,同时人走到a
    1,a,b 表示第a条边边权改为b
    基本框架:树链剖分+线段树(区间查询、单点修改)
    以前做过这种类型的题权值在点上,而这道题的权值在边上,怎么办?
    想想边权是干什么用的?
    是从a点走到b点,答案要累加上这个权值
    既然边权仅仅是起到累加的作用
    我们是不是可以用同样能起到累加作用的点权代替呢?
    所以
    如果a到b之间有一条权值为w的边,可以看做a和b之间有一个权值为w的点
    从a到b必然经过这个点,相当于经过这条边
    这就完成了边权向点权的转化
    怎么实现?
    把边转化成的点从n+1开始标号
    每输入第i条边,a,b,w,就往点a,n+i和点n+i,b之间加一条边
    修改第i条边的边权相当于修改点n+i的点权
    注意这样的话,点数就变成了n*2,刚开始做数组就开小了
    线段树区间范围变成了[1,n+n-1]
    代码中线段树是2*n空间建树法,
    k的左孩子l=k+1,右孩子r=k+(tr[k+1].r-tr[k+1].l+1<<1)
    #include<cstdio>
    #include<algorithm>
    #define N 100001
    using namespace std;
    int n,q,now,w[N],head[N*2],e_tot,tr_tot,ans;
    int fa[N*2],dep[N*2],son[N*2],id[N*2],sz,bl[N*2];
    struct edge{int to,next;}e[N*4];
    struct node{int l,r,sum;}tr[N*4];
    inline void insert(int u,int v)
    {
        e[++e_tot].to=v;e[e_tot].next=head[u];head[u]=e_tot;
        e[++e_tot].to=u;e[e_tot].next=head[v];head[v]=e_tot;
    }
    inline void init()
    {
        scanf("%d%d%d",&n,&q,&now);
        int u,v;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w[i]);
            insert(u,i+n);
            insert(i+n,v);
        }
    }
    inline void build(int l,int r)
    {
        tr_tot++;
        tr[tr_tot].l=l;tr[tr_tot].r=r;
        if(l==r) return;
        int mid=l+r>>1;
        build(l,mid);build(mid+1,r);
    }
    inline void change(int k,int x,int v)
    {
        if(tr[k].l==tr[k].r) {tr[k].sum=v;return;}
        int mid=tr[k].l+tr[k].r>>1,l=k+1,r=k+(tr[k+1].r-tr[k+1].l+1<<1);
        if(x<=mid) change(l,x,v);
        else change(r,x,v);
        tr[k].sum=tr[l].sum+tr[r].sum;
    }
    inline void point() 
    {
        for(int i=1;i<n;i++) change(1,id[n+i],w[i]);
    }
    inline void dfs1(int x)
    {
        son[x]++;
        for(int i=head[x];i;i=e[i].next)
        {
            if(e[i].to==fa[x]) continue;
            fa[e[i].to]=x;
            dep[e[i].to]=dep[x]+1;
            dfs1(e[i].to);
            son[x]+=son[e[i].to];
        }
    }
    inline void dfs2(int x,int chain)
    {
        sz++;
        id[x]=sz;
        bl[x]=chain;
        int y=0;
        for(int i=head[x];i;i=e[i].next)
        {
            if(e[i].to==fa[x]) continue;
            if(son[e[i].to]>son[y]) y=e[i].to;
        }
        if(!y) return;
        dfs2(y,chain);
        for(int i=head[x];i;i=e[i].next)
        {
            if(e[i].to==fa[x]||e[i].to==y) continue;
            dfs2(e[i].to,e[i].to);
        }
    }
    inline void query(int k,int opl,int opr)
    {
        if(tr[k].l>=opl&&tr[k].r<=opr) {ans+=tr[k].sum; return;}
        int mid=tr[k].l+tr[k].r>>1,l=k+1,r=k+(tr[k+1].r-tr[k+1].l+1<<1);
        if(opl<=mid) query(l,opl,opr);
        if(opr>mid) query(r,opl,opr);
    }
    inline void solve_query(int u,int v)
    {
        ans=0;
        while(bl[u]!=bl[v])
        {
            if(id[bl[u]]<id[bl[v]]) swap(u,v);
            query(1,id[bl[u]],id[u]);
            u=fa[bl[u]];
        }
        if(id[u]>id[v]) swap(u,v);
        query(1,id[u],id[v]);
        printf("%d
    ",ans);
    }
    inline void solve()
    {
        int p,a,b;
        for(int i=1;i<=q;i++)
        {
            scanf("%d",&p);
            if(p) {scanf("%d%d",&a,&b);  change(1,id[n+a],b);}
            else {scanf("%d",&a);  solve_query(now,a);  now=a;}
            
        }
    }
    int main()
    {
        init();
        build(1,n+n-1);
        dfs1(1);
        dfs2(1,1);
        point();
        solve();
    }
  • 相关阅读:
    C# NAudio 变声
    初探Protostuff的使用
    CentOS7 配置阿里云yum源,非常之简单
    javacv 视频增加视频(画中画)
    抓包调试fiddler
    CentOS7 手动编译升级GCC至9.3.0
    C# CefSharp 新版本(83 以后版本) 如何在js中直接调用c#类
    毕业设计——驾驶证理论考试系统的设计与实现 2022年1月2日20:35:14
    送餐机器人乐动雷达记录
    linux下拉取git代码
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6399157.html
Copyright © 2011-2022 走看看