#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+)/;
for(ll q_i=;q_i<=q;q_i++)
{
cin>>num>>s;
for(int step=;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/ == num) num=num_u;//如果根据父节点求出来的左孩子就是num,那么num确实是左子节点
else num=num-lowbit_num;//否则num就是右子节点
}
if(s[step]=='L') num-=lowbit_num/;
if(s[step]=='R') num+=lowbit_num/;
}
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)

情况下的耗时……

codeforces 792D - Paths in a Complete Binary Tree的更多相关文章

  1. 【codeforces 792D】Paths in a Complete Binary Tree

    [题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...

  2. PAT1110:Complete Binary Tree

    1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  4. A1110. Complete Binary Tree

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  5. PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  6. PAT 甲级 1110 Complete Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...

  7. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

  8. [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)

    1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...

  9. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

随机推荐

  1. SpEL、PropertyPlaceholderConfigurer与@Value、#{}、${}

    概念 SpEL:Spring EL表达式 PropertyPlaceholderConfigurer:即org.springframework.beans.factory.config.Propert ...

  2. tomcat 的 server.xml配置文件

    tomcat的配置文件在其安装后生成的conf目录下,其中主配置文件便是conf下的server.xml文件. server.xml文件由server->service->engine-& ...

  3. NetBpm 示例:请假审批(6)

    转载注明出处: http://www.cnblogs.com/anbylau2130/p/3877983.html 原文: 请假示例 流程定义包源码下载(注:par包就是zip格式压缩包).原文地址: ...

  4. Oracle之表空间基于时间点的恢复

    记一次优化过程中:一次误操作,在不影响其他表空间的情况下:采用表空间基于时间点的恢复(TSPITR)方法恢复数据的过程. 1.TSPITR恢复原理    TSPITR目前最方便的方法是使用RMAN进行 ...

  5. Java使用dom4j读取xml时报错:org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence

    1.Java使用dom4j读取xml时报错: org.dom4j.DocumentException: Error on line 2 of document  : Invalid byte 2 of ...

  6. api 25 PopupWindow会占据整个屏幕

    解决方法:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Android 7.x中,PopupWindow高度为match_pa ...

  7. codeforces水题100道 第十五题 Codeforces Round #262 (Div. 2) A. Vasya and Socks (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/460/A题意:Vasya每天用掉一双袜子,她妈妈每m天给他送一双袜子,Vasya一开始有n双袜子, ...

  8. linux下的shell操作mysql

    (1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...

  9. xmapp 404设置

    这样做的好处一个是很友好,另一个是对于你的网站会更安全些,如果没设置,别人在你的网址后随便输入一个路径,会显示404错误,并且会显示你的服务器版本号,服务器配置一目了然,为了避免这种情况,可以设置错误 ...

  10. 【大数据系列】MapReduce示例一年之内的最高气温

    一.项目采用maven构建,如下为pom.xml中引入的jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...