D. Paths in a Complete Binary Tree CodeForces - 792D

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.

Input
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.
这里写图片描述
Output
Print q numbers, i-th number must be the answer to the i-th query.

Example
Input
15 2
4
UURL
8
LRLLLLLLLL
Output
10
5
题目给的是一个按照先序遍历的完全二叉树。按照先序遍历对完全二叉树的节点之间对序号含有一定的规律。
1、如果当前节点的序号为x,那么其左孩子节点的序号为(x-(x&-x)/2), 其右孩子节点的序号为(x+(x&-x)/2)。
2、如果当前节点的序号为x, 如果它是右孩子,那么它的父节点为(x-(x&-x)), 如果它是左孩子,那么它的父节点为(x+(x&-x))。
大家可以用二进制进行验证。
然后就算类似于模拟的做法就行了。

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define ll long long int
ll low(ll x)
{
  return x&-x;
}
string s;
int main()
{
  ll n;
  ll q;
  cin >> n >> q;
  ll x;
  ll mid = n/2+1;
  while(q--) {
    cin >> x;
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
      if(s[i] == 'U' && x != mid) {
        ll f = x+low(x);
        if(x == f-low(f)/2)   x = f;
        else if(x != mid) x -= low(x);
      }
      if(s[i] == 'L') x -= low(x)/2;
      if(s[i] == 'R') x += low(x)/2;
    //  cout << x << endl;
    }
    printf("%lld\n", x);
  }

  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值