結果
問題 | No.2242 Cities and Teleporters |
ユーザー |
|
提出日時 | 2023-03-10 23:06:43 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,626 bytes |
コンパイル時間 | 12,893 ms |
コンパイル使用メモリ | 289,908 KB |
最終ジャッジ日時 | 2025-02-11 09:09:12 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 7 WA * 19 |
ソースコード
#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 (cur == to) continue;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();}