题意:一颗中序遍历的满二叉树,求从编号为u的结点出发,按指定操作执行的最后结果
思路:
规律,当前点和儿子的差值为 x&(-x) ,其中x为编号
#include<bits/stdc++.h> #define PI acos(-1.0) #define pb push_back using namespace std; typedef long long ll; const int N=2e5+5; const int MOD=1e9+7; const int INF=0x3f3f3f3f; char s[N]; bool checkleft(ll st){ ll t=(st&(-st)); ll fa=st+t; if( fa-(fa&(-fa))/2 == st) return true; return false; } int main(void){ // cout << z<<endl; ll n,q,len,st,t; cin>>n>>q; while(q--){ scanf("%I64d",&st); scanf("%s",s+1); len=strlen(s+1); // cout << s+1<<endl; for(int i=1;i<=len;i++){ // cout<<"st="<<st<<endl; t=(st&(-st)); if(s[i]=='L') st-=t/2; else if(s[i]=='R') st+=t/2; else if(s[i]=='U'){ if(st==(n+1)/2) continue; int flag=checkleft(st); if(flag) st+=t; else st-=t; // printf("st=%I64d\nt=%I64d\n",st,t); } } cout <<st <<endl; } return 0; } /********* *********/