結果
問題 | No.2242 Cities and Teleporters |
ユーザー | nu50218 |
提出日時 | 2023-03-11 00:00:36 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,245 ms / 3,000 ms |
コード長 | 3,256 bytes |
コンパイル時間 | 3,469 ms |
コンパイル使用メモリ | 232,944 KB |
実行使用メモリ | 23,856 KB |
最終ジャッジ日時 | 2024-09-18 05:46:59 |
合計ジャッジ時間 | 25,474 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 1,160 ms
23,804 KB |
testcase_06 | AC | 1,151 ms
23,724 KB |
testcase_07 | AC | 1,147 ms
23,732 KB |
testcase_08 | AC | 1,245 ms
23,728 KB |
testcase_09 | AC | 1,072 ms
23,728 KB |
testcase_10 | AC | 534 ms
23,732 KB |
testcase_11 | AC | 657 ms
23,728 KB |
testcase_12 | AC | 658 ms
23,724 KB |
testcase_13 | AC | 841 ms
23,724 KB |
testcase_14 | AC | 1,063 ms
23,724 KB |
testcase_15 | AC | 828 ms
23,604 KB |
testcase_16 | AC | 840 ms
23,856 KB |
testcase_17 | AC | 1,226 ms
23,728 KB |
testcase_18 | AC | 781 ms
23,420 KB |
testcase_19 | AC | 867 ms
23,268 KB |
testcase_20 | AC | 828 ms
22,784 KB |
testcase_21 | AC | 714 ms
22,928 KB |
testcase_22 | AC | 845 ms
22,784 KB |
testcase_23 | AC | 885 ms
23,732 KB |
testcase_24 | AC | 883 ms
23,852 KB |
testcase_25 | AC | 873 ms
23,724 KB |
ソースコード
#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> uniqueH; for (int i = 0; i < N; i++) { if (i == 0 || H[i] != H[i - 1]) { uniqueH.push_back(H[i]); } } vector<int> next_idx[20]; for (int i = 0; i < 20; i++) { next_idx[i].resize(uniqueH.size()); for (int j = 0; j < (int)next_idx[i].size(); j++) { next_idx[i][j] = j; } } for (int i = 0; i < N; i++) { auto itr = upper_bound(uniqueH.begin(), uniqueH.end(), T[i]); if (itr == uniqueH.begin()) continue; itr--; int idx = itr - uniqueH.begin(); int cur = lower_bound(uniqueH.begin(), uniqueH.end(), H[i]) - uniqueH.begin(); next_idx[0][cur] = max(next_idx[0][cur], idx); } for (int i = 0; i + 1 < (int)uniqueH.size(); 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 < (int)uniqueH.size(); j++) { if (next_idx[i - 1][j] != -1) { next_idx[i][j] = next_idx[i - 1][next_idx[i - 1][j]]; } } } debug(next_idx[0]); 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(uniqueH.begin(), uniqueH.end(), T[x]) - uniqueH.begin(); from--; if (from == -1) { cout << -1 << endl; continue; } int to = lower_bound(uniqueH.begin(), uniqueH.end(), H[y]) - uniqueH.begin(); bool ok = false; int imin = 0; int imax = 1000000; while (imax - imin > 1) { int imid = (imin + imax) / 2; int cur = from; for (int i = 0; i < 20; i++) { if ((imid - 1) & (1 << i)) { cur = next_idx[i][cur]; } } if (cur >= to) { ok = true; imax = imid; } else { imin = imid; } } cout << (ok ? imax : -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(); }