#GL0000064. Các phép toán trên cây Stern–Brocot (Stern-Brocot Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Các phép toán trên cây Stern–Brocot (Stern-Brocot Tree)
Stern-Brocot Tree
Source: Library Checker
Version: Phuoc Hung OJ Extended
Problem Statement
A node of the Stern–Brocot tree is determined by bounds and , and has value . The root uses bounds and , giving value ; denotes positive infinity. From a node , an L step replaces its right bound by ; an R step replaces its left bound by . The other bound is retained. Compute the child from the new bounds by adding numerators and denominators.
The depth of a node is the number of edges from the root to that node. The root has depth . Consecutive identical steps can be grouped: for example, RRRLLR is encoded as R 3 L 2 R 1.
Process exactly one command:
ENCODE_PATH a b: encode the path from the root to into direction/count groups. The root has an empty path and zero groups.DECODE_PATH k c1 n1 ... ck nk: follow the given direction/count groups from the root and recover the final node.LCA a b c d: find the deepest node shared by the root-to- and root-to- paths. A node counts as its own ancestor.ANCESTOR k a b: find the node at depth exactly on the path to . This is a depth measured from the root, not a number of upward steps. The requested node does not exist if exceeds the depth of .RANGE a b: return the two bounds that determine node under the construction above. Their open interval contains the values of the subtree rooted at , including this node.
Input
One command name followed by the parameters described in the statement. Every numerator–denominator pair representing a node consists of positive coprime integers. In ANCESTOR, k is the required depth from the root, rather than the number of edges to move upward. Each path run consists of a direction L or R and a positive count; consecutive directions differ.
Output
Print one line according to the command:
ENCODE_PATH: print the number of groups , followed by the direction/count pairs. For example,3 R 1 L 1 R 1. Print only0for an empty path.DECODE_PATHorLCA: print the numerator and denominator, separated by a space, without a slash.ANCESTOR: print the numerator and denominator if the requested node exists, or print only-1otherwise.RANGE: for left and right bounds and , printp q r s. Represent zero by0 1and positive infinity by1 0.
Subtasks
- Subtask 1 (20 points): All input/output numerators and denominators at most ; queried depths at most .
- Subtask 2 (30 points): All input/output numerators and denominators at most ; queried depths at most .
- Subtask 3 (50 points): All input/output numerators and denominators at most ; in ANCESTOR; every DECODE_PATH result has numerator and denominator at most .
Examples
Input
ENCODE_PATH 5 3
Output
3 R 1 L 1 R 1
Explanation
The path from 1/1 to 5/3 is R, L, R, producing three groups.