Educational Codeforces Round 18 -- D. Paths in a Complete Binary Tree(二叉树模拟)

题意:

给你一棵完全二叉树, 要求从某个结点出发, 开始行走,可以向上走,向左走,向右走, 模拟这个过程。

思路:

观察那个二叉树的形状, 直接二分走就可以找到每一个结点。

我们先找到那个结点,然后从这个结点进行二分模拟行走, 因为有向上走的情况存在,所以可以开一个栈,来维护所走过的路。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int maxn = 1e5+10;
struct Node{
    LL v;
    LL L,R;
    Node(LL v = 0,LL L = 0,LL R = 0):v(v),L(L),R(R){}
};

stack<Node>sk;



char s[maxn];

int main(){

    LL n;
    int q;
    scanf("%lld %d",&n, &q);
    while(q--){
        LL m;
        scanf("%lld%s",&m,s);
        while(!sk.empty()) sk.pop();


        LL l = 1, r = n;
        LL cur = l+r>>1LL;
        while(cur != m){
            sk.push(Node(cur,l,r));
            if (m < cur){
                r = cur-1;
            }
            else l = cur + 1;
            cur = l + r >> 1LL;
        }



        for (int i = 0; s[i]; ++i){
            if (s[i] == 'U'){
                if (sk.empty()) continue;
                else {
                    cur = sk.top().v;
                    l = sk.top().L;
                    r = sk.top().R;
                    sk.pop();

                }
            }
            else {
                if (l == r) continue;
                sk.push(Node(cur,l,r));
                if (s[i] == 'L'){
                    r = cur-1;
                }
                else l = cur+1;
                cur = l + r >> 1LL;
            }
        }
        printf("%lld\n",cur);




    }

    return 0;
}



D. Paths in a Complete Binary Tree
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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值