-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
45 lines (43 loc) · 829 Bytes
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
long long n;
long long goDown(long long x, char c) {
long long pw = 1;
long long tmp = x;
while(tmp % 2 == 0) {
tmp /= 2;
pw *= 2;
}
if(pw == 1) return x;
else if (c == 'L') return x - pw / 2;
else return x + pw / 2;
}
long long goUp(long long x) {
long long pw = 1;
long long tmp = x;
while(tmp % 2 == 0) {
tmp /= 2;
pw *= 2;
}
if(tmp % 4 == 1) return x + pw;
else return x - pw;
}
int main() {
ios_base :: sync_with_stdio (false);
cin.tie(0);
int q;
cin >> n >> q;
for(int i = 0; i < q; i++) {
string s;
long long u;
cin >> u >> s;
for(auto j : s) {
long long v;
if(j == 'U') v = goUp(u);
else v = goDown(u, j);
if(1 <= v && v <= n) u = v;
}
cout << u << "\n";
}
return 0;
}