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

    Housewife Wind

    Time Limit: 4000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 2763
    64-bit integer IO format: %lld      Java class name: Main
     
    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

     
    解题:树链剖分
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 using namespace std;
      6 const int maxn = 100010;
      7 struct arc{
      8     int to,w,next;
      9     arc(int x = 0,int y = 0,int z = -1){
     10         to = x;
     11         w = y;
     12         next = z;
     13     }
     14 }e[maxn<<1];
     15 struct node{
     16     int lt,rt,sum;
     17 }tree[maxn<<2];
     18 int head[maxn],fa[maxn],top[maxn],dep[maxn];
     19 int siz[maxn],son[maxn],loc[maxn];
     20 int n,q,s,tot,clk;
     21 void add(int u,int v,int w){
     22     e[tot] = arc(v,w,head[u]);
     23     head[u] = tot++;
     24 }
     25 void FindHeavyEdge(int u,int father,int depth){
     26     fa[u] = father;
     27     son[u] = -1;
     28     siz[u] = 1;
     29     dep[u] = depth;
     30     for(int i = head[u]; ~i; i = e[i].next){
     31         if(e[i].to == father) continue;
     32         FindHeavyEdge(e[i].to,u,depth + 1);
     33         siz[u] += siz[e[i].to];
     34         if(son[u] == -1 || siz[e[i].to] > siz[son[u]])
     35             son[u] = e[i].to;
     36     }
     37 }
     38 void ConnectHeavyEdge(int u,int ancestor){
     39     top[u] = ancestor;
     40     loc[u] = clk++;
     41     if(son[u] != -1) ConnectHeavyEdge(son[u],ancestor);
     42     for(int i = head[u]; ~i; i = e[i].next){
     43         if(e[i].to == fa[u] || son[u] == e[i].to) continue;
     44         ConnectHeavyEdge(e[i].to,e[i].to);
     45     }
     46 }
     47 void build(int lt,int rt,int v){
     48     tree[v].lt = lt;
     49     tree[v].rt = rt;
     50     tree[v].sum = 0;
     51     if(lt == rt) return;
     52     int mid = (lt + rt)>>1;
     53     build(lt,mid,v<<1);
     54     build(mid + 1,rt,v<<1|1);
     55 }
     56 void update(int pos,int val,int v){
     57     if(tree[v].lt == tree[v].rt){
     58         tree[v].sum = val;
     59         return;
     60     }
     61     if(pos <= tree[v<<1].rt) update(pos,val,v<<1);
     62     else update(pos,val,v<<1|1);
     63     tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum;
     64 }
     65 int query(int lt,int rt,int v){
     66     if(lt <= tree[v].lt && rt >= tree[v].rt)
     67         return tree[v].sum;
     68     int ret = 0;
     69     if(lt <= tree[v<<1].rt) ret = query(lt,rt,v<<1);
     70     if(rt >= tree[v<<1|1].lt) ret += query(lt,rt,v<<1|1);
     71     return ret;
     72 }
     73 int QUERY(int u,int v){
     74     if(u == v) return 0;
     75     int ret = 0;
     76     while(top[u] != top[v]){
     77         if(dep[top[u]] < dep[top[v]]) swap(u,v);
     78         ret += query(loc[top[u]],loc[u],1);
     79         u = fa[top[u]];
     80     }
     81     if(u == v) return ret;
     82     if(dep[u] > dep[v]) swap(u,v);
     83     ret += query(loc[son[u]],loc[v],1);
     84     return ret;
     85 }
     86 int main(){
     87     int u,v,w,op;
     88     while(~scanf("%d%d%d",&n,&q,&s)){
     89         memset(head,-1,sizeof head);
     90         tot = clk = 0;
     91         for(int i = 1; i < n; ++i){
     92             scanf("%d%d%d",&u,&v,&w);
     93             add(u,v,w);
     94             add(v,u,w);
     95         }
     96         FindHeavyEdge(1,0,0);
     97         ConnectHeavyEdge(1,1);
     98         build(0,clk-1,1);
     99         for(int i = 0; i < tot; i += 2){
    100             u = e[i].to;
    101             v = e[i + 1].to;
    102             if(dep[u] < dep[v]) swap(u,v);
    103             update(loc[u],e[i].w,1);
    104         }
    105         while(q--){
    106             scanf("%d",&op);
    107             if(op){
    108                 scanf("%d%d",&u,&w);
    109                 int ith = (u-1)*2;
    110                 if(dep[e[ith].to] < dep[e[ith + 1].to]) ++ith;
    111                 update(loc[e[ith].to],w,1);
    112             }else{
    113                 scanf("%d",&u);
    114                 printf("%d
    ",QUERY(s,u));
    115                 s = u;
    116             }
    117         }
    118     }
    119     return 0;
    120 }
    View Code
  • 相关阅读:
    CentOS 配置自启动Redis
    WPF Popup全屏 弹出方法。解决只显示75%的问题。
    UpdatePanel控件的使用和局部刷新
    关于width与padding
    WPF 快捷方式
    深入浅出WPF——附加事件(Attached Event)
    控件属性使用代码动代绑定
    ICommand.CanExecuteChanged事件订阅对象的变化
    输入德文出现乱码现象的处理方法
    MVVM 模版里的控件怎样触发命令
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4844088.html
Copyright © 2011-2022 走看看