首页 > 其他 > 详细

[BZOJ4756]Promotion Counting

时间:2018-10-07 10:07:54      阅读:152      评论:0      收藏:0      [点我收藏+]

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
 
线段树合并
代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #define M 100010
 5 #define ls ch[node][0]
 6 #define rs ch[node][1]
 7 using namespace std;
 8 
 9 int n,m,num,cnt;
10 int head[M],a[M],b[M],ans[M],rt[M];
11 int v[M<<5],ch[M<<5][2];
12 struct point{int to,nxt;}e[M<<1];
13 
14 void add(int from,int to)
15 {
16     e[++num].nxt=head[from];
17     e[num].to=to;
18     head[from]=num;
19 }
20 
21 void insert(int &node,int l,int r,int x)
22 {
23     if(!node) node=++cnt;v[node]++;
24     if(l==r) return;
25     int mid=(l+r)/2;
26     if(x<=mid) insert(ls,l,mid,x);
27     else insert(rs,mid+1,r,x);
28 }
29 
30 int query(int node,int l,int r,int l1,int r1)
31 {
32     if(!node) return 0;
33     if(l1<=l&&r1>=r) return  v[node];
34     if(l1>r||r1<l) return 0;
35     int mid=(l+r)/2;
36     return query(ls,l,mid,l1,r1)+query(rs,mid+1,r,l1,r1);
37 }
38 
39 int merge(int x,int y)
40 {
41     if(!x||!y) return x+y;
42     int node=++cnt;
43     v[node]=v[x]+v[y];
44     ls=merge(ch[x][0],ch[y][0]);
45     rs=merge(ch[x][1],ch[y][1]);
46     return node;
47 }
48 
49 void dfs(int x)
50 {
51     for(int i=head[x];i;i=e[i].nxt)
52     {
53         int to=e[i].to;
54         dfs(to);
55         rt[x]=merge(rt[x],rt[to]);
56     }
57     ans[x]=query(rt[x],1,m,a[x]+1,m);
58     insert(rt[x],1,m,a[x]);
59 }
60 int main()
61 {
62     scanf("%d",&n);
63     for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[++m]=a[i];
64     sort(b+1,b+1+m); m=unique(b+1,b+1+m)-b-1;
65     for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+m,a[i])-b;
66     for(int i=2;i<=n;i++)
67     {
68         int x;scanf("%d",&x);
69         add(x,i);
70     }
71     dfs(1);
72     for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
73     return 0;
74 }

 

[BZOJ4756]Promotion Counting

原文:https://www.cnblogs.com/Slrslr/p/9749316.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!