题目传送门

Promotion Counting

题目描述

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 \ldots N1…N (1 \leq N \leq 100,0001≤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)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)p(j)>p(i).

奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者!

为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N≤100,000) 编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。所有的第 ii 头牛都有一个不同的能力指数 p(i)p(i),描述了她对其工作的擅长程度。如果奶牛 ii 是奶牛 jj 的祖先节点(例如,上司的上司的上司),那么我们我们把奶牛 jj 叫做 ii 的下属。

不幸地是,奶牛们发现经常发生一个上司比她的一些下属能力低的情况,在这种情况下,上司应当考虑晋升她的一些下属。你的任务是帮助奶牛弄清楚这是什么时候发生的。简而言之,对于公司的中的每一头奶牛 ii,请计算其下属 jj 的数量满足 p(j) > p(i)p(j)>p(i)。

输入输出格式

输入格式:

The first line of input contains NN.

The next NN lines of input contain the proficiency ratings p(1) \ldots p(N)p(1)…p(N) for the cows. Each is a distinct integer in the range 1 \ldots 1,000,000,0001…1,000,000,000.

The next N-1N−1 lines describe the manager (parent) for cows 2 \ldots N2…N. Recall that cow 1 has no manager, being the president.

输入的第一行包括一个整数 NN。

接下来的 NN 行包括奶牛们的能力指数 p(1) \cdots p(N)p(1)⋯p(N). 保证所有数互不相同,在区间 1 \cdots 10^91⋯109 之间。

接下来的 N-1N−1 行描述了奶牛 2 \cdots N2⋯N 的上司(双亲节点)的编号。再次提醒,1 号奶牛作为总裁,没有上司。

输出格式:

Please print NN lines of output. The iith line of output should tell the number of subordinates of cow ii with higher proficiency than cow ii.

输出包括 NN 行。输出的第 ii 行应当给出有多少奶牛 ii 的下属比奶牛 ii 能力高。

输入输出样例

输入样例#1:

  1. 5
  2. 804289384
  3. 846930887
  4. 681692778
  5. 714636916
  6. 957747794
  7. 1
  8. 1
  9. 2
  10. 3
输出样例#1:

  1. 2
  2. 0
  3. 1
  4. 0
  5. 0

说明

感谢@rushcheyo 的翻译


  分析:

  线段树合并。

  先构建出树形结构,然后对每一个节点建立一个权值线段树,在$dfs$过程中,对于每一个子节点把它的每一个子节点的线段树与它的线段树合并。查询答案就在当前线段树中查询比该节点的权值$val[x]$更大的值有多少个即可。

  Code:

  1. //It is made by HolseLee on 15th Oct 2018
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. const int N=1e5+;
  9. int n,head[N],cnte,val[N],root[N],tot,cnt,seg[N*],ls[N*],rs[N*],ans[N];
  10. struct Node {
  11. int id,v;
  12. inline bool operator < (const Node x) const {
  13. return v<x.v;
  14. }
  15. }b[N];
  16. struct Edge {
  17. int to,nxt;
  18. }e[N];
  19.  
  20. inline int read()
  21. {
  22. char ch=getchar(); int num=; bool flag=false;
  23. while( ch<'' || ch>'' ) {
  24. if( ch=='-' ) flag=true; ch=getchar();
  25. }
  26. while( ch>='' && ch<='' ) {
  27. num=num*+ch-''; ch=getchar();
  28. }
  29. return flag ? -num : num;
  30. }
  31.  
  32. inline void add(int x,int y)
  33. {
  34. e[++cnte].to=y;
  35. e[cnte].nxt=head[x];
  36. head[x]=cnte;
  37. }
  38.  
  39. void build(int &rt,int l,int r,int x)
  40. {
  41. if( !rt ) rt=++tot;
  42. seg[rt]++;
  43. if( l==r ) return;
  44. int mid=(l+r)>>;
  45. if( x<=mid ) build(ls[rt],l,mid,x);
  46. else build(rs[rt],mid+,r,x);
  47. }
  48.  
  49. int query(int rt,int l,int r,int x)
  50. {
  51. if( !rt ) return ;
  52. if( x<=l ) return seg[rt];
  53. int mid=(l+r)>>;
  54. if( x<=mid ) return query(ls[rt],l,mid,x)+query(rs[rt],mid+,r,x);
  55. else return query(rs[rt],mid+,r,x);
  56. }
  57.  
  58. int merge(int x,int y)
  59. {
  60. if( !x || !y ) return x+y;
  61. int rt=++tot;
  62. seg[rt]=seg[x]+seg[y];
  63. ls[rt]=merge(ls[x],ls[y]);
  64. rs[rt]=merge(rs[x],rs[y]);
  65. return rt;
  66. }
  67.  
  68. void dfs(int x)
  69. {
  70. for(int i=head[x],y; i; i=e[i].nxt) {
  71. y=e[i].to; dfs(y);
  72. root[x]=merge(root[x],root[y]);
  73. }
  74. ans[x]=query(root[x],,cnt,val[x]+);
  75. build(root[x],,cnt,val[x]);
  76. }
  77.  
  78. int main()
  79. {
  80. n=read();
  81. int x,y;
  82. for(int i=; i<=n; ++i) b[i].id=i, b[i].v=read();
  83. sort(b+,b+n+);
  84. for(int i=; i<=n; ++i)
  85. if( b[i].v>b[i-].v ) val[b[i].id]=++cnt;
  86. else val[b[i].id]=cnt;
  87. for(int i=; i<=n; ++i) {
  88. x=read(); add(x,i);
  89. }
  90. dfs();
  91. for(int i=; i<=n; ++i) printf("%d\n",ans[i]);
  92. return ;
  93. }

洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]的更多相关文章

  1. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  2. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  3. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

    P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...

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

    题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...

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

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

  6. 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  7. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...

  8. [USACO17JAN]Promotion Counting晋升者计数

    题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...

  9. 「洛谷4197」「BZOJ3545」peak【线段树合并】

    题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...

随机推荐

  1. 值得关注的sql-on-hadoop框架

    http://www.infoq.com/cn/news/2014/06/sql-on-hadoop 数据的操作语言是SQL,因此很多工具的开发目标自然就是能够在Hadoop上使用SQL.这些工具有些 ...

  2. SpringBoot 线程池配置 实现AsyncConfigurer接口方法

      目的是:  通过实现AsyncConfigurer自定义线程池,包含异常处理  实现AsyncConfigurer接口对异常线程池更加细粒度的控制 *a) 创建线程自己的线程池  b) 对void ...

  3. Nginx upstream的5种权重分配方式【转】

    原文地址:Nginx upstream的5种权重分配方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight指定轮询几率,weig ...

  4. HDU 1069 Monkey and Banana(最长递减子序列)

    题目链接 题意:摞长方体,给定长方体的长宽高,个数无限制,可随意翻转,要求下面的长方体的长和宽都大于上面的,都不能相等,问最多能摞多高. 题解:个数无限,其实每种形态最多就用一次,把每种形态都单独算一 ...

  5. Eclipse中如何改变主题

    童鞋们, eclipse主题太丑?想设置护眼的主题? 看看这些主题: 请移驾: Eclipse Color Themes 怎么设设置? 1. 打开”eclipse marketplace“, 如下图: ...

  6. 浏览器断点调试js

    说了一些 Chrome 开发者工具的技巧,其实并没有涉及到开发者工具最核心的功能之一:断点调试.断点可以让程序运行到某一行的时候,把程序的整个运行状态进行冻结.你可以清晰地看到到这一行的所有的作用域变 ...

  7. HTML 解析 textarea 中的换行符

    用户在textarea中输入的换行符,传到后台,再返回前端,展示在div中. 如果需要div显示为与textarea 一致的效果,需添加: .detail { white-space: pre-lin ...

  8. Ping程序的实现

    Ping程序的实现 在windows系统下进行cmd可以进行ping操作. ping命令是用来确定本地主机与网络中其他主机的网络通信情况,或者查看是否是为效IP. ping的工作原理:网络另一主机发送 ...

  9. Python标准库笔记(9) — functools模块

    functools 作用于函数的函数 functools 模块提供用于调整或扩展函数和其他可调用对象的工具,而无需完全重写它们. 装饰器 partial 类是 functools 模块提供的主要工具, ...

  10. 实现checkebox全选取消操作

    方法一: javascript代码: function checkedChild(obj,index){ var checkBoxs = document.getElementsByName(&quo ...