Educational Codeforces Round 18D(完全二叉树中序遍历&lowbit)
题目链接: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)的更多相关文章
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- Educational Codeforces Round 81 + Gym 102267
UPD:变色了!!!历史最高1618~ Educational Codeforces Round 81 (Rated for Div. 2) The 2019 University of Jordan ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 64(ECR64)
Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...
- Educational Codeforces Round 69 D E
Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
随机推荐
- Topcoder SRM 638 DIV 2 (大力出奇迹)
水题,就是一个暴力.大力出奇迹. Problem Statement There is a narrow passage. Inside the passage there are some wo ...
- IOS8 TouchID使用介绍
本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35555123 IOS8将指纹识别技术开放出来了.我们能够利用用户设置的touch I ...
- 3932: [CQOI2015]任务查询系统
3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2559 Solved: 819[Submit][Sta ...
- 通过css选择器class给元素添加cursor的坑
笔者在chrome中遇到了奇特的问题,在通过class给元素添加cursor的自定义图片时.出现了"Invald property value"提示,crosshair.help等 ...
- 九度OJ 1094:String Matching(字符串匹配) (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1259 解决:686 题目描述: Finding all occurrences of a pattern in a text is a p ...
- window.onerror 错误监听,发到后台
var doc = document.body || document.documentElement; var _onerror = Onerror(''); var Onerror = funct ...
- C++ xml 解析器
C++的xml解析器有很多,这个知乎回答里有一个列表:https://www.zhihu.com/question/32046606 下面使用其中的RapidXml试试. 官方地址: https:// ...
- linux mount的安装配置
1.首先需要安装nfs-utils 和 portmap: yum install nfs-utils portmap (安装软件,也可直接安装 yum install nfs* portmap)chk ...
- HttpServlet cannot be resolved to a type解决方法
1:是因为没有加入servlet-api.jar 2:下载网址:http://download.csdn.net/detail/jiuyueguang/5745209 3:然后在项目右键->bu ...
- MYSQL进阶学习笔记十五:MySQL 的账号权限赋予!(视频序号:进阶_33,34)
知识点十六:MySQL的账号权限赋予(33) 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执 ...