logo AlgoBeat OnlineJudge
登录 注册

#215816. [JAG 2025 Summer Camp #1] Walking on Binary Tree

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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:

  • ()
  • 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".

样例

样例输入 1

5
LLRLR
3
1 12
9 2
913 2025

样例输出 1

7
2
23

样例输入 2

1
L
1
1 3

样例输出 2

-1