結果

問題 No.2242 Cities and Teleporters
ユーザー nono00nono00
提出日時 2023-03-11 03:08:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,283 ms / 3,000 ms
コード長 1,905 bytes
コンパイル時間 1,394 ms
コンパイル使用メモリ 87,944 KB
実行使用メモリ 31,488 KB
最終ジャッジ日時 2023-10-18 09:38:41
合計ジャッジ時間 22,306 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1,035 ms
31,488 KB
testcase_06 AC 935 ms
31,488 KB
testcase_07 AC 1,053 ms
31,488 KB
testcase_08 AC 1,283 ms
31,488 KB
testcase_09 AC 1,279 ms
31,488 KB
testcase_10 AC 750 ms
31,488 KB
testcase_11 AC 739 ms
31,488 KB
testcase_12 AC 737 ms
31,488 KB
testcase_13 AC 788 ms
31,488 KB
testcase_14 AC 923 ms
31,488 KB
testcase_15 AC 796 ms
31,488 KB
testcase_16 AC 806 ms
31,488 KB
testcase_17 AC 990 ms
31,488 KB
testcase_18 AC 781 ms
31,224 KB
testcase_19 AC 723 ms
30,960 KB
testcase_20 AC 771 ms
30,168 KB
testcase_21 AC 744 ms
30,432 KB
testcase_22 AC 701 ms
30,168 KB
testcase_23 AC 725 ms
31,488 KB
testcase_24 AC 725 ms
31,488 KB
testcase_25 AC 730 ms
31,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

int main() {
    int n;
    std::cin >> n;
    int log = 1;
    while ((1 << log) < n) {
        log++;
    }
    std::vector<int> h(n), t(n);
    std::vector<std::pair<int, int>> event;
    event.reserve(2 * n);
    for (int i = 0; i < n; i++) {
        std::cin >> h[i];
        event.emplace_back(h[i], ~i);
    }
    for (int i = 0; i < n; i++) {
        std::cin >> t[i];
        event.emplace_back(t[i], i);
    }
    std::sort(event.begin(), event.end());
    std::vector<std::vector<int>> parent(n, std::vector<int>(log + 1, -1));
    int max = -1;
    int max_index = -1;
    for (auto [x, i]: event) {
        // h
        if (i < 0) {
            i = ~i;
            if (max < t[i]) {
                max = t[i];
                max_index = i;
            }
        } else {
        // t
            if (x < max) {
                parent[i][0] = max_index;
            }
        }
    }

    for (int j = 0; j < log; j++) {
        for (int i = 0; i < n; i++) {
            int mid_parent = parent[i][j];
            if (mid_parent >= 0) {
                parent[i][j + 1] = parent[mid_parent][j];
            }
        }
    }

    int q;
    std::cin >> q;
    while (q--) {
        int a, b;
        std::cin >> a >> b;
        a--; b--;

        // a -> b
        // h[b] <= t[a]となるまでdoublingで上がっていく
        int count = 0;
        for (int i = log; i >= 0; i--) {
            if (parent[a][i] != -1) {
                if (t[parent[a][i]] < h[b]) {
                    a = parent[a][i];
                    count += (1 << i);
                }
            }
        }
        if (h[b] <= t[a]) {
            std::cout << 1 << '\n';
        } else if (parent[a][0] == -1) {
            std::cout << -1 << '\n';
        } else {
            std::cout << count + 2 << '\n';
        }
    }
}
0