PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000064. Các phép toán trên cây Stern–Brocot (Stern-Brocot Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 a/ba/b and c/dc/d, and has value (a+c)/(b+d)(a+c)/(b+d). The root uses bounds 0/10/1 and 1/01/0, giving value 1/11/1; 1/01/0 denotes positive infinity. From a node u/vu/v, an L step replaces its right bound by u/vu/v; an R step replaces its left bound by u/vu/v. 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 00. 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 a/ba/b into direction/count groups. The root has an empty path and zero groups.
  • DECODE_PATH k c1 n1 ... ck nk: follow the kk 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-a/ba/b and root-to-c/dc/d paths. A node counts as its own ancestor.
  • ANCESTOR k a b: find the node at depth exactly kk on the path to a/ba/b. This is a depth measured from the root, not a number of upward steps. The requested node does not exist if kk exceeds the depth of a/ba/b.
  • RANGE a b: return the two bounds that determine node a/ba/b under the construction above. Their open interval contains the values of the subtree rooted at a/ba/b, 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 kk, followed by the kk direction/count pairs. For example, 3 R 1 L 1 R 1. Print only 0 for an empty path.
  • DECODE_PATH or LCA: 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 -1 otherwise.
  • RANGE: for left and right bounds p/qp/q and r/sr/s, print p q r s. Represent zero by 0 1 and positive infinity by 1 0.

Subtasks

  • Subtask 1 (20 points): All input/output numerators and denominators at most 100100; queried depths at most 100100.
  • Subtask 2 (30 points): All input/output numerators and denominators at most 10510^5; queried depths at most 10510^5.
  • Subtask 3 (50 points): All input/output numerators and denominators at most 10910^9; 0≤k≤1090\le k\le 10^9 in ANCESTOR; every DECODE_PATH result has numerator and denominator at most 10910^9.

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.