Paths in a Complete Binary Tree codeforces

time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

题目大意:给出一个完全二叉树,用深搜的方法给每一个节点进行标号。然后给出一个起始点,及一条路径,R向右孩子走,L向左孩子走,U向父亲节点走。若无法走,则跳过该节点。

解体思路:记当前节点为x,y=(x&-x)/2,x&-x的值为二进制表示形式下x最后一个1的值。向左孩子走x-=y,向又孩子走x+=y。

比较麻烦处理的是向父亲节点走。在这里共有两种方法处理,同时两种方法的关键点都在于判断当前节点是父亲节点的左孩子还是右孩子。

1.注意若y为当前父亲节点的左孩子,那么最后连两个1中间相差一个0或者只有一个1。若为当前父亲节点的右孩子,那么最后必有两个1相邻。

2.假设当前节点为父亲节点的左孩子,通过此法求出父亲节点,然后由父亲节点反推左孩子,看两者是否相同,若相同则为左孩子,若不相同则该节点为父亲节点的右孩子。


#include <iostream>
using namespace std;
typedef long long ll;

ll left(ll a) {
    ll x = (a & (-a)) / 2;
    return a -= x;
}

ll right(ll a) {
    ll x = (a & (-a)) / 2;
    return a += x;
}

int main() {
    ll n, q;
    cin >> n >> q;
    while(q--) {
        string s;
        ll l;
        cin >> l;
        cin >> s;
        ll a = l;
        for(ll i = 0; i < s.size(); ++i) {
            ll b = (a & (-a));
            if(s[i] == 'L') {
                a = left(a);
            }
            if(s[i] == 'R') {
                a = right(a);
            }
            if(s[i] == 'U') {
                /*int x = a + b;
                int y = a - b;
                if(left(x) == a && x <= n) {
                    a = x;
                }
                if(right(y) == a && y <= n && y != 0) {
                    a = y;
                }*/
                if((n + 1) / 2 == a) continue;
                ll x = (a - b) & (b - a);
                if(((b << 1) < x) || !x) {
                    a += b;
                } else {
                    a -= b;
                }
            }
        }
        cout << a << endl;
    }
    return 0;
}

#include <iostream>
using namespace std;
typedef long long ll;

ll left(ll y) {
    ll x = (y & (-y)) / 2;
    return y -= x;
}

ll right(ll y) {
    ll x = (y & (-y)) / 2;
    return y += x;
}

int main() {
    ll n, q;
    cin >> n >> q;
    while(q--) {
        string s;
        ll l;
        cin >> l;
        cin >> s;
        ll a = l;


        for(unsigned int i = 0; i < s.size(); ++i) {

            ll b = (a & (-a));
            if(s[i] == 'L') {
                a = left(a);
            }
            if(s[i] == 'R') {
                a = right(a);
            }
            if(s[i] == 'U') {
                if((n + 1) / 2 == a) continue;
                ll x = a + b;
                ll y = a - b;
                ll temp;
                if(left(x) == a && x <= n) {
                    temp = x;
                }
                if(right(y) == a && y <= n && y != 0) {
                    temp = y;
                }
                a = temp;
            }
           // cout << a << endl;
        }
        cout << a << endl;
    }
    return 0;
}

/*
7 10
6
LURLLLLRLR
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值