BZOJ4756: [Usaco2017 Jan]Promotion Counting

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 218 Solved: 150
Description

The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent 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 man
ager 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 seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. 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
HINT

Source

Platinum鸣谢Acty提供译文

此题我们可以遍历一下这棵树,然后用树状数组来维护,对于每个点的答案即为 子树节点个数-(遍历它的子树前比它小的数的个数-便利后比它小的数的个数)。当然在将一个点加入树状数组时,我们需要在遍历完它的子树后再加入(这样就不会算自己了)。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[110000],b[110000],tot,Next[210000],head[110000],tree[210000],size[110000];
int ans[210000],f[110000];
bool flag[110000];
int lowbit(int x) { return x&(-x);}
void add(int x,int y)
{
    tot++;
    Next[tot]=head[x];
    head[x]=tot;
    tree[tot]=y;
}
int get(int x)
{
    int ans=0;
    for (int i=x;i>=1;i-=lowbit(i)) ans+=f[i];
    return ans;
}
void add(int x)
{
    for (int i=x;i<=n;i+=lowbit(i)) f[i]++;
}
void dfs(int x)
{
    flag[x]=true;size[x]=1;
    int y=get(a[x]);
    for (int i=head[x];i;i=Next[i]) 
        if (!flag[tree[i]]) dfs(tree[i]),size[x]+=size[tree[i]];
    ans[x]=size[x]-(get(a[x])-y)-1;
    add(a[x]);
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) 
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
        flag[i]=false;
    }
    sort(b+1,b+n+1);
    int t=unique(b+1,b+n+1)-b-1;
    for (int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+t+1,a[i])-b;
    tot=0;
    for (int i=2;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        add(x,i);
    }
    dfs(1);
    for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值