• 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.

    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 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
    题意:根据给的这种满二叉树,U移动到父节点,R移动到右节点。L移动到左节点,当然不能继续移动不要用,问最后的数字是多少?
    解法:把数字转成二进制,看看
    num&(-num)得到当前节点同层的最左边的节点是什么

    Pos=num&(-num),num&(Pos*2)得出当前在左边还是右边,然后就模拟
     1 #include<stdio.h>
     2 //#include<bits/stdc++.h>
     3 #include<string.h>
     4 #include<iostream>
     5 #include<math.h>
     6 #include<sstream>
     7 #include<set>
     8 #include<queue>
     9 #include<map>
    10 #include<vector>
    11 #include<algorithm>
    12 #include<limits.h>
    13 #define inf 0x7fffffff
    14 #define INFL 0x7fffffffffffffff
    15 #define lson l,m,rt<<1
    16 #define rson m+1,r,rt<<1|1
    17 #define LL long long
    18 #define ULL unsigned long long
    19 using namespace std;
    20 const int M = 2000000;
    21 LL n,m;
    22 string s;
    23 LL num;
    24 LL ans;
    25 int main()
    26 {
    27     cin>>n>>m;
    28     ans=(n+1)/2;
    29     for(int i=1;i<=m;i++)
    30     {
    31         cin>>num>>s;
    32      //   cout<<s.size()<<endl;
    33       //  cout<<pos<<endl;
    34         for(int j=0;j<s.size();j++)
    35         {
    36             LL pos=(num&-num);
    37             if(s[j]=='U'&&num!=ans)
    38             {
    39                 if(num&(pos*2))
    40                 {
    41                     num-=pos;
    42                 }
    43                 else
    44                 {
    45                     num+=pos;
    46                 }
    47             }
    48             else if(s[j]=='L')
    49             {
    50                 pos/=2;
    51                 num-=pos;
    52                 //cout<<num<<"C"<<endl;
    53             }
    54             else if(s[j]=='R')
    55             {
    56                 pos/=2;
    57                 num+=pos;
    58               //  cout<<num<<"D"<<endl;
    59             }
    60         }
    61         cout<<num<<endl;
    62     }
    63     return 0;
    64 }


  • 相关阅读:
    SQL 数据库中将某表中的一列数据拆分作为查询条件
    SQL数据库导入数据时提示未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序。 (System.Data)
    SQL常用内置函数
    SQL常用语句
    关于网页中鼠标双击文字选中设置
    SQL数据库查询列的类型及长度
    ASP. NET MVC项目 使用iTextSharp将网页代码生成PDF文件
    eslint-config-airbnb vs prettier vs standard
    windows批处理(bat脚本)
    python日志库loguru
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6641899.html
Copyright © 2020-2023  润新知