CF792D.Paths in a Complete Binary Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

TT is a complete binary tree consisting of nn vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So nn is a number such that n+1n+1 is a power of 22 .

In the picture you can see a complete binary tree with n=15n=15 .

Vertices are numbered from 11 to nn in a special recursive way: we recursively assign numbers to all vertices from the left subtree (if current vertex is not a leaf), then assign a number to the current vertex, and then recursively assign numbers to all vertices from the right subtree (if it exists). In the picture vertices are numbered exactly using this algorithm. It is clear that for each size of a complete binary tree exists exactly one way to give numbers to all vertices. This way of numbering is called symmetric.

You have to write a program that for given nn answers qq queries to the tree.

Each query consists of an integer number uiu_{i} ( 1<=ui<=n1<=u_{i}<=n ) and a string sis_{i} , where uiu_{i} is the number of vertex, and sis_{i} represents the path starting from this vertex. String sis_{i} doesn't contain any characters other than 'L', 'R' and 'U', which mean traverse to the left child, to the right child and to the parent, respectively. Characters from sis_{i} have to be processed from left to right, considering that uiu_{i} is the vertex where the path starts. If it's impossible to process a character (for example, to go to the left child of a leaf), then you have to skip it. The answer is the number of vertex where the path represented by sis_{i} ends.

For example, if ui=4u_{i}=4 and si=s_{i}= «UURL», then the answer is 1010 .

输入格式

The first line contains two integer numbers nn and qq ( 1<=n<=10181<=n<=10^{18} , q>=1q>=1 ). nn is such that n+1n+1 is a power of 22 .

The next 2q2q lines represent queries; each query consists of two consecutive lines. The first of these two lines contains uiu_{i} ( 1<=ui<=n1<=u_{i}<=n ), the second contains non-empty string sis_{i} . sis_{i} doesn't contain any characters other than 'L', 'R' and 'U'.

It is guaranteed that the sum of lengths of sis_{i} (for each ii such that 1<=i<=q1<=i<=q ) doesn't exceed 10510^{5} .

输出格式

Print qq numbers, ii -th number must be the answer to the ii -th query.

输入输出样例

  • 输入#1

    15 2
    4
    UURL
    8
    LRLLLLLLLL
    

    输出#1

    10
    5
    
首页