Promotion Counting
Time limit2sMemory limit512 MB
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 to (). 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 has a proficiency rating that describes how good she is at her job, and all ratings are distinct. If cow is an ancestor of cow (for example, a manager of a manager of a manager), then is a subordinate of .
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 in the company, count the number of subordinates with .
Input
The first line contains .
The next lines contain the proficiency ratings , one per line. Each is a distinct integer from 1 to 1,000,000,000.
The next lines give the manager (parent) of cows , in that order. Cow 1 is the president and has no manager.
Output
Print lines. Line contains the number of subordinates of cow whose proficiency is higher than that of cow .