Educational Codeforces Round 18 D
Description
T is a complete binary tree consisting of n vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So n is a number such that n + 1 is a power of 2.
In the picture you can see a complete binary tree with n = 15.
Vertices are numbered from 1 to n in a special recursive way: we recursively assign numbers to all vertices from the left subtree (if current vertex is not a leaf), then assign a number to the current vertex, and then recursively assign numbers to all vertices from the right subtree (if it exists). In the picture vertices are numbered exactly using this algorithm. It is clear that for each size of a complete binary tree exists exactly one way to give numbers to all vertices. This way of numbering is called symmetric.
You have to write a program that for given n answers q queries to the tree.
Each query consists of an integer number ui (1 ≤ ui ≤ n) and a string si, where ui is the number of vertex, and si represents the path starting from this vertex. String si doesn't contain any characters other than 'L', 'R' and 'U', which mean traverse to the left child, to the right child and to the parent, respectively. Characters from si have to be processed from left to right, considering that ui is the vertex where the path starts. If it's impossible to process a character (for example, to go to the left child of a leaf), then you have to skip it. The answer is the number of vertex where the path represented by si ends.
For example, if ui = 4 and si = «UURL», then the answer is 10.
The first line contains two integer numbers n and q (1 ≤ n ≤ 1018, q ≥ 1). n is such that n + 1 is a power of 2.
The next 2q lines represent queries; each query consists of two consecutive lines. The first of these two lines contains ui (1 ≤ ui ≤ n), the second contains non-empty string si. si doesn't contain any characters other than 'L', 'R' and 'U'.
It is guaranteed that the sum of lengths of si (for each i such that 1 ≤ i ≤ q) doesn't exceed 105.
Print q numbers, i-th number must be the answer to the i-th query.
15 2
4
UURL
8
LRLLLLLLLL
10
5
题意:根据给的这种满二叉树,U移动到父节点,R移动到右节点。L移动到左节点,当然不能继续移动不要用,问最后的数字是多少?
解法:把数字转成二进制,看看
num&(-num)得到当前节点同层的最左边的节点是什么 Pos=num&(-num),num&(Pos*2)得出当前在左边还是右边,然后就模拟
#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x7fffffff
#define INFL 0x7fffffffffffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
const int M = ;
LL n,m;
string s;
LL num;
LL ans;
int main()
{
cin>>n>>m;
ans=(n+)/;
for(int i=;i<=m;i++)
{
cin>>num>>s;
// cout<<s.size()<<endl;
// cout<<pos<<endl;
for(int j=;j<s.size();j++)
{
LL pos=(num&-num);
if(s[j]=='U'&&num!=ans)
{
if(num&(pos*))
{
num-=pos;
}
else
{
num+=pos;
}
}
else if(s[j]=='L')
{
pos/=;
num-=pos;
//cout<<num<<"C"<<endl;
}
else if(s[j]=='R')
{
pos/=;
num+=pos;
// cout<<num<<"D"<<endl;
}
}
cout<<num<<endl;
}
return ;
}
Educational Codeforces Round 18 D的更多相关文章
- Educational Codeforces Round 18
A. New Bus Route 题目大意:给出n个不同的数,问差值最小的数有几对.(n<=200,000) 思路:排序一下,差值最小的一定是相邻的,直接统计即可. #include<cs ...
- Educational Codeforces Round 18 B
Description n children are standing in a circle and playing the counting-out game. Children are numb ...
- Educational Codeforces Round 18 A
Description There are n cities situated along the main road of Berland. Cities are represented by th ...
- Educational Codeforces Round 18 C. Divide by Three DP
C. Divide by Three A positive integer number n is written on a blackboard. It consists of not more ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
随机推荐
- C++函数模板例子
//C++函数模板实例 #include <iostream>template <class Any>void Swap(Any &a, Any &b); in ...
- TreeSet实现Comparator接口的排序算法的分析
为了方便,用lambda表达式代替comparator接口 例子如下: public static void main(String[] args) { TreeSet<Integer> ...
- hadoop mapred和mapreduce包
mapred包是老的1.0的map reduce api mapreduce包是新的2.0的map reduce api
- A JavaScript library for reading EXIF meta data from image files.
exif-js/exif-js: JavaScript library for reading EXIF image metadata https://github.com/exif-js/exif- ...
- HTML——使用表格进行页面布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Could not load file or assembly 'MyAssembly.XmlSerializers
https://stackoverflow.com/questions/17755559/could-not-load-file-or-assembly-myassembly-xmlserialize ...
- js操作创建和操作外部样式的例子
兼容IE8及以上的IE浏览器1. [代码][HTML]代码 <!DOCTYPE html><html lang="en"> <head> ...
- BZOJ_4311_向量_线段树按时间分治
BZOJ_4311_向量_CDQ分治+线段树按时间分治 Description 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y) ...
- Palindromic Squares
链接 分析:求出b进制以后在判是否为回文 /* ID:wanghan PROB:palsquare LANG:C++ */ #include "iostream" #include ...
- 【BZOJ 3223】 文艺平衡树
[题目链接] 点击打开链接 [算法] 本题是splay区间操作的模板题 我们每个点的权值设为”当前在序列中的排名“,根据二叉排序树的性质,这棵树的中序遍历就是当前序列 如果我们要获得一段区间[l,r] ...