• bzoj1782[Usaco2010 Feb]slowdown 慢慢游*


    bzoj1782[Usaco2010 Feb]slowdown 慢慢游

    题意:

    n只奶牛各有一个目的地。它们按顺序从根节点到达自己的目的地,如果当前奶牛经过了其它已经到达的奶牛的目的地,就要放慢一次脚步。求每只奶牛要放慢多少次脚步。n≤100000。

    题解:

    对树dfs,求每个节点的进栈时间和出栈时间,然后当每只奶牛到目的地时,将目的地的进栈时间对应数组元素+1,出栈时间对应数组元素-1。每只奶牛的放慢脚步次数就是它出发前它的目的地进栈时间对应数组元素的前缀和。这一过程用树状数组维护。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 100010
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 #define lb(x) x&-x
     7 using namespace std;
     8 
     9 inline int read(){
    10     char ch=getchar(); int f=1,x=0;
    11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    12     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    13     return f*x;
    14 }
    15 int l[maxn],r[maxn],ho[maxn],sm[maxn*2],n,tot;
    16 struct e{int t,n;}; e es[maxn*2]; int ess,g[maxn];
    17 void pe(int f,int t){es[++ess]=(e){t,g[f]}; g[f]=ess; es[++ess]=(e){f,g[t]}; g[t]=ess;}
    18 void dfs(int x,int fa){l[x]=++tot; for(int i=g[x];i;i=es[i].n)if(es[i].t!=fa)dfs(es[i].t,x); r[x]=++tot;}
    19 int query(int x){int q=0; while(x)q+=sm[x],x-=lb(x); return q;}
    20 void add(int x,int v){while(x<=tot)sm[x]+=v,x+=lb(x);}
    21 int main(){
    22     n=read(); inc(i,1,n-1)pe(read(),read()); inc(i,1,n)ho[i]=read(); dfs(1,0);
    23     inc(i,1,n){printf("%d
    ",query(l[ho[i]])); add(l[ho[i]],1); add(r[ho[i]],-1);} return 0;
    24 }

    20160902

  • 相关阅读:
    worker.properties配置
    uriworkermap.properties配置
    Apache Tomcat连接器-Web服务器操作方法
    x01.os.14: 时间都去哪儿了
    x01.os.13: 文件系统
    x01.os.12: 在 windows 中写 OS
    x01.os.11: IPC 路线图
    x01.os.10: 输入输出
    x01.os.9: 进程切换
    x01.os.8: 加载内核
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5839414.html
Copyright © 2020-2023  润新知