#P8575. 「DTOI-2」星之河
「DTOI-2」星之河
Background
The stars are sparse as the river’s shadow turns, the frost is heavy and the moonlight stands alone.
Problem Description
The ruler of the stars has a star chart, which can be abstracted as a tree rooted at node . Each node on the tree has a red star and a blue star, with brightness denoted by and .
Now, the ruler of the stars wants to know, for each node , how many nodes in its subtree (excluding the node itself) satisfy: their red star brightness is less than or equal to the red star brightness of , and their blue star brightness is less than or equal to the blue star brightness of .
You need to output the answer for each node in increasing order of node index. To reduce the amount of output, if the answer is , you do not need to output it.
Input Format
The first line contains an integer .
The next lines each contain two positive integers , indicating that the tree has an edge .
The next lines each contain two integers, representing and .
Output Format
For each node whose answer is non-zero, output one line containing an integer, which is the answer for that node.
10
2 1
3 1
4 3
5 1
6 4
7 2
8 2
9 4
10 3
3 1
2 4
-3 3
4 -2
-2 3
-3 -6
-5 -1
-4 -7
-5 -1
-7 -7
5
2
3
1
Hint
Sample Explanation
For node , the children that are less than or equal to it are , so output .
For node , the child that is less than or equal to it is , so output .
For nodes to , there are no children that are less than or equal to them, so do not output anything.
Constraints
| Special Property | Total Score | ||
|---|---|---|---|
| None | |||
| The tree is a chain | |||
| None |
For all testdata, it is guaranteed that and .
Translated by ChatGPT 5