#include<cstdio>
#include<iostream>
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
ll n,q,num,root;string s;
int main()
{
scanf("%I64d%I64d",&n,&q);
root=(n+1)/2;
for(ll q_i=1;q_i<=q;q_i++)
{
cin>>num>>s;
for(int step=0;step<s.size();step++)
{
ll lowbit_num=lowbit(num);//假设num这个节点是左子节点
if(s[step]=='U' && num!=root)
{
ll num_u=num+lowbit_num;//求出在假设情况下的num的父节点num_u
ll lowbit_num_u=lowbit(num_u);
if(num_u - lowbit_num_u/2 == num) num=num_u;//如果根据父节点求出来的左孩子就是num,那么num确实是左子节点
else num=num-lowbit_num;//否则num就是右子节点
}
if(s[step]=='L') num-=lowbit_num/2;
if(s[step]=='R') num+=lowbit_num/2;
}
printf("%I64d\n",num);
}
}
思路来自http://blog.csdn.net/Courage_kn/article/details/69218592
用#define比定义一个lowbit函数快……不过好像很多时候不能像函数那样随便用,容易出问题……
这是分别用
long long lowbit(long long x){return x&(-x);}
和
#define lowbit(x) x&(-x)
情况下的耗时……