Editorial for An Animal Contest 5 P3 - Ski Resort
Submitting an official solution before solving the problem yourself is a bannable offence.
Observe that for any given node, there is a certain interval of skills that will always reach it. Initially, node has an interval of . We will use the following process to calculate the rest of the intervals:
We perform a DFS down the tree. When a node is reached, we will sort its outgoing edges by difficulty level. Then we make the following observation: if there are two hills with difficulty and such that , a skier with a skill of will traverse down the more difficult hill if and only if . We use this property to calculate the intervals of the children of our current node (or you can skip this observation and use binary search).
Once all intervals are calculated, sort all the skills of the skiers. Then, use binary search to calculate the number of skiers who will visit each node.
We make the same observation: for any given node, if there are two hills connecting to it with difficulty and such that , a skier with a skill of will traverse down the more difficult hill if and only if . Using this property, we can maintain the path that a skier will take from the root to a leaf by processing the skiers in increasing order of skill and maintaining a priority queue for the edges. Details are left as an exercise to the reader.