P11375树上游走题解
LiuenzeGESP · · 题解
题目传送门
本题仅需模拟即可,不需要二叉树的数据结构。第
接下来考虑第
假设当前所在节点编号为
如果
综上所述,我们只需要用
还有一点:题目只说最后所处的节点编号不超过
解决方案:一旦编号超过
最后按题意模拟即可。
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long//不开long long见祖宗
int n,s,t;
int inf=1e12;
string str;
signed main(){
cin>>n>>s;
cin>>str;
for(int i=0;i<str.size();i++){
if(str[i]=='U'){
if(s==1)continue;//根节点没有父节点
if(t){
t--;//抵消
continue;
}
s/=2;
}
if(str[i]=='L'){
if(2*s>inf)t++;//不进行移动,记录下来。
else s*=2;
}
else if(str[i]=='R'){
if(2*s+1>inf)t++;//同上。
else s=s*2+1;
}
}
cout<<s<<endl;
return 0;
}