#include #include #include using namespace std; using ll = long long; int main () { int N, Q; cin >> N >> Q; string s; cin >> s; int D = 0, R = 0; for (auto c : s) { if (c == 'D') D++; if (c == 'R') R++; } if (D == 0) { for (int i = 0; i < Q; i++) { int H, W, P; cin >> H >> W >> P; cout << (P + W) % N << "\n"; } return 0; } if (R == 0) { for (int i = 0; i < Q; i++) { int H, W, P; cin >> H >> W >> P; cout << (P + H) % N << "\n"; } return 0; } // 両方向でダブリング -> 短い方を採用 const int MAX = 32; vector> dp_R(N + 1, vector(MAX)), dp_D(N + 1, vector(MAX)); // dp_R[i][j] := カウンタiからスタートして、(カウンタiは踏む)右に2^jマス進んだ後にいるマス(modなし) { int l = 0, r = 0; while (l < N) { if (r < l) r = l; while (true) { if (s[r % N] == 'R') break; r++; } dp_R[l][0] = r + 1; l++; } } { int l = 0, r = 0; while (l < N) { if (r < l) r = l; while (true) { if (s[r % N] == 'D') break; r++; } dp_D[l][0] = r + 1; l++; } } for (int j = 0; j < MAX - 1; j++) { for (int i = 0; i < N; i++) { dp_R[i][j + 1] = dp_R[dp_R[i][j] % N][j] - (dp_R[i][j] % N) + dp_R[i][j]; dp_D[i][j + 1] = dp_D[dp_D[i][j] % N][j] - (dp_D[i][j] % N) + dp_D[i][j]; } } for (int i = 0; i < Q; i++) { int H, W, P; cin >> H >> W >> P; ll h = P; ll w = P; for (int j = 0; j < MAX; j++) { if (0 < (H & (1LL << j))) { h = dp_D[h % N][j]; } } for (int j = 0; j < MAX; j++) { if (0 < (W & (1LL << j))) { w = dp_R[w % N][j]; } } cout << min(h, w) % N << "\n"; } }