#P15759. [JAG 2025 Summer Camp #1] Walking on Binary Tree
[JAG 2025 Summer Camp #1] Walking on Binary Tree
题目描述
You are given an infinite complete binary tree whose vertices are labeled with positive integers. The root is vertex , and for every vertex (), its parent is .
You are also given a string of length . Each character of is either 'L' or 'R'.
Consider the following process: you are currently at some vertex , and want to reach vertex by repeatedly moving through the tree.
On the -th move (1-indexed), suppose you are at vertex . You may choose one of the following moves:
Downward move: If is 'L', move to vertex . Otherwise, move to vertex .
Upward move: You can choose only if . Move to vertex .
Note that you cannot stay at the same vertex.
You are given independent queries. In the -th query, you start at vertex and want to reach vertex .
For each query, determine whether it is possible to reach from . If it is possible, find the minimum number of moves required.
输入格式
The input is given in the following format:
$$\begin{aligned} & N \\ & S \\ & Q \\ & u_1 \ v_1 \\ & u_2 \ v_2 \\ & \vdots \\ & u_Q \ v_Q \end{aligned}$$- ()
- and are integers.
- is a string of length consisting of 'L' and 'R'.
输出格式
Output lines. On the -th line, print the minimum number of moves required to reach from if it is possible; otherwise, print "-1".
5
LLRLR
3
1 12
9 2
913 2025
7
2
23
1
L
1
1 3
-1