【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 ...
随机推荐
- ASP.NET 4 and Visual Studio 2010
https://msdn.microsoft.com/en-us/library/ee532866.aspx The topics in this section provide informatio ...
- JavaScript Patterns 2.7 Avoiding Implied Typecasting
Dealing with == and === false == 0 or "" == 0 return true. always use the === and !== oper ...
- WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer
WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer 分类: WPF2012-04-24 09:59 660人阅读 评论(0) ...
- CF19 E Fairy——树上差分
题目:http://codeforces.com/contest/19/problem/E 先把图连成一棵树,然后对于每条非树边,判断它是在奇环中还是偶环中: 把环上的点打上相应的差分标记,并记录有多 ...
- B. Trees in a Row(cf)
B. Trees in a Row time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codevs3162抄书问题(划分型dp)
3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每 ...
- Java的Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区别???
最近在看Java多线程这本书,但是发现里面有个概念自己搞不清楚.就是Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区 ...
- Codeforces 825D 二分贪心
题意:给一个 s 串和 t 串, s 串中有若干问号,问如何填充问号使得 s 串中字母可以组成最多的 t 串.输出填充后的 s 串. 思路:想了下感觉直接怼有点麻烦,要分情况:先处理已经可以组成 t ...
- 332 Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 通过HTTP协议实时获取微信聊天记录
第一步:登陆 1.get访问微信首页https://wx.qq.com 提供session.headers 用途:获取cookie 后续访问必须带session.headers.cookie这三个参数 ...