【codeforces 792D】Paths in a Complete Binary Tree
【题目链接】:http://codeforces.com/contest/792/problem/D
【题意】
给你一棵满二叉树;
给你初始节点;
给你若干个往上走,左走,右走操作;
让你输出一系列操作结束之后节点的位置;
【题解】
这个节点的标志方式类似树状数组;
用树状数组左走右走就好;
L->x-=lowbit(x)/2;
R->x+=lowbit(x)/2;
U->先假设x是左儿子,然后依照规则求出父亲F,然后看看F的左儿子是不是真的是x,是的话爸爸就是F,否则x是右儿子,然后根据x是右儿子求出对应的父亲节点就好;
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
LL n,q,x;
int len;
char s[N];
LL lowbit(LL x){return x&(-x);}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rel(n),rel(q);
while (q--)
{
rel(x);
scanf("%s",s+1);
len = strlen(s+1);
rep1(i,1,len)
{
LL t = lowbit(x);
if (s[i]=='L')//向左走
{
x-=t/2;
}
else
if (s[i]=='R')//向右走
{
x+=t/2;
}
else
{
if (x==(n+1)/2) continue;//是根节点就跳过
//假设x是爸爸的左儿子
LL baba = x+t;
LL zbaba = baba-lowbit(baba)/2;//然后求出这个假装爸爸的真左儿子
if (zbaba==x)
{
x = baba;
}
else//是右儿子;
{
x = x-t;
}
}
}
printf("%lld\n",x);
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 792D】Paths in a Complete Binary Tree的更多相关文章
- 【codeforces 870F】Paths
Description You are given a positive integer n. Let's build a graph on vertices 1, 2, ..., n in such ...
- codeforces 792D - Paths in a Complete Binary Tree
#include<cstdio> #include<iostream> #define lowbit(x) x&(-x) typedef long long ll; u ...
- 【leetcode❤python】 111. Minimum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【PAT 甲级】1151 LCA in a Binary Tree (30 分)
题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 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 ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
随机推荐
- 杂项-JSP-Runoob:JSP 标准标签库(JSTL)
ylbtech-杂项-JSP-Runoob:JSP 标准标签库(JSTL) 1.返回顶部 1. JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通 ...
- 客户端JavaScript Ajax
创建: 2017/10/21 完成: 2017/10/23 [TODO] 对Ajax收发各类型数据制作模板 补充跨域通信(cross origin) p457 HTTP通信 HTTP 超文本 ...
- 用JavaScript实现歌词滚动播放
各种音乐播放器上都有一个自动滚动播放歌词的功能,那么这个功能用JavaScript怎么实现呢?请看下文. 一般音乐播放器使用的歌词格式都是lrc,为了方便处理,我们这里使用XML格式的歌词.介绍一个网 ...
- js将字符串中所有反斜杠\替换成正斜杠/
区分正斜杠与反斜杠: 正斜杠:http://.http紧跟着的斜杠,离手输入最近的斜杠,shift中间斜杠.45度角斜杠.正斜杠不需要转义 反斜杠:回车与空格之间.程序表示时需要转义成\\. str. ...
- python值函数名的使用以及闭包,迭代器
一.函数名的运用 函数名就是一个变量名,但它是一个特殊的变量名,是一个后面加括号可以执行函数的变量名. def func(): print("我是一个小小的函数") a = fun ...
- 去掉myeclipse的预览窗口
1,选择菜单: windows -> preferences2,在弹出窗口中选择General-> Editors -> FileAssociations3,在上方框内选择*.jsp ...
- Coursera公开课-Machine_learing:编程作业
第二周编程作业:Linear Regression 分为单一变量和多变量,假想函数为:hθ(x)=θ0+θ1x1+θ2x2+θ3x3+⋯+θnxn.明显已经包含单一变量的情况,所以完成多变量可以一并解 ...
- C# Nugut CsvHelper 使用
装载自: 跳转链接>>>
- PHP常用的一些函数:
背景:这一次是对一些函数进行整理,方便以后的使用. 1.date(); date()函数的作用是获取当前日期时间,由于PHP 5.0对date()函数进行了重写,因此,当前的日期时间函数比系统时间少了 ...
- Android基础TOP3:Activity的线性,相对,帧和表格布局的概括
线性布局 LinearLayout: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...