zoukankan      html  css  js  c++  java
  • AC日记——Housewife Wind poj 2763

    Language:
    Housewife Wind
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 10525   Accepted: 2921

    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

    思路:

      边权转点权;

      我们可以这样转换:

      a到b的一条边的权值,我们可以看做是添加了一个新的点c;

      新的点c的权值便是边的权值;

      a—c—b

      构成这样的链;

      这样再套树剖模板;

      !!!一定要加双向边,我被这个浪费了一下午。。

    来,上代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    #define maxn 200005
    
    using namespace std;
    
    struct TreeNodeType {
        int l,r,dis,mid;
    };
    struct TreeNodeType tree[maxn<<2];
    
    struct EdgeType {
        int to,next;
    };
    struct EdgeType edge[maxn<<2];
    
    int if_z,n,q,s,deep[maxn],f[maxn],top[maxn],flag[maxn];
    int size[maxn],dis[maxn],cnt,head[maxn],dis_[maxn];
    
    char Cget;
    
    inline void read_int(int &now)
    {
        now=0,if_z=1,Cget=getchar();
        while(Cget>'9'||Cget<'0')
        {
            if(Cget=='-') if_z=-1;
            Cget=getchar();
        }
        while(Cget>='0'&&Cget<='9')
        {
            now=now*10+Cget-'0';
            Cget=getchar();
        }
        now*=if_z;
    }
    
    inline void edge_add(int from,int to)
    {
        cnt++;
        edge[cnt].to=to;
        edge[cnt].next=head[from];
        head[from]=cnt;
    }
    
    void search_1(int now,int fa)
    {
        int pos=cnt++;
        deep[now]=deep[fa]+1,f[now]=fa;
        for(int i=head[now];i;i=edge[i].next)
        {
            if(edge[i].to==fa) continue;
            search_1(edge[i].to,now);
        }
        size[now]=cnt-pos;
    }
    
    void search_2(int now,int chain)
    {
        int pos=0;
        top[now]=chain,flag[now]=++cnt;
        dis[flag[now]]=dis_[now];
        for(int i=head[now];i;i=edge[i].next)
        {
            if(edge[i].to==f[now]) continue;
            if(size[edge[i].to]>size[pos]) pos=edge[i].to;
        }
        if(pos==0) return ;
        search_2(pos,chain);
        for(int i=head[now];i;i=edge[i].next)
        {
            if(edge[i].to==pos||edge[i].to==f[now]) continue;
            search_2(edge[i].to,edge[i].to);
        }
    }
    
    inline void tree_up(int now)
    {
        tree[now].dis=tree[now<<1].dis+tree[now<<1|1].dis;
    }
    
    void tree_build(int now,int l,int r)
    {
        tree[now].l=l,tree[now].r=r;
        if(l==r)
        {
            tree[now].dis=dis[l];
            return ;
        }
        tree[now].mid=(l+r)>>1;
        tree_build(now<<1,l,tree[now].mid);
        tree_build(now<<1|1,tree[now].mid+1,r);
        tree_up(now);
    }
    
    int tree_query(int now,int l,int r)
    {
        if(tree[now].l==l&&tree[now].r==r)
        {
            return tree[now].dis;
        }
        if(l>tree[now].mid) return tree_query(now<<1|1,l,r);
        else if(r<=tree[now].mid) return tree_query(now<<1,l,r);
        else
        {
            return tree_query(now<<1,l,tree[now].mid)+tree_query(now<<1|1,tree[now].mid+1,r);
        }
    }
    
    void tree_change(int now,int to,int x)
    {
        if(tree[now].l==tree[now].r)
        {
            tree[now].dis=x;
            return ;
        }
        if(to<=tree[now].mid) tree_change(now<<1,to,x);
        else tree_change(now<<1|1,to,x);
        tree_up(now);
    }
    
    int solve_query(int x,int y)
    {
        int pos=0;
        while(top[x]!=top[y])
        {
            if(deep[top[x]]<deep[top[y]]) swap(x,y);
            pos+=tree_query(1,flag[top[x]],flag[x]);
            x=f[top[x]];
        }
        if(deep[x]>deep[y]) swap(x,y);
        pos+=tree_query(1,flag[x],flag[y]);
        return pos;
    }
    
    int main()
    {
        read_int(n),read_int(q),read_int(s);
        int u,v;
        for(int i=1;i<n;i++)
        {
            read_int(u),read_int(v),read_int(dis_[i+n]);
            edge_add(u,i+n),edge_add(n+i,v);
            edge_add(i+n,u),edge_add(v,n+i);
        }
        cnt=0,search_1(s,0);
        cnt=0,search_2(s,s);
        tree_build(1,1,n<<1);
        int type;
        for(int i=1;i<=q;i++)
        {
            read_int(type);
            if(type==0)
            {
                read_int(u);
                printf("%d
    ",solve_query(s,u));
                s=u;
            }
            else
            {
                read_int(u),read_int(v);
                tree_change(1,flag[u+n],v);
            }
        }
        return 0;
    }
  • 相关阅读:
    CXF学习(2) helloworld
    foreach与Iterable学习
    java基础之JDBC八:Druid连接池的使用
    java基础之JDBC七:C3P0连接池的使用
    java基础之JDBC六:DBCP 数据库连接池简介
    java基础之JDBC五:批处理简单示例
    java基础之JDBC四:事务简单示例
    java基础之JDBC三:简单工具类的提取及应用
    java基础之JDBC二:原生代码基础应用
    java基础之JDBC一:概述及步骤详解
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6413883.html
Copyright © 2011-2022 走看看