結果

問題 No.2242 Cities and Teleporters
ユーザー 👑 nu50218nu50218
提出日時 2023-03-10 23:07:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,587 bytes
コンパイル時間 3,394 ms
コンパイル使用メモリ 229,924 KB
実行使用メモリ 22,776 KB
最終ジャッジ日時 2023-10-18 08:50:14
合計ジャッジ時間 19,784 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 1 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 685 ms
22,716 KB
testcase_06 AC 676 ms
22,716 KB
testcase_07 AC 704 ms
22,716 KB
testcase_08 AC 827 ms
22,716 KB
testcase_09 AC 613 ms
22,716 KB
testcase_10 AC 405 ms
22,716 KB
testcase_11 AC 608 ms
22,716 KB
testcase_12 AC 604 ms
22,716 KB
testcase_13 AC 614 ms
22,716 KB
testcase_14 AC 651 ms
22,716 KB
testcase_15 AC 606 ms
22,716 KB
testcase_16 AC 632 ms
22,716 KB
testcase_17 AC 733 ms
22,716 KB
testcase_18 AC 629 ms
22,452 KB
testcase_19 AC 730 ms
22,452 KB
testcase_20 AC 593 ms
21,924 KB
testcase_21 AC 588 ms
21,924 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOCAL
#include <local.hpp>
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#define debug(...) (static_cast<void>(0))
#define postprocess() (static_cast<void>(0))
#endif

// #include "atcoder/dsu.hpp"
// #include "atcoder/modint.hpp"
// using mint = atcoder::modint998244353;
// using mint = atcoder::modint1000000007;

using namespace std;
using ll = long long;
using ld = long double;

void solve([[maybe_unused]] int test) {
    int N;
    cin >> N;

    vector<pair<int, int>> _H(N);
    for (int i = 0; i < N; i++) {
        cin >> _H[i].first;
        _H[i].second = i;
    }

    sort(_H.begin(), _H.end());
    vector<int> index(N);
    for (int i = 0; i < N; i++) {
        index[_H[i].second] = i;
    }

    vector<int> H(N);
    for (int i = 0; i < N; i++) {
        H[i] = _H[i].first;
    }

    vector<int> T(N);
    for (int i = 0; i < N; i++) {
        cin >> T[index[i]];
    }

    vector<int> next_idx[20];
    for (int i = 0; i < 20; i++) {
        next_idx[i].resize(N);
        fill(next_idx[i].begin(), next_idx[i].end(), -1);
    }

    for (int i = 0; i < N; i++) {
        auto itr = upper_bound(H.begin(), H.end(), T[i]);

        if (itr == H.begin()) continue;
        itr--;

        int idx = itr - H.begin();

        next_idx[0][i] = idx;
    }
    for (int i = 0; i + 1 < N; i++) {
        next_idx[0][i + 1] = max(next_idx[0][i + 1], next_idx[0][i]);
    }

    for (int i = 1; i < 20; i++) {
        for (int j = 0; j < N; j++) {
            if (next_idx[i - 1][j] != -1) {
                next_idx[i][j] = next_idx[i - 1][next_idx[i - 1][j]];
            }
        }
    }

    int Q;
    cin >> Q;

    for (int q = 0; q < Q; q++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        x = index[x];
        y = index[y];

        int from = upper_bound(H.begin(), H.end(), T[x]) - H.begin();
        from--;

        int to = lower_bound(H.begin(), H.end(), H[y]) - H.begin();

        int ans = 1;
        int cur = from;

        for (int i = 20 - 1; i >= 0; i--) {
            if (next_idx[i][cur] != -1 && next_idx[i][cur] < to) {
                cur = next_idx[i][cur];
                ans += 1 << i;
            }
        }

        if (cur < to && next_idx[0][cur] != -1) {
            ans++;
            cur = next_idx[0][cur];
        }

        cout << (cur >= to ? ans : -1) << endl;
    }
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    for (int _t = 1; _t <= t; _t++) solve(_t);

    postprocess();
}
0