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 ...
随机推荐
- 转载:用python爬虫抓站的一些技巧总结
原文链接:http://www.pythonclub.org/python-network-application/observer-spider 原文的名称虽然用了<用python爬虫抓站的一 ...
- HDOJ_1000
#include int main() { int i, j; while(scanf("%d%d", &i, &j) == 2) printf("%d\ ...
- 读写ini配置文件 .
ini文件的第一行不能是有效内容(不能为section),否则读出键值为空. INI文件是一个无固定标准格式的设置档.它以简单的文字与简单的结构组成,常常使用在视窗操作系统,或是其他操作系统上,许多程 ...
- VUE 之 vuex 和 axios
1.Vuex 部分 1.1 Vuex 是专门为vue.js设计的集中式状态管理架构,其实就是将数据存在一个store中,共各个组件共享使用 1.2 配置: 1.2.1 下载:--npm install ...
- HttpServletRequestWrapper模拟实现分布式Session
HttpSession的内容都放在一个单独的Map中,模拟远程分布式Session. 1.使用HttpServletRequestWrapper创建自定义Request2.使用动态代理包装自定义Req ...
- bzoj5406: Gift
全程膜拜 码得都要一样了.. 对于这种数列置换的可以理解成多个环,而对于一个大小为d的环把顺序弄对要做d-1次 总起来就是n-环数的次数 加上暴力30pt到手啦 假如题目没有限制,那就是第一类斯特林数 ...
- WinDbg 在64位系统下转储32位进程
在64位系统下,首先要判断进程是32位,还是64位 在Win8之前,进程名后带星号(*)则是32位进程.但Win8.1后,则不显示星号.需要选出“平台”列,来确认32位,还是64位. 在64位系统下的 ...
- 常用: JS 获取浏览器窗口大小
// 获取窗口宽度 if (windows.innerWidth) winWidth = windows.innerWidth; else if ((document.body) && ...
- eclipse创建maven项目出现以下报错: org.apache.maven.archiver.MavenArchiver.getManifest (org.apache.maven.project.MavenProject,org.apache.mav en.archiver.MavenArchiveConfiguration)
解决方法: 更新eclipse中的maven插件 Help -> Install New Software -> add -> http://repo1.maven.org/ma ...
- poj 1274 The Perfect Stall 解题报告
题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...