zoukankan      html  css  js  c++  java
  • 【bzoj 4756】[Usaco2017 Jan] Promotion Counting

    Description

    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 1…N1…N (1≤N≤100,000), 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 ii has a distinct proficiency rating, p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a manager of a manager) of cow jj, then we say jj is a subordinate of ii.

    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 ii in the company, please count the number of subordinates jj where p(j)>p(i).

    n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。

    问对于每个奶牛来说,它的子树中有几个能力值比它大的。

    Input

    The first line of input contains N

    The next N lines of input contain the proficiency ratings p(1)…p(N) for the cows. Each is a distinct integer in the range 1…1,000,000,000.The next N-1 lines describe the manager (parent) for cows 2…N.Recall that cow 1 has no manager, being the president.

    n,表示有几只奶牛 n<=100000

    接下来n行为1-n号奶牛的能力值pi

    接下来n-1行为2-n号奶牛的经理(树中的父亲)

    Output

    Please print N lines of output. The ith line of output should tell the number of subordinates of cow ii with higher proficiency than cow i.

    共n行,每行输出奶牛i的下属中有几个能力值比i大

    Sample Input

    5
    804289384
    846930887
    681692778
    714636916
    957747794
    1
    1
    2
    3

    Sample Output

    2
    0
    1
    0
    0

     

    大概是线段树合并的裸题……?

     

     1 #include<cstdio>
     2 #include<algorithm> 
     3 #include<cstring>
     4 #define LL long long
     5 using namespace std;
     6 const int N=1e5+10;
     7 int n,cnt,tot;
     8 int id[N],p[N],fa[N],first[N],ans[N],root[N];
     9 int ls[N*100],rs[N*100],tr[N*100];
    10 struct edge{int to,next;}e[N];
    11 int read()
    12 {
    13     int x=0,f=1;char c=getchar();
    14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    16     return x*f;
    17 }
    18 void ins(int u,int v){e[++tot]=(edge){v,first[u]};first[u]=tot;}
    19 void insert(int l,int r,int& pos,int num,int w)
    20 {
    21     pos=++cnt;tr[pos]+=w;
    22     if(l==r)return;
    23     int mid=(l+r)>>1;
    24     if(num<=mid)insert(l,mid,ls[pos],num,w);
    25     else insert(mid+1,r,rs[pos],num,w);
    26 }
    27 int merge(int now,int last)
    28 {
    29     if(!now||!last)return now^last;//子树为空就直接并上去 
    30     ls[now]=merge(ls[now],ls[last]);
    31     rs[now]=merge(rs[now],rs[last]);
    32     tr[now]=tr[ls[now]]+tr[rs[now]];
    33     return now;
    34 }
    35 int query(int l,int r,int pos,int L,int R)
    36 {
    37     if(L<=l&&R>=r)return tr[pos];
    38     int sum=0,mid=(l+r)>>1;
    39     if(L<=mid)sum+=query(l,mid,ls[pos],L,R);
    40     if(R>mid)sum+=query(mid+1,r,rs[pos],L,R);
    41     return sum;
    42 }
    43 void dfs(int x)
    44 {
    45     insert(1,n,root[x],id[x],1);
    46     for(int i=first[x];i;i=e[i].next)dfs(e[i].to);
    47     for(int i=first[x];i;i=e[i].next)root[x]=merge(root[x],root[e[i].to]);
    48     ans[x]=query(1,n,root[x],id[x]+1,n);
    49 }
    50 int main()
    51 {
    52     n=read();
    53     for(int i=1;i<=n;i++)id[i]=p[i]=read();
    54     sort(p+1,p+n+1);
    55     for(int i=1;i<=n;i++)id[i]=lower_bound(p+1,p+n+1,id[i])-p;
    56     for(int i=2;i<=n;i++)fa[i]=read(),ins(fa[i],i);
    57     dfs(1);
    58     for(int i=1;i<=n;i++)printf("%d
    ",ans[i]);
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    程序员累了怎么办?
    vue.js 组件注册实例
    background新增的N个强悍功能!!!
    相思别去问得失
    你说的我正在经历
    千折扇
    盼盼Degenerate——清除浮动的方法
    let 和 var定义变量的区别-盼盼Degenerate
    winform关闭窗体时,给用户友好提示!
    web学习笔记1--HTML
  • 原文地址:https://www.cnblogs.com/zsnuo/p/7718672.html
Copyright © 2011-2022 走看看