D. Paths in a Complete Binary Tree

54 篇文章 0 订阅
9 篇文章 0 订阅

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 ≤ 1018q ≥ 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 sisi 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

这个题还真是浪费了点时间呐;

题意就是给你一棵完全二叉树,按照先序遍历来标号,然后给你起始点和操作,问你终点的编号是多少。操作期间如果有越界的情况应当忽略过去。

思路:

看这个时间应当是从起始点按照所给的操作不断的加某个数和减去某个数。这样的话就需要我们来仔细观察这棵树的规律了。

我想到的一个方法就是每个结点给定一个深度,然后按照深度来求得这某个数。

因为你会发现对于在同一个横线(深度)上的,对于ULR操作所加的或者所减的数都是一样的,然后不难发现这个数跟2的深度次幂有关系。最下面的深度为0,不能进行LR操作,然后继续往上1层,深度为1,你会发现LR操作都是加减2的0次方,U操作自然就是加或者减2的1次方。

总结一下就是对于深度为d的结点LR操作都是减加2的d-1次方,U操作是加或者减去2的d次方。

然后就会发现一个问题,对于LR操作很简单,只要d大于1直接进行加减就可以。

但是U操作,对于加减操作是看你当前的结点为左孩子还是右孩子的。

想了一段时间,发现关键还是得找规律,而这个规律还必须不受其余的影响,应当按照当前深度d以及结点的编号来求得。于是又开始找规律。

根据深度我们是能计算出最左端的结点的编号的。以此为突破点,我们可以发现差值是固定的,相差的就是2的d+1次方。然后左右子树肯定是交替的,所以我们计算出差了几个然后取一下模就能确定左右子树啦╰( ̄▽ ̄)╭ ~(~ ̄▽ ̄)~确定之后就好说了,进行加减就行了。


当然一开始我们要确定x的深度的,就从中间的端点开始类似插入二叉排序树的结点的方法来把x插入到相应的位置就行了,一边更新出深度来。

#include <bits/stdc++.h>

using namespace std;
const int MAXN=1e5+7;
const int mod=10007;
char s[MAXN];

long long n,m;
int main()
{
    scanf("%I64d%I64d",&n,&m);
    long long x;
    while(m--)
    {
        scanf("%I64d%s",&x,s);
        long long p=(n+1)>>1ll;
        long long d=log2(p);
        //先找到x的位置,更新出深度d
        while(x!=p)
        {
            if(x>p)
            {
                p+=1ll<<(d-1);
                d--;
            }
            else
            {
                p-=1ll<<(d-1);
                d--;
            }
        }
        //开始跑
        bool flag;
        for(int i=0,l=strlen(s);i<l;++i)
        {
            if(s[i]=='U')
            {
                flag=((x-(1ll<<d))/(1ll<<(d+1)))%2;//确定左右孩子结点,0左1右
                if(!flag&&1ll<<(d+1)<=n)//确保不要超出边界
                {
                    x+=1ll<<d;
                    d++;
                }
                else if(flag)//右孩子肯定不需要判断边界啦
                {
                    x-=1ll<<d;
                    d++;
                }
            }
            else
            if(s[i]=='L'&&d)//确保还有深度才能往下跑
            {
                x-=1ll<<(d-1);
                d--;
            }
            else
            if(s[i]=='R'&&d)
            {
                x+=1ll<<(d-1);
                d--;
            }
        }
        printf("%I64d\n",x);
    }
        return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值