#P12153. 【MX-X11-T7】「蓬莱人形 Round 1」信念
【MX-X11-T7】「蓬莱人形 Round 1」信念
Description
Given a rooted tree with nodes (numbered to , root is node ).
Each node has a pair where:
- is the distance from to the root.
- is the string formed by concatenating the IDs of all nodes along the simple path from the root to (the character set consists of node IDs).
There are queries. For each query , compute as follows:
- Find the set of node IDs in the -neighborhood.
- Sort in ascending order by their pairs (using as the primary key and 's lexicographical order as the secondary key). Let the sorted list be .
- is defined as $\sum_{i=1}^{|S|-1} \operatorname{Dis}(c_i, c_{i+1})$.
Definitions:
- : The number of edges on the unique simple path between and .
- -neighborhood: The set of nodes whose distance from is .
Input Format
- First line: Two integers (number of nodes and queries).
- Second line: integers where the -th integer is the parent of node .
- Next lines: Two integers per line (query parameters).
Output Format
Output lines, each containing the answer to the corresponding query.
10 3
1 2 2 3 4 5 6 6 9
1 3
3 2
4 3
11
8
15
10 10
1 2 2 3 4 5 6 6 9
1 3
3 2
4 3
10 3
2 2
3 3
5 3
1 0
7 2
1 2
11
8
15
5
11
16
8
0
2
4
Hint
Explanation #1

The pairs for each node are (where "|" is used only to separate characters in strings):
| Node ID | Pair |
|---|---|
For query :
- sequence:
- $f(1,3) = \operatorname{Dis}(1,2)+\operatorname{Dis}(2,3)+\operatorname{Dis}(3,4)+\operatorname{Dis}(4,5)+\operatorname{Dis}(5,6) = 11$.
For query :
- sequence:
- .
For query :
- sequence:
- .
Constraints
This problem uses subtask scoring.
For all test data: , , , .
| Subtask | Special Property | Points | |
|---|---|---|---|
| 1 | None | 5 | |
| 2 | A | 25 | |
| 3 | B | 10 | |
| 4 | None | 15 | |
| 5 | |||
| 6 | 30 |
- Special Property A: Parent nodes are randomly generated within valid ranges.
- Special Property B: All leaf nodes have the same depth.
Note: Use fast I/O methods for this problem.
Translated by DeepSeek R1
京公网安备 11011102002149号