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\),问最多能抽出多少个数, ...
随机推荐
- EasyDarwin开源流媒体服务器如何实现按需推送直播的
--本文转自EasyDarwin开源团队成员邵帅的博客:http://blog.csdn.net/ss00_2012/article/details/51441753 我们使用EasyDarwin的推 ...
- 九度OJ 1124:Digital Roots(数根) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2963 解决:1066 题目描述: The digital root of a positive integer is found by s ...
- machine learning for hacker记录(2) 数据分析
本章主要讲了对数据的一些基本探索,常见的six numbers,方差,均值等 > data.file <- file.path('data', '01_heights_weights_ge ...
- 消息handler message 线程通信 空消息
空消息的使用 private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { ...
- html body div height: 100%;
最近做了测试 html{ height: 100%;//全部内容高度,包括滚动出现的内容 background-color:#000;} body{height: 100%;//只一页屏幕,用作滚动的 ...
- HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- Gym - 100283K K. Cubes Shuffling —— 贪心
题目链接:http://codeforces.com/gym/100283/problem/K 题解: 要使其相邻两项的差值之和最小,那么越靠中间,其数值越小. 那么剩下的问题就是如何放数字了.一开始 ...
- H264视频通过RTMP直播
http://blog.csdn.net/firehood_/article/details/8783589 前面的文章中提到了通过RTSP(Real Time Streaming Protocol) ...
- linux应用之openssh server安装及配置(centos)
安装OpenSSH Server 首先,我们搜索一下CentOS的软件库里面有没有已经定义好的SSH服务器包: $ yum search ssh ... ... openssh.x86_64 : An ...
- [APIO 2014] 序列分割
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3675 [算法] 首先 , 我们发现将一段序列切成若干段所获得的收益与顺序无关 于是我 ...