BZOJ 4756 Usaco2017 Jan Promotion Counting

4756: [Usaco2017 Jan]Promotion Counting

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 486  Solved: 340
[Submit][Status][Discuss]

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提供译文 Us

 

提供两种思路 第一种dfs+树状数组

第二种 dfs+线段树合并

线段树合并做法很显然

dfs+树状数组的话  我们在dfs的过程中遍历到一个点的时候 记录一下这时候比他小的数量  等整个子树遍历完了以后用 现在比他小的个数-刚开始记录的个数 就是他子树中比他小的个数

用树状数组实现即可 遍历完子树后把这个点加入树状数组中即可

/**************************************************************
    Problem: 4756
    User: zhangenming
    Language: C++
    Result: Accepted
    Time:340 ms
    Memory:33168 kb
****************************************************************/
 
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define eps 1e-7
#define inf 1e9+10
using namespace std;
inline int read(){
    int x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int MAXN=1e6+10;
struct node{
    int y,next;
}e[MAXN];
int c[MAXN],n,m,linkk[MAXN],len,ans[MAXN],siz[MAXN];
inline void insert(int x,int y){
    e[++len].y=y;e[len].next=linkk[x];linkk[x]=len;
}
struct val{
    int v,id;
}a[MAXN];
inline bool cmp(val n,val m){
    return n.v<m.v||(n.v==m.v&&n.id<m.id);
}
inline bool mycmp(val n,val m){
    return n.id<m.id;
}
inline int lowbit(int x){
    return x&-x;
}
inline void change(int v){
    for(int i=v;i<MAXN;i+=lowbit(i)){
        c[i]++;
    }
}
inline int query(int v){
    int ans=0;
    for(int i=v;i;i-=lowbit(i)){
        ans+=c[i];
    }
    return ans;
}
inline void dfs(int x){
    ans[x]=-query(a[x].v);
    for(int i=linkk[x];i;i=e[i].next){
        dfs(e[i].y);siz[x]+=siz[e[i].y];
    }
    ans[x]+=query(a[x].v);
    ans[x]=siz[x]-ans[x];
    change(a[x].v);siz[x]++;
}
int main(){
    //freopen("All.in","r",stdin);
    //freopen("zh.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++){
        a[i].v=read();a[i].id=i;
    }
    sort(a+1,a+n+1,cmp);
    int tot=1;
    for(int i=1;i<=n;i++){
        a[i].v=tot;
        if(a[i].v!=a[i+1].v) tot++;
    }
    for(int i=2;i<=n;i++){
        int t=read();
        insert(t,i);
    }
    sort(a+1,a+n+1,mycmp);
    dfs(1);
    for(int i=1;i<=n;i++){
        printf("%d\n",ans[i]);
    }
    return 0;
}

  

posted @ 2018-08-21 08:42  zhangenming  阅读(144)  评论(0编辑  收藏  举报