zoukankan      html  css  js  c++  java
  • 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化

    题目描述

    The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!

    The cows, conveniently numbered  (), organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its "parent" in the tree). Each cow has a distinct proficiency rating, , which describes how good she is at her job. If cow  is an ancestor (e.g., a manager of a manager of a manager) of cow , then we say  is a subordinate of .

    Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow  in the company, please count the number of subordinates  where .

    奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者!

    为了方便,把奶牛从  编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。所有的第  头牛都有一个不同的能力指数 ,描述了她对其工作的擅长程度。如果奶牛  是奶牛  的祖先节点(例如,上司的上司的上司),那么我们我们把奶牛  叫做  的下属。

    不幸地是,奶牛们发现经常发生一个上司比她的一些下属能力低的情况,在这种情况下,上司应当考虑晋升她的一些下属。你的任务是帮助奶牛弄清楚这是什么时候发生的。简而言之,对于公司的中的每一头奶牛 ,请计算其下属  的数量满足 

    输入输出格式

    输入格式:

    The first line of input contains .

    The next  lines of input contain the proficiency ratings  for the cows. Each is a distinct integer in the range .

    The next  lines describe the manager (parent) for cows . Recall that cow 1 has no manager, being the president.

    输入的第一行包括一个整数 

    接下来的  行包括奶牛们的能力指数 . 保证所有数互不相同,在区间  之间。

    接下来的  行描述了奶牛  的上司(双亲节点)的编号。再次提醒,1 号奶牛作为总裁,没有上司。

    输出格式:

    Please print  lines of output. The th line of output should tell the number of subordinates of cow  with higher proficiency than cow .

    输出包括  行。输出的第  行应当给出有多少奶牛  的下属比奶牛  能力高。

    输入输出样例

    输入样例#1:
    5
    804289384
    846930887
    681692778
    714636916
    957747794
    1
    1
    2
    3
    输出样例#1:
    2
    0
    1
    0
    0

    题解:
    1.先读入每个权值然后离散化编号.
    2.然后就是遍历这颗树,然后一边修改一边统计.
    3.关键:左子树会影响到右子树的答案,那么我们就在进入右子树之前,先统计一边(答案记为Y),出来之后再统计一遍(答案记为X)那么该点的答案就为X-Y
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int N=100005;
     7 int gi(){
     8     int str=0;char ch=getchar();
     9     while(ch>'9'||ch<'0')ch=getchar();
    10     while(ch>='0' && ch<='9')str=str*10+ch-48,ch=getchar();
    11     return str;
    12 }
    13 int val[N],b[N],id[N],Tree[N*4],n;
    14 #define ls (node<<1)
    15 #define rs (node<<1|1)
    16 void updata(int node){
    17     Tree[node]=Tree[ls]+Tree[rs];
    18 }
    19 void change(int l,int r,int node,int p)
    20 {
    21     if(l>p || r<p)return ;
    22     if(l==p && r==p)
    23     {
    24         Tree[node]++;
    25         return ;
    26     }
    27     int mid=(l+r)>>1;
    28     change(l,mid,ls,p);
    29     change(mid+1,r,rs,p);
    30     updata(node);
    31 }
    32 int getsum(int l,int r,int node,int sa,int se)
    33 {
    34     if(l>se || r<sa)return 0;
    35     if(sa<=l && r<=se)return Tree[node];
    36     int mid=(l+r)>>1;
    37     return getsum(l,mid,ls,sa,se)+getsum(mid+1,r,rs,sa,se);
    38 }
    39 int mt(int x)
    40 {
    41     int l=1,r=n,mid;
    42     while(l<=r)
    43     {
    44         mid=(l+r)>>1;
    45         if(b[mid]==x)return mid;
    46         if(b[mid]>x)r=mid-1;
    47         else l=mid+1;
    48     }
    49     return -1;
    50 }
    51 int head[N],num=1,ans[N];
    52 struct Lin{
    53     int next,to;
    54 }a[N];
    55 void init(int x,int y){
    56     a[++num].next=head[x];
    57     a[num].to=y;
    58     head[x]=num;
    59 } 
    60 void dfs(int x)
    61 {
    62     int tmp=getsum(1,n,1,id[x]+1,n);
    63     for(int i=head[x];i;i=a[i].next)dfs(a[i].to);
    64     ans[x]=getsum(1,n,1,id[x]+1,n)-tmp;
    65     change(1,n,1,id[x]);
    66 }
    67 int main()
    68 {
    69     n=gi();
    70     int x;
    71     for(int i=1;i<=n;i++)val[i]=b[i]=gi();
    72     sort(b+1,b+n+1);
    73     for(int i=1;i<=n;i++)id[i]=mt(val[i]);
    74     for(int i=2;i<=n;i++)x=gi(),init(x,i);
    75     dfs(1);
    76     for(int i=1;i<=n;i++)printf("%d
    ",ans[i]);
    77     return 0;
    78 }
    
    
    
     
  • 相关阅读:
    node分离路由文件
    node项目搭建步骤
    在express获取POST(类似表单请求)的数据
    10分钟搭建Kubernetes容器集群平台(kubeadm)
    今日考题
    jQuery方法介绍
    JQuery练习题
    今日面试题:
    bom操作,事件与jquery
    今日理解之js
  • 原文地址:https://www.cnblogs.com/Yuzao/p/6912432.html
Copyright © 2011-2022 走看看