結果

問題 No.2716 Falcon Method
ユーザー InTheBloomInTheBloom
提出日時 2024-04-05 22:43:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,883 ms / 2,000 ms
コード長 2,139 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 91,820 KB
実行使用メモリ 119,212 KB
最終ジャッジ日時 2024-04-05 22:43:38
合計ジャッジ時間 20,601 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 5 ms
6,676 KB
testcase_08 AC 6 ms
6,676 KB
testcase_09 AC 5 ms
6,676 KB
testcase_10 AC 4 ms
6,676 KB
testcase_11 AC 229 ms
6,676 KB
testcase_12 AC 1,229 ms
113,408 KB
testcase_13 AC 1,154 ms
115,184 KB
testcase_14 AC 1,326 ms
95,736 KB
testcase_15 AC 1,182 ms
109,708 KB
testcase_16 AC 1,250 ms
96,044 KB
testcase_17 AC 1,433 ms
100,388 KB
testcase_18 AC 1,626 ms
104,736 KB
testcase_19 AC 1,289 ms
94,104 KB
testcase_20 AC 1,136 ms
108,124 KB
testcase_21 AC 187 ms
6,676 KB
testcase_22 AC 253 ms
6,676 KB
testcase_23 AC 1,883 ms
119,212 KB
testcase_24 AC 252 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 1,696 ms
119,212 KB
testcase_28 AC 1,710 ms
119,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

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<vector<ll>> dp_R(N + 1, vector<ll>(MAX)), dp_D(N + 1, vector<ll>(MAX));
    // dp_R[i][j] := カウンタiからスタートして、(カウンタiは踏む)右に2^jマス進むために踏むマス数

    {
        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 - l + 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 - l + 1;
            l++;
        }
    }

    for (int j = 0; j < MAX - 1; j++) {
        for (int i = 0; i < N; i++) {
            dp_R[i][j + 1] = dp_R[(i + dp_R[i][j]) % N][j] + dp_R[i][j];
            dp_D[i][j + 1] = dp_D[(i + dp_D[i][j]) % N][j] + 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";
    }
}
0