結果

問題 No.2242 Cities and Teleporters
ユーザー hliuser1hliuser1
提出日時 2023-12-24 05:10:14
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,406 bytes
コンパイル時間 2,798 ms
コンパイル使用メモリ 162,624 KB
実行使用メモリ 50,188 KB
最終ジャッジ日時 2023-12-24 05:10:35
合計ジャッジ時間 19,984 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 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 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 419 ms
50,060 KB
testcase_12 AC 425 ms
50,060 KB
testcase_13 AC 597 ms
50,060 KB
testcase_14 AC 898 ms
50,060 KB
testcase_15 AC 607 ms
50,060 KB
testcase_16 AC 572 ms
50,060 KB
testcase_17 AC 947 ms
50,060 KB
testcase_18 AC 518 ms
49,436 KB
testcase_19 AC 404 ms
49,224 KB
testcase_20 AC 546 ms
47,864 KB
testcase_21 AC 467 ms
48,196 KB
testcase_22 AC 381 ms
47,992 KB
testcase_23 AC 468 ms
50,060 KB
testcase_24 AC 519 ms
50,060 KB
testcase_25 AC 964 ms
50,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;

#define all(x) begin(x), end(x)

signed main() {
    cin.tie(0)->sync_with_stdio(0);

    int N, Q;
    cin >> N;

    using City = array<int, 3>;
    vector<City> A(N);
    for (City& c : A) cin >> c[0];
    for (City& c : A) cin >> c[1];
    for (int i = 0; i < N; ++i) A[i][2] = i;
    sort( all(A) );

    vector<int> H(N), T(N), I(N+1), max_T_idx(N);
    for (int i = 0; i < N; ++i) {
        auto [h, t, idx] = A[i];
        H[i] = h;
        I[idx+1] = i;
        T[i] = t;
        if (i == 0) max_T_idx[0] = 0;
        else {
            int j = max_T_idx[i-1];
            if (t > T[j]) max_T_idx[i] = i;
            else max_T_idx[i] = j;
        }
    }

    int K = 31 - __builtin_clz(N);
    // dp[i][j] = max T on the path {i, next[i], next[next[i]], next^(2^j)[i]}
    vector<vector<int>> dp(N, vector<int>(K+1, -1));
    vector<vector<int>> next(N, vector<int>(K+1, -1));

    auto get_next = [&] (int i) -> int {
        int t = T[i];
        auto idx = upper_bound(all(H), t) - begin(H);
        if (idx == 0) return -1;
        return max_T_idx[idx - 1];
    };

    auto dfs = [&] (auto&& dfs, int i, int j) -> pair<int,int> {
        if (i < 0) return {-1, -1};
        if (next[i][j] != -1) return {next[i][j], dp[i][j]};
        if (j == 0) {
            next[i][j] = get_next(i);
            if (next[i][j] == -1) dp[i][j] = T[i];
            else dp[i][j] = max(T[i], T[ next[i][j] ]);
            return {next[i][j], dp[i][j]};
        }
        auto [inner, ival] = dfs(dfs, i, j-1);
        std::tie(next[i][j], dp[i][j]) = dfs(dfs, inner, j-1);
        dp[i][j] = max(dp[i][j], T[i]);
        return {next[i][j], dp[i][j]};
    };

    auto solve = [&] (int a, int b) {
        int ans = 0;
        int nxt, val;
        tie(nxt,val) = dfs(dfs, a, K);
        if (val < H[b])
            return -1;

        for (int i = K; ~i; --i) {
            tie(nxt, val) = dfs(dfs, a, i);
            if (val < H[b]) {
                a = nxt;
                ans += (1 << i);
            }
        }
        if (T[a] < H[b]) return ans + 2;
        return ans + 1;
    };

    vector<int> dbug(N);
    for (int i = 0; i < N; ++i)
        dbug[i] = get_next(i);

    cin >> Q;
    while (Q--) {
        int a, b;
        cin >> a >> b;
        cout << solve(I[a], I[b]) << '\n';
    }
}
0