#P7543. [CEOI 2011] Treasure Hunt
[CEOI 2011] Treasure Hunt
Description
You are given a tree that initially has only one root node . You need to process the following two types of operations:
path u smeans that nodes are added one by one under node : the -th added node is the child of the -th added node . In particular, the first added node is a child of . Suppose that before adding, the maximum node label in the tree is . Then the new nodes will be labeled consecutively as .dig u vasks for the midpoint of and . Formally, let the distance between and be . You need to output the label of the node on the path from to whose distance from is .
Input Format
This is an interactive problem. First, you need to read the number of operations from standard input.
Then, for the next operations, you will receive one of the following two command formats:
P u srepresents onepath u soperation.D u vrepresents onedig u voperation.
For the first type, you do not need to output anything. For the second type, you must output the answer and flush the output buffer before you can read subsequent operations.
After you output a line, please flush the buffer:
- In C++, use fflush(stdout) or cout.flush().
- In Pascal, use flush(output).
- In Python, use stdout.flush().
- For other languages, please check the documentation.
Output Format
For each D u v operation, output one line containing the answer. It is guaranteed that there is at least one such operation.
11
P 1 2
D 1 3
P 2 5
D 7 3
D 3 7
P 1 2
P 3 3
D 10 11
P 5 1
D 14 8
D 2 4
2
5
4
1
6
2
Hint
For of the testdata, the maximum node label in the final tree does not exceed , and .
For of the testdata, the maximum node label in the final tree does not exceed .
For of the testdata, the maximum node label in the final tree does not exceed , and .
Notes
This problem is translated from CEOI 2011 day 1 T3 Treasure Hunt。
Translated by ChatGPT 5
京公网安备 11011102002149号