【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 ...
随机推荐
- linux用户态和内核态通信之netlink机制【转】
本文转载自:http://blog.csdn.net/zcabcd123/article/details/8272360 这是一篇学习笔记,主要是对<Linux 系统内核空间与用户空间通信的实现 ...
- CF36 E Two Paths——欧拉(回)路
题目:http://codeforces.com/contest/36/problem/E 给定一张无向图,要求输出两条欧拉路覆盖所有边: 分类讨论,首先判-1:有两个以上连通块 / 有四个以上奇度数 ...
- centos6.4 ssh免密码登陆(只需三个步骤)
学习Hadoop的时候,用到的.这里作为记录. 以下是最简洁的方式: 4台虚拟机: 用户:root.hadoop hostname 分别是:Master.Hadoop.Slave1.Hadoop.Sl ...
- es6 import 与 export
1.export 命令 export 命令用于规定模块的对外接口. 一个模块就是一个独立的文件.该文件内部所有的变量,外部无法获取.要想外部能够读取模块内部的某个变量,就必须使用 export 关键字 ...
- 微信小程序连接Java后台
有人问我小程序怎么连后台,这里直接贴代码 在app.js里 // api request request(url, params) { return new Promise((resolve, rej ...
- Eclipse 添加 YAML插件
官网:https://github.com/oyse/yedit 离线版本:(链接: https://pan.baidu.com/s/1PJzkS1tI-VigZvfbYXUh9A 密码: gfep) ...
- Codeforces 825D 二分贪心
题意:给一个 s 串和 t 串, s 串中有若干问号,问如何填充问号使得 s 串中字母可以组成最多的 t 串.输出填充后的 s 串. 思路:想了下感觉直接怼有点麻烦,要分情况:先处理已经可以组成 t ...
- 350 Intersection of Two Arrays II 两个数组的交集 II
给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意: 输出结果中每个元素出现的次数, ...
- java中的结构--switch选择结构
if-switch 选择结构 switch结构可以更好的解决等值判断问题switch 选择结构的语法:switch (表达式){ case 常量 1: //代码块1: break; case 常量 2 ...
- 线性回归的Cost function实现
此处使用Octave来实现 线性方程的代价函数: 代价函数: X 是测试值,假设用矩阵表示为 为了方便用矩阵计算我们把X加一列 1 : 同时 那么h( ...