結果

問題 No.2242 Cities and Teleporters
ユーザー trineutrontrineutron
提出日時 2023-03-10 22:57:21
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 666 ms / 3,000 ms
コード長 2,017 bytes
コンパイル時間 4,090 ms
コンパイル使用メモリ 257,184 KB
実行使用メモリ 23,640 KB
最終ジャッジ日時 2023-10-18 08:40:08
合計ジャッジ時間 15,759 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 508 ms
23,640 KB
testcase_06 AC 494 ms
23,640 KB
testcase_07 AC 526 ms
23,640 KB
testcase_08 AC 666 ms
23,640 KB
testcase_09 AC 565 ms
23,640 KB
testcase_10 AC 423 ms
23,640 KB
testcase_11 AC 393 ms
23,640 KB
testcase_12 AC 394 ms
23,640 KB
testcase_13 AC 432 ms
23,640 KB
testcase_14 AC 481 ms
23,640 KB
testcase_15 AC 428 ms
23,640 KB
testcase_16 AC 472 ms
23,640 KB
testcase_17 AC 553 ms
23,640 KB
testcase_18 AC 376 ms
23,376 KB
testcase_19 AC 372 ms
23,376 KB
testcase_20 AC 410 ms
22,848 KB
testcase_21 AC 398 ms
22,848 KB
testcase_22 AC 363 ms
22,848 KB
testcase_23 AC 378 ms
23,640 KB
testcase_24 AC 381 ms
23,640 KB
testcase_25 AC 382 ms
23,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> h(n), t(n);
    for (int i = 0; i < n; i++) {
        cin >> h.at(i);
    }
    for (int i = 0; i < n; i++) {
        cin >> t.at(i);
    }
    int q;
    cin >> q;
    vector<int> a(q), b(q);
    for (int i = 0; i < q; i++) {
        cin >> a.at(i) >> b.at(i);
        a.at(i)--;
        b.at(i)--;
    }

    auto h_copy = h;
    sort(h_copy.begin(), h_copy.end());
    h_copy.erase(unique(h_copy.begin(), h_copy.end()), h_copy.end());
    for (int i = 0; i < n; i++) {
        h.at(i) =
            upper_bound(h_copy.begin(), h_copy.end(), h.at(i)) - h_copy.begin();
        t.at(i) =
            upper_bound(h_copy.begin(), h_copy.end(), t.at(i)) - h_copy.begin();
    }

    const int m = h_copy.size();
    constexpr int l = 20;
    vector<vector<int>> highest(l, vector<int>(m + 1));
    for (int i = 0; i <= m; i++) {
        highest.at(0).at(i) = i;
    }
    for (int i = 0; i < n; i++) {
        highest.at(0).at(h.at(i)) = max(highest.at(0).at(h.at(i)), t.at(i));
    }
    for (int i = 0; i < m; i++) {
        highest.at(0).at(i + 1) =
            max(highest.at(0).at(i + 1), highest.at(0).at(i));
    }
    for (int i = 0; i < l - 1; i++) {
        for (int j = 0; j <= m; j++) {
            highest.at(i + 1).at(j) = highest.at(i).at(highest.at(i).at(j));
        }
    }

    for (int i = 0; i < q; i++) {
        const int ta = t.at(a.at(i)), hb = h.at(b.at(i));
        assert(ta >= 0 and hb > 0);
        if (highest.back().at(ta) < hb) {
            puts("-1");
            continue;
        }
        if (ta >= hb) {
            puts("1");
            continue;
        }
        int ans = 2, x = ta;
        while (highest.at(0).at(x) < hb) {
            int k = 0;
            while (highest.at(k).at(x) < hb) {
                k++;
            }
            k--;
            x = highest.at(k).at(x);
            ans += 1 << k;
        }
        cout << ans << "\n";
    }

    return 0;
}
0