【题目链接】: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的更多相关文章

  1. 【codeforces 870F】Paths

    Description You are given a positive integer n. Let's build a graph on vertices 1, 2, ..., n in such ...

  2. codeforces 792D - Paths in a Complete Binary Tree

    #include<cstdio> #include<iostream> #define lowbit(x) x&(-x) typedef long long ll; u ...

  3. 【leetcode❤python】 111. Minimum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  4. 【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 ...

  5. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  6. 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 ...

  7. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  8. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  9. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

随机推荐

  1. hdu 4123(树形dp+倍增)

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. 【POJ 2054】 Color a Tree

    [题目链接] http://poj.org/problem?id=2054 [算法] 贪心 [代码] #include <algorithm> #include <bitset> ...

  3. [App Store Connect帮助]三、管理 App 和版本(2.6)输入 App 信息:新增 watchOS App 信息

    如果您的 iOS App 中包含 watchOS App,请确保您的描述中包含该 App 在 Apple Watch 上的功能.您还需要为 Apple Watch 的 App Store 提供额外的屏 ...

  4. 在chrome里模拟调试微信浏览器

    开发者模式(下面有配图): 开发者模式/DevTools.More tools/Network conditions/User agent/ Custom/安卓或ios代理配置配置 更改User ag ...

  5. NetCore Netty 框架 BT.Netty.RPC 系列随讲 二 WHO AM I 之 NETTY/NETTY 与 网络通讯 IO 模型之关系?

    一:NETTY 是什么? Netty 是什么?  这个问题其实百度上一搜一堆. 这是官方话的描述:Netty 是一个基于NIO的客户.服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个 ...

  6. UNIX环境高级编程--7

    进程环境main函数:    C程序总是从main函数开始执行.main函数原型是:    int main(int argc, char *argv[]);    当内核执行C程序时(使用一个exe ...

  7. Elasticsearch之CURL命令的DSL查询

    它是Domain Specific Language领域特定语言. https://www.elastic.co/guide/en/elasticsearch/reference/current/in ...

  8. NHibernate学习(零)-本次学习遇到的错误汇总

    问题一: "System.TypeInitializationException"类型的未经处理的异常在 KimismeDemo.exe 中发生 其他信息: "NHibe ...

  9. Unity学习-地形的设置(五)

    添加地形游戏对象 [Hierarchy-Create-Terrain] 为了看的看清楚,在添加一个平行光 [Hierarchy-Create-Direction light] 导入地形包 [Asset ...

  10. <assert.h>

    Diagnostics 定义宏: void assert (scalar-expression); 若expression为0,则打印出错信息(类似Assertion failed: expressi ...