結果

問題 No.2242 Cities and Teleporters
ユーザー hliuser1hliuser1
提出日時 2023-12-24 05:24:09
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 1,149 ms / 3,000 ms
コード長 2,377 bytes
コンパイル時間 2,889 ms
コンパイル使用メモリ 162,636 KB
実行使用メモリ 56,312 KB
最終ジャッジ日時 2023-12-24 05:24:31
合計ジャッジ時間 17,490 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 937 ms
56,312 KB
testcase_06 AC 916 ms
56,312 KB
testcase_07 AC 936 ms
56,312 KB
testcase_08 AC 1,149 ms
56,312 KB
testcase_09 AC 1,050 ms
56,312 KB
testcase_10 AC 230 ms
56,312 KB
testcase_11 AC 369 ms
56,312 KB
testcase_12 AC 407 ms
56,312 KB
testcase_13 AC 499 ms
56,312 KB
testcase_14 AC 608 ms
56,312 KB
testcase_15 AC 488 ms
56,312 KB
testcase_16 AC 498 ms
56,312 KB
testcase_17 AC 611 ms
56,312 KB
testcase_18 AC 391 ms
55,784 KB
testcase_19 AC 371 ms
55,376 KB
testcase_20 AC 456 ms
54,004 KB
testcase_21 AC 428 ms
54,272 KB
testcase_22 AC 328 ms
54,152 KB
testcase_23 AC 373 ms
56,312 KB
testcase_24 AC 421 ms
56,312 KB
testcase_25 AC 472 ms
56,312 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) + 1;
    // 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);
        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