1 #include<cstdio> 2 #include<iostream> 3 #define lowbit(x) x&(-x) 4 typedef long long ll; 5 using namespace std; 6 ll n,q,num,root;string s; 7 int main() 8 { 9 scanf("%I64d%I64d",&n,&q); 10 root=(n+1)/2; 11 for(ll q_i=1;q_i<=q;q_i++) 12 { 13 cin>>num>>s; 14 for(int step=0;step<s.size();step++) 15 { 16 ll lowbit_num=lowbit(num);//假设num这个节点是左子节点 17 if(s[step]=='U' && num!=root) 18 { 19 ll num_u=num+lowbit_num;//求出在假设情况下的num的父节点num_u 20 ll lowbit_num_u=lowbit(num_u); 21 if(num_u - lowbit_num_u/2 == num) num=num_u;//如果根据父节点求出来的左孩子就是num,那么num确实是左子节点 22 else num=num-lowbit_num;//否则num就是右子节点 23 } 24 if(s[step]=='L') num-=lowbit_num/2; 25 if(s[step]=='R') num+=lowbit_num/2; 26 } 27 printf("%I64d ",num); 28 } 29 }
思路来自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)
情况下的耗时……