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 }

转载于:https://www.cnblogs.com/Slrslr/p/9749316.html

[BZOJ4756]Promotion Counting相关推荐

  1. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  2. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

  3. 洛谷3605 Promotion Counting

    线段树合并都是蓝题了嘛 我可能和时代脱轨了emm... 直接离散化然后合并就好啦w 生病了真难受QAQ //Love and Freedom. #include<cstdio> #incl ...

  4. [USACO17JAN]Promotion Counting 题解

    前言 巨佬说:要有线段树,结果蒟蒻打了一棵树状数组... 想想啊,奶牛都开公司当老板了,我还在这里码代码,太失败了. 话说奶牛开个公司老板不应该是FarmerJohn吗? 题解 刚看到这道题的时候竟然 ...

  5. P3605 [USACO17JAN]Promotion Counting P dfs序

    传送门 文章目录 题意: 思路: 题意: 思路: 这个题可以用各种姿势a掉,树启和线段树合并都可以,比较无脑.下面给一个解决这种问题比较巧妙的做法. 考虑暴力怎么写,我们先将每个点的权值离散化一下,每 ...

  6. P3605 [USACO17JAN]Promotion Counting P(树状数组)

    解析 做法很多的一道题 sol1 先求出dfs序,离线下来,然后按权值大小的顺序统计答案并插到对应的dfs序中 sol2 离散化后,dfs过程中动态维护树状数组,利用前后差值求出答案 sol3 树上d ...

  7. 牛客假日团队赛1 D.Promotion Counting

    链接: https://ac.nowcoder.com/acm/contest/918/D 题意: Bessie the cow is helping Farmer John run the USA ...

  8. Promotion Counting【USACO 2016 January Contest, Bronze】

    今天来分享一下我做过的几道Usaco的比较简单的题,Usaco是美国的一个c++竞赛比赛,但是全球各地的人都可以参加,Usaco没有监考,全凭诚信,但是你拿着这个 作弊 借鉴来的成绩,所有美国的大学都 ...

  9. USACO比赛题泛刷

    随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...

最新文章

  1. CoreAnimation —— CATransaction(隐式动画)
  2. Swift - 闭包的介绍及用法(以数组排序为例)
  3. 国防科大提出基于可变形三维卷积的视频超分辨,代码已开源
  4. Oracle 原理:用户和权限,Profile,系统权限,对象权限,角色
  5. 【OFDM系列1】OFDM调制原理、参数、循环前缀、采样偏差、频偏及估计详解
  6. 转: 微博的多机房部署的实践(from infoq)
  7. springMVC数据模型model,modelmap,map,@ModelAttribute的相互关系
  8. 正则表达式(待补充)
  9. smartsvn破解版
  10. 计算机丢失MSVCR100.dll文件的解决办法
  11. The server time zone value '?й???????' is unrecognized or represents more than one time zone.
  12. 求岛屿的最大面积java
  13. GOOGLE卫星地图
  14. Android webview加载天猫店铺时会报net::ERR_UNKNOWN_URL_SCHEME;
  15. Python爬虫教程——入门一之爬虫基础了解
  16. 客快物流大数据项目(三十):软件下载后存放位置
  17. 若伊Vue快速容器部署
  18. json格式转数组过程数值改变的问题
  19. 徐耀赐:新型道路交通工程安全设施发展展望1(图文版)
  20. 【软件安装】远程桌面软件系列

热门文章

  1. debian下erlang新版本安装
  2. virt-install选项详解
  3. linux如何挂载windows下的共享文件
  4. Leetcode题目:Reverse String
  5. 我的Java开发学习之旅------求字符串中出现次数最多的字符串以及出现的次数...
  6. 正确使用ArrayList和LinkedList
  7. WordPress数据库研究
  8. 蓝桥杯 ALGO-28 算法训练 星际交流
  9. 【iOS】Unlock iPhone to Continue Xcode cannot launch demo1_greating on iPhone because the device is lo
  10. dbscan和谱聚类_R 无监督聚类算法(1)K-means和层次聚类