Description

T is a complete binary tree consisting of n 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 n is a number such that n + 1 is a power of 2.

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

Vertices are numbered from 1 to n 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 n answers q queries to the tree.

Each query consists of an integer number ui (1 ≤ ui ≤ n) and a string si, where ui is the number of vertex, and si represents the path starting from this vertex. String si 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 si have to be processed from left to right, considering that ui 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 si ends.

For example, if ui = 4 and si = «UURL», then the answer is 10.

Input

The first line contains two integer numbers n and q (1 ≤ n ≤ 1018, q ≥ 1). n is such that n + 1 is a power of 2.

The next 2q lines represent queries; each query consists of two consecutive lines. The first of these two lines contains ui (1 ≤ ui ≤ n), the second contains non-empty string sisi doesn't contain any characters other than 'L', 'R' and 'U'.

It is guaranteed that the sum of lengths of si (for each i such that 1 ≤ i ≤ q) doesn't exceed 105.

Output

Print q numbers, i-th number must be the answer to the i-th query.

Example
input
15 2
4
UURL
8
LRLLLLLLLL
output
10
5
题意:根据给的这种满二叉树,U移动到父节点,R移动到右节点。L移动到左节点,当然不能继续移动不要用,问最后的数字是多少?
解法:把数字转成二进制,看看
num&(-num)得到当前节点同层的最左边的节点是什么 Pos=num&(-num),num&(Pos*2)得出当前在左边还是右边,然后就模拟
 #include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x7fffffff
#define INFL 0x7fffffffffffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
const int M = ;
LL n,m;
string s;
LL num;
LL ans;
int main()
{
cin>>n>>m;
ans=(n+)/;
for(int i=;i<=m;i++)
{
cin>>num>>s;
// cout<<s.size()<<endl;
// cout<<pos<<endl;
for(int j=;j<s.size();j++)
{
LL pos=(num&-num);
if(s[j]=='U'&&num!=ans)
{
if(num&(pos*))
{
num-=pos;
}
else
{
num+=pos;
}
}
else if(s[j]=='L')
{
pos/=;
num-=pos;
//cout<<num<<"C"<<endl;
}
else if(s[j]=='R')
{
pos/=;
num+=pos;
// cout<<num<<"D"<<endl;
}
}
cout<<num<<endl;
}
return ;
}

Educational Codeforces Round 18 D的更多相关文章

  1. Educational Codeforces Round 18

    A. New Bus Route 题目大意:给出n个不同的数,问差值最小的数有几对.(n<=200,000) 思路:排序一下,差值最小的一定是相邻的,直接统计即可. #include<cs ...

  2. Educational Codeforces Round 18 B

    Description n children are standing in a circle and playing the counting-out game. Children are numb ...

  3. Educational Codeforces Round 18 A

    Description There are n cities situated along the main road of Berland. Cities are represented by th ...

  4. Educational Codeforces Round 18 C. Divide by Three DP

    C. Divide by Three   A positive integer number n is written on a blackboard. It consists of not more ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  6. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  7. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  8. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  9. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

随机推荐

  1. Appium启动报Permission Denial的问题

    前言 在Android真机上跑自动化脚本时,发现在启动App时报java.lang.SecurityException: Permission Denial: starting Intent : 原先 ...

  2. 一个UserState(WCF)的小例子

    练习WCF的时候,遇到了一个异步调用(也许是)的问题.后来使用了重载参数UserState试着解决了一下,但不是很清楚这个参数的其他用途.现在贴出这个例子. Service部分: 客户端后台调用部分: ...

  3. SignatureDoesNotMatch REST接口 在任何时间、任何地点、任何互联网设备上 在Header中包含签名

    PutObject_关于Object操作_API 参考_对象存储 OSS-阿里云 https://help.aliyun.com/document_detail/31978.html OSS API ...

  4. SpringMvc參数的接受以及serializeArray的使用方法

    需求:从页面提交一个table中的数据到后台,通经常使用于批量改动 把全部的数据到放到 input属性中,设置name定义成为对象的相关属性,使用Jquery的serializeArray这种方法封装 ...

  5. PHP的date 函数

    <!DOCTYPE html> <html> <body> <?php echo "今天是 " . date("Y/m/d&qu ...

  6. POJ3045 Cow Acrobats —— 思维证明

    题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  7. jsp的4大作用域

    jsp的4大作用域 首先要声明一点,所谓“作用域”就是“信息共享的范围”,也就是说一个信息能够在多大的范围内有效.4个JSP内置对象的作用域分别为:application.session.reques ...

  8. [POI 2014] Couriers

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3524 [算法] 首先离线 , 将询问按右端点排序 如果我们知道[l , r]这个区间 ...

  9. 虚拟机bridged, NAT and host-only网络区别

    In Linux, a network of each type is created when running vmware-config.pl. In Windows, they are auto ...

  10. Bootstrap-CSS:辅助类

    ylbtech-Bootstrap-CSS:辅助类 1.返回顶部 1. Bootstrap 辅助类 本章将讨论 Bootstrap 中的一些可能会派上用场的辅助类. 文本 以下不同的类展示了不同的文本 ...