您的位置:首页 > 其它

Educational Codeforces Round 18 D. Paths in a Complete Binary Tree

2017-04-09 22:58 399 查看
原题链接

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 ≤ 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


对于i节点,用l, r表示以该节点为根的子树所有节点的数值的范围,同时l,r也可以表示i节点,对于左孩子,用l, (l+r)/2-1表示,对于右孩子,用(l+r)/2+1, r表示

#include <bits/stdc++.h>
#define maxn 200005
typedef long long ll;
using namespace std;

struct Node{
ll l, r;
}node[maxn];
char str[maxn];
int main() {
// freopen("in.txt", "r", stdin);
ll n, q, m;
scanf("%I64d%I64d", &n, &q);
while(q--) {
scanf("%I64d%s", &m, str);
int cnt = 0;
node[cnt].l = 1;
node[cnt].r = n;
ll f;
while((f = (node[cnt].l + node[cnt].r) / 2) != m) {
cnt++;
if(m < f){
node[cnt].l = node[cnt-1].l;
node[cnt].r = f - 1;
}
else {
node[cnt].l = f + 1;
node[cnt].r = node[cnt-1].r;
}
}
for(int i = 0; str[i]; i++) {
if(str[i] == 'L') {
if(node[cnt].l == node[cnt].r)
continue;
else {
cnt++;
node[cnt].l = node[cnt-1].l;
node[cnt].r = (node[cnt-1].l + node[cnt-1].r) / 2 - 1;
}
}
if(str[i] == 'R') {
if(node[cnt].l == node[cnt].r)
continue;
else {
cnt++;
node[cnt].l = (node[cnt-1].l + node[cnt-1].r) / 2 + 1;
node[cnt].r = node[cnt-1].r;
}
}
if(str[i] == 'U') {
if(cnt == 0)
continue;
else {
cnt--;
}
}
}
printf("%I64d\n", (node[cnt].l + node[cnt].r) / 2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: