题目链接:http://codeforces.com/contest/792/problem/D

题意:第一行输入n, q,分别表示给出一颗n个节点的中序遍历满二叉树,后面有q个询问;

接下来有q组形如:

  x

  str

的输入,x为当前所在节点的序号,str为一个操作字符串,对于其中每一个字符, 若其为 U 移向其父亲节点,L移向其左儿子,R移向其右儿子。如果将要移出树外,则本次不移动。

思路:找规律。

通过找规律可以发现,对于当前`x,若其向左移动,则x'=x-lowbit(x)/2,若其向右移动,则x'=x+lowbit(x)/2,

若其向上移动,则x有两种可能:x'=x+lowbit(x), x'=x-lowbit(x), 通过规律可以发现 lowbit(x')=2*lowbit(x), 通过这点可以判断出x'的取值;

注意取值的边界条件;

其中lowbit(x)=(-x)&x,其值为: 2^p,其中p为x的二进制表示从左往右第一个1之前的0的个数。

代码:

 #include <iostream>
#define ll long long
using namespace std; int main(void){
ll n;
int q;
cin >> n >> q;
while(q--){
ll x;
string str;
cin >> x >> str;
for(int i=; i<str.size(); i++){
ll cnt=(-x)&x;
switch (str[i]){
case 'U': {
ll cc1=x+cnt;
ll cc2=x-cnt;
ll cnt1=(-cc1)&cc1;
ll cnt2=(-cc2)&cc2;
if(cnt1==cnt*&&cc1<=n){
x=cc1;
}else if(cnt2==cnt*&&cc2<=n){
x=cc2;
}
break;
}
case 'L': {
if(x-cnt/>){
x-=cnt/;
}
break;
}
case 'R': {
if(x+cnt/<=n){
x+=cnt/;
}
break;
}
}
}
cout << x << endl;
}
return ;
}

Educational Codeforces Round 18D(完全二叉树中序遍历&lowbit)的更多相关文章

  1. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  2. Educational Codeforces Round 81 + Gym 102267

    UPD:变色了!!!历史最高1618~ Educational Codeforces Round 81 (Rated for Div. 2) The 2019 University of Jordan ...

  3. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  4. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  5. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  6. Educational Codeforces Round 64(ECR64)

    Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...

  7. Educational Codeforces Round 69 D E

    Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...

  8. Educational Codeforces Round 64部分题解

    Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...

  9. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

随机推荐

  1. HDU 5313 Bipartite Graph(二分图染色+01背包水过)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  2. crazyflie2.0 RCC时钟知识

    因为眼下手里仅仅有16MHZ的2520封装的贴片晶振,8MHZ这样的封装做不到这么小,所以就先用16MHZ,这样我们就须要改动程序相关的RCC时钟: 1,stm32f4xx.h #define HSE ...

  3. spring mvc 设置设置默认首页的方式

    背景: 项目使用springmvc管理请求,有一个小的需求,输入域名的时候自动进入某个页面(或者说自动发起某个请求). 过程: 1,首先想到 在web.xml中配置welcome-file-list的 ...

  4. Recurrent neural networks are very powerful, because they combine two properties

    https://www.cs.toronto.edu/~hinton/csc2535/notes/lec10new.pdf Distributed hidden state that allows t ...

  5. Linux常用命令——持续更新(2018-05-09)

    此命令默认是在centos环境下执行,除非特殊标明. 1.查看ip: ifconfig 2.创建指定用户并分配到某个组:创建用户user并分配到root组 useradd -g root user 3 ...

  6. Redis学习笔记(1):Redis的说明与安装

    Redis学习笔记(1):Redis说明的安装 说明 什么是Redis REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-va ...

  7. [NOIP2011提高组day2]-2-聪明的质监员

    2.聪明的质监员(qc.cpp/c/pas) [问题描述] 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到 n 逐一编号,每个矿石都有自己的重量 wi 以及价 ...

  8. CentOS(Linux) - 安装软件笔记(总) - 开发环境安装顺序及汇总

    1.安装java环境 参考文章 CentOS7.1 使用资源搜集 2.需要可视化管理服务器时,需要先安装VPSmate 参考文章 CentOS(Linux) - 安装软件笔记(一) - VPSMate ...

  9. 客户端调用cxf发布的服务

    import java.util.ArrayList; import java.util.List; import javax.xml.namespace.QName; import org.apac ...

  10. js中的命名空间

    尽量不要使用全局变量,防止环境污染和命名冲突. 所以,将全局变量放在一个命名空间下,是一个好的解决方案. 静态命名空间 1. 直接赋值 这是最基本的方法,但是它很啰嗦,你得重复书写多次变量名.好处是它 ...