結果

問題 No.2242 Cities and Teleporters
ユーザー trineutrontrineutron
提出日時 2023-03-10 22:40:13
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,941 bytes
コンパイル時間 3,214 ms
コンパイル使用メモリ 258,144 KB
実行使用メモリ 23,672 KB
最終ジャッジ日時 2023-10-18 08:20:45
合計ジャッジ時間 16,056 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_05 AC 504 ms
23,672 KB
testcase_06 AC 490 ms
23,672 KB
testcase_07 AC 525 ms
23,672 KB
testcase_08 AC 644 ms
23,672 KB
testcase_09 AC 562 ms
23,672 KB
testcase_10 AC 429 ms
23,672 KB
testcase_11 AC 394 ms
23,672 KB
testcase_12 AC 397 ms
23,672 KB
testcase_13 AC 436 ms
23,672 KB
testcase_14 AC 473 ms
23,672 KB
testcase_15 AC 430 ms
23,672 KB
testcase_16 AC 465 ms
23,672 KB
testcase_17 AC 550 ms
23,672 KB
testcase_18 AC 379 ms
23,408 KB
testcase_19 AC 374 ms
23,408 KB
testcase_20 AC 408 ms
22,880 KB
testcase_21 AC 402 ms
22,880 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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, 0));
    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));
        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++;
            }
            if (k == 0) break;
            k--;
            x = highest.at(k).at(x);
            ans += 1 << k;
        }
        cout << ans << "\n";
    }

    return 0;
}
0