题目链接: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. LeetCode(155)题解--Min Stack

    https://leetcode.com/problems/min-stack/ 题目: Design a stack that supports push, pop, top, and retrie ...

  2. ajax跨域请求的问题

    使用getJson跨域请求,需要向服务器发送一个参数callback=? $.getJSON("http://appcenter.mobitide.com/admin/appSearch.p ...

  3. root无权限删除 原因 进程 占用 文件

    [root@test opt]# find / | grep gitlab | xargs rm -rfrm: cannot remove ‘/sys/fs/cgroup/devices/system ...

  4. appium(12)-The starting of an app

    Steps: you have to prepare environment for Android. Details are provided here: http://appium.io/slat ...

  5. 使用官方Android-support-v7在低版本上使用ActionBarActivity

    昨天晚上更新了下Android SDK Manager,发现Extras下的Android Support Library已经更新到19.1了,上网一查原来是sdk\extras\android\su ...

  6. 7-2 jmu-python-猜数游戏(10 point(s)) 【python】

    7-2 jmu-python-猜数游戏(10 point(s)) 用户从键盘输入两个整数,第一个数是要猜测的数n(<10),第二个数作为随机种子,随机生成一个1~10的整数,如果该数不等于n,则 ...

  7. cookie VS sessionstorge VS localstorge

    虽然cookie , localstorge , sessionstorge三者都有存储的功能,但是还是有区别, git上地址:https://github.com/lily1010/cookie-s ...

  8. Unable to create Debug Bridge:Unable to start adb server:error:cannot parse version

    打开Android Studio时报如下错误提示: Unable to create Debug Bridge:Unable to start adb server:error:cannot pars ...

  9. 机器学习: 特征脸算法 EigenFaces

    人脸识别是机器学习和机器视觉领域非常重要的一个研究方向,而特征脸算法是人脸识别里非常经典的一个算法,EigenFaces 是基于PCA (principal component analysis) 即 ...

  10. configure: error: APR not found. Please read the documentation

    本以为Apache的编译安装很简单,其实不然: 以前的环境下编译报错很少 ,但这次不行了 提示configure: error: APR not found. Please read the docu ...