結果
| 問題 |
No.2242 Cities and Teleporters
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-03-11 03:25:32 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 733 ms / 3,000 ms |
| コード長 | 3,066 bytes |
| コンパイル時間 | 3,340 ms |
| コンパイル使用メモリ | 272,064 KB |
| 実行使用メモリ | 22,848 KB |
| 最終ジャッジ日時 | 2024-09-18 06:09:55 |
| 合計ジャッジ時間 | 15,085 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
int main() {
constexpr int B = 18;
int n; cin >> n;
vector<int> h(n), t(n);
REP(i, n) cin >> h[i];
REP(i, n) cin >> t[i];
vector<int> v(n * 2);
ranges::copy(h, v.begin());
ranges::copy(t, next(v.begin(), n));
ranges::sort(v);
v.erase(unique(ALL(v)), v.end());
REP(i, n) h[i] = distance(v.begin(), ranges::lower_bound(v, h[i]));
REP(i, n) t[i] = distance(v.begin(), ranges::lower_bound(v, t[i]));
// REP(i, n) cout << h[i] << " \n"[i + 1 == n];
// REP(i, n) cout << t[i] << " \n"[i + 1 == n];
int q; cin >> q;
vector<int> a(q), b(q); REP(i, q) cin >> a[i] >> b[i], --a[i], --b[i];
{
vector<int> ord(n);
iota(ALL(ord), 0);
ranges::sort(ord, {}, [&](const int i) -> int { return h[i]; });
vector<int> tmp(n);
REP(i, n) tmp[i] = h[ord[i]];
h.swap(tmp);
REP(i, n) tmp[i] = t[ord[i]];
t.swap(tmp);
REP(i, n) tmp[ord[i]] = i;
REP(i, q) a[i] = tmp[a[i]];
REP(i, q) b[i] = tmp[b[i]];
}
vector dp(B, vector(n, -1));
iota(ALL(dp[0]), 0);
REP(i, n) {
if (i > 0) chmax(dp[0][i], dp[0][i - 1]);
if (const auto it = ranges::upper_bound(h, t[i]); it != h.begin()) {
chmax(dp[0][i], distance(h.begin(), prev(it)));
}
}
REP(bit, B - 1) {
iota(ALL(dp[bit + 1]), 0);
REP(i, n) {
if (i > 0) chmax(dp[bit + 1][i], dp[bit + 1][i - 1]);
chmax(dp[bit + 1][i], dp[bit][dp[bit][i]]);
}
}
// REP(i, n) cout << dp[0][i] << " \n"[i + 1 == n];
// REP(i, n) cout << dp[1][i] << " \n"[i + 1 == n];
// REP(i, q) cout << a[i] << ' ' << b[i] << '\n';
REP(i, q) {
if (h[b[i]] <= t[a[i]]) {
cout << 1 << '\n';
continue;
}
if (t[a[i]] < h.front()) {
cout << "-1\n";
continue;
}
if (const auto it = ranges::upper_bound(h, t[a[i]]); it != h.begin()) {
a[i] = distance(h.begin(), prev(it));
}
int lb = 0, ub = n;
while (ub - lb > 1) {
const int mid = midpoint(lb, ub);
int town = a[i];
for (int j = mid, bit = 0; j > 0 && town != -1; j /= 2, ++bit) {
if (j % 2 == 1) town = dp[bit][town];
}
(b[i] <= town ? ub : lb) = mid;
}
cout << (ub == n ? -1 : ub + 1) << '\n';
}
return 0;
}
emthrm