PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000175. Đếm cấp dưới giỏi hơn (Promotion Counting)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm cấp dưới giỏi hơn (Promotion Counting)

Promotion Counting

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

A company employs NN people numbered 11 through NN. Employee 11 is the president; every other employee ii has exactly one direct manager paripar_i. These relations form a tree rooted at 11. Employee ii has a proficiency rating pip_i, and all ratings are pairwise distinct. A subordinate of uu means any proper descendant of uu, including both direct and indirect reports; uu itself is never counted. For every employee uu, count the subordinates vv whose ratings satisfy the strict inequality pv>pup_v>p_u.

Input

The first line contains NN. The following NN lines give the ratings p1,…,pNp_1,\ldots,p_N (one per line). The next N−1N-1 lines give paripar_i for i=2,3,…,Ni=2,3,\ldots,N in this order. The management relation is a tree rooted at employee 11.

Output

Print exactly NN lines. The uu-th line contains the number of proper descendants with a strictly higher rating than employee uu. Each leaf contributes zero.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le 30; 1≤pi≤1091\le p_i\le10^9 and all proficiency ratings are distinct; root 1, exactly one parent paripar_i for each i=2..Ni=2..N, with 1≤pari≤N1\le par_i\le N, and these edges form a rooted tree.
  • Subtask 2 (30 points): 1≤N≤20001\le N\le 2000; 1≤pi≤1091\le p_i\le10^9 and all proficiency ratings are distinct; root 1, exactly one parent paripar_i for each i=2..Ni=2..N, with 1≤pari≤N1\le par_i\le N, and these edges form a rooted tree.
  • Subtask 3 (50 points): 1≤N≤1000001\le N\le 100000; 1≤pi≤1091\le p_i\le10^9 and all proficiency ratings are distinct; root 1, exactly one parent paripar_i for each i=2..Ni=2..N, with 1≤pari≤N1\le par_i\le N, and these edges form a rooted tree.

Examples

Example 1

Input

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

Output

2
0
1
0
0

Explanation

  • Employee 1: proficiency 804289384; descendants [2, 3, 4, 5]; higher-proficiency descendants [2, 5]. Output 2.
  • Employee 2: proficiency 846930887; descendants [4]; higher-proficiency descendants none. Output 0.
  • Employee 3: proficiency 681692778; descendants [5]; higher-proficiency descendants [5]. Output 1.
  • Employee 4: proficiency 714636916; descendants none; higher-proficiency descendants none. Output 0.
  • Employee 5: proficiency 957747794; descendants none; higher-proficiency descendants none. Output 0.

Example 2

Input

4
10
30
20
40
1
1
2

Output

3
1
0
0

Explanation

  • Employee 1: proficiency 10; descendants [2, 3, 4]; higher-proficiency descendants [2, 3, 4]. Output 3.
  • Employee 2: proficiency 30; descendants [4]; higher-proficiency descendants [4]. Output 1.
  • Employee 3: proficiency 20; descendants none; higher-proficiency descendants none. Output 0.
  • Employee 4: proficiency 40; descendants none; higher-proficiency descendants none. Output 0.