#P1797. 【模板】Stern-Brocot 树
【模板】Stern-Brocot 树
Description
Conventions:
- In this problem, all fractions in the input/output must be in lowest terms.
- In this problem, every input fraction satisfies .
Task 1: ENCODE_PATH
- Input format:
ENCODE_PATH; - Output format: .
Given a fraction , output the path from the root of the Stern-Brocot tree to the node in the following format:
Here means go to the left or right child, and is how many times to repeat . You must ensure , .
Task 2: DECODE_PATH
- Input format:
DECODE_PATH; - Output format: .
Given a path from the root in the same format as in Task 1, output the fraction on that node. It is guaranteed that the answer satisfies .
Task 3: LCA
Given and , output the fraction on the LCA of and .
- Input format:
LCA; - Output format: .
Task 4: ANCESTOR
Given and , output the value of the node whose depth is on the chain from up to the root. If it does not exist, output .
- Input format:
ANCESTOR; - Output format: .
- Constraints: .
Task 5: RANGE
Given , let be the set of all fractions in the subtree of in the Stern-Brocot tree. Output and .
In particular, for , output ; for , output .
- Input format:
RANGE; - Output format: .
Input Format
The first line contains a positive integer , the number of test cases.
Each of the next lines describes one task, in the formats given in the Description.
Output Format
Output lines, each containing the answer to the corresponding task, in the formats given in the Description.
10
DECODE_PATH 0
DECODE_PATH 3 R 2 L 1 R 2
ENCODE_PATH 1 1
ENCODE_PATH 5 11
LCA 2 3 3 5
LCA 5439 19294 8291 29410
ANCESTOR 10 1 1
ANCESTOR 300 1000000000 1
RANGE 1 1
RANGE 11 8
1 1
11 4
0
2 L 2 R 4
2 3
148 525
-1
301 1
0 1 1 0
4 3 7 5
Hint
- .
- All input fractions are in lowest terms.
- Every input fraction satisfies .
Translated by ChatGPT 5
京公网安备 11011102002149号