codeforces 792D - Paths in a Complete Binary Tree
#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的更多相关文章
- 【codeforces 792D】Paths in a Complete Binary Tree
[题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...
- PAT1110:Complete Binary Tree
1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- A1110. Complete Binary Tree
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 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 ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- 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 ...
- [二叉树建树&完全二叉树判断] 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 ...
- 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 ...
随机推荐
- SpringMVC由浅入深day02_8json数据交互
8 json数据交互 8.1 为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据 ...
- MySQL---循环语句
mysql 操作同样有循环语句操作,网上说有3中标准的循环方式: while 循环 . loop 循环和repeat循环.还有一种非标准的循环: goto. 鉴于goto 语句的跳跃性会造成使用的的思 ...
- scala中Map和Tuple
/** * Created by root * Description : Tuple and Map */ object MapTest { def main(args: Array[String] ...
- 【RF库Collections测试】Log Dictionary 【同log list】
Name:Log DictionarySource:Collections <test library>Arguments:[ dictionary | level=INFO ]Logs ...
- Ansible 远程执行脚本
1. 先在服务端创建一个 shell 脚本 [root@localhost ~]$ cat /tmp/test.sh #!/bin/bash echo "hello world" ...
- codeforces水题100道 第十六题 Codeforces Round #164 (Div. 2) A. Games (brute force)
题目链接:http://www.codeforces.com/problemset/problem/268/A题意:足球比赛中如果主场球队的主场球衣和客队的客队球衣颜色一样,那么要求主队穿上他们的可对 ...
- ASP/SQL 注入天书
引言 随着 B/S 模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对用户输入数据 ...
- Android设计和开发系列第一篇:Notifications通知(Develop—API Guides)
Notifications IN THIS DOCUMENT Design Considerations Creating a Notification Required notification c ...
- Qt编写机房安全作业预警系统
最近给一个朋友做了个项目,运行在一个日本大型企业中,大屏显示.机房安全作业预警系统工事主要是由监控系统和报警系统组成.其目的就是通过监控系统实时观察空压机房内的动态,通过报警系统确认空压机房在规定的时 ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...