Under construction

Still being built. Things may change

Promotion Counting

Time limit2sMemory limit512 MB

Summary
For each node of a rooted tree, count descendants whose value is larger than the node's own value.
Level

Medium7 of 10

Topics
Tree, DFS, Segment tree
Solved
No attempts yet

Problem

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!

The cows are numbered 11 to NN (1≤N≤100,0001 \le N \le 100{,}000). They 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 proficiency rating p(i)p(i) that describes how good she is at her job, and all ratings are distinct. If cow ii is an ancestor of cow jj (for example, a manager of a manager of a manager), then jj is a subordinate of ii.

Unfortunately, a manager often has less proficiency than several of her subordinates, in which case the manager should consider promoting some of them. Help the cows figure out when this is happening. For each cow ii in the company, count the number of subordinates jj with p(j)>p(i)p(j) > p(i).

Input

The first line contains NN.

The next NN lines contain the proficiency ratings p(1),p(2),…,p(N)p(1), p(2), \ldots, p(N), one per line. Each is a distinct integer from 1 to 1,000,000,000.

The next N−1N-1 lines give the manager (parent) of cows 2,3,…,N2, 3, \ldots, N, in that order. Cow 1 is the president and has no manager.

Output

Print NN lines. Line ii contains the number of subordinates of cow ii whose proficiency is higher than that of cow ii.

Examples1

  1. Example 1

    Input
    5
    804289384
    846930887
    681692778
    714636916
    957747794
    1
    1
    2
    3
    
    Expected output
    2
    0
    1
    0
    0