【codeforces 792D】Paths in a Complete Binary Tree
【题目链接】:http://codeforces.com/contest/792/problem/D
【题意】
给你一棵满二叉树;
给你初始节点;
给你若干个往上走,左走,右走操作;
让你输出一系列操作结束之后节点的位置;
【题解】
这个节点的标志方式类似树状数组;
用树状数组左走右走就好;
L->x-=lowbit(x)/2;
R->x+=lowbit(x)/2;
U->先假设x是左儿子,然后依照规则求出父亲F,然后看看F的左儿子是不是真的是x,是的话爸爸就是F,否则x是右儿子,然后根据x是右儿子求出对应的父亲节点就好;
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
LL n,q,x;
int len;
char s[N];
LL lowbit(LL x){return x&(-x);}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rel(n),rel(q);
while (q--)
{
rel(x);
scanf("%s",s+1);
len = strlen(s+1);
rep1(i,1,len)
{
LL t = lowbit(x);
if (s[i]=='L')//向左走
{
x-=t/2;
}
else
if (s[i]=='R')//向右走
{
x+=t/2;
}
else
{
if (x==(n+1)/2) continue;//是根节点就跳过
//假设x是爸爸的左儿子
LL baba = x+t;
LL zbaba = baba-lowbit(baba)/2;//然后求出这个假装爸爸的真左儿子
if (zbaba==x)
{
x = baba;
}
else//是右儿子;
{
x = x-t;
}
}
}
printf("%lld\n",x);
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}