#P3605. [USACO17JAN] Promotion Counting P

    ID: 785 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>2017线段树USACO树状数组离散化深度优先搜索DFS

[USACO17JAN] Promotion Counting P

题目描述

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 1N1 \ldots N (1N100,0001 \leq N \leq 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).

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

为了方便,把奶牛从 1n1\sim n 编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。

所有的第 ii 头牛都有一个不同的能力指数 pip_i,描述了她对其工作的擅长程度。如果奶牛 ii 是奶牛 jj 的祖先节点,那么我们我们把奶牛 jj 叫做 ii 的下属。

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

输入格式

The first line of input contains NN.

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

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

输入的第一行包括一个整数 nn

接下来的 nn 行包括奶牛们的能力指数 p1,p2pnp_1,p_2 \dots p_n。保证所有数互不相同。

接下来的 n1n-1 行描述了奶牛 2n2 \sim 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 能力高。

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
2
0
1
0
0

提示

感谢@rushcheyo 的翻译

【数据范围】
对于 100%100\% 的数据,1n1051\le n \le 10^51pi1091 \le p_i \le 10^9