結果
| 問題 |
No.2242 Cities and Teleporters
|
| コンテスト | |
| ユーザー |
srjywrdnprkt
|
| 提出日時 | 2025-03-26 00:06:50 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 560 ms / 3,000 ms |
| コード長 | 2,077 bytes |
| コンパイル時間 | 4,376 ms |
| コンパイル使用メモリ | 290,516 KB |
| 実行使用メモリ | 23,756 KB |
| 最終ジャッジ日時 | 2025-03-26 00:07:07 |
| 合計ジャッジ時間 | 15,767 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
//#include <atcoder/modint>
using namespace std;
//using namespace atcoder;
using ll = long long;
//using mint = modint998244353;
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
/*
町iにいる時、H_j<=T_i以下であるような町jのうち、T_jが最大であるものに行くのが最適でそれ以外は考慮しなくて良い。
よって、各町からk回移動したあとの行き先がダブリングによりO(log k)で求まる。
クエリには、Aからスタートして初めてTがH_B以上となる回数をO(log N)で求める。
*/
int N, mx=-1, argmx=-1;
cin >> N;
vector<int> H(N), T(N);
vector<pair<int, int>> v;
queue<pair<int, int>> que;
vector dp(20, vector<int>(N));
for (int i=0; i<N; i++) cin >> H[i];
for (int i=0; i<N; i++){
cin >> T[i];
v.push_back({H[i], i});
}
sort(v.begin(), v.end());
for (auto &z : v) que.push(z);
v.clear();
for (int i=0; i<N; i++) v.push_back({T[i], i});
sort(v.begin(), v.end());
for (int i=0; i<N; i++){
while(!que.empty() && que.front().first <= v[i].first){
int x, y;
tie(x, y) = que.front();
que.pop();
if (T[y] > mx){
mx = T[y];
argmx = y;
}
}
dp[0][v[i].second] = argmx;
}
for (int i=0; i<N; i++) if (dp[0][i] == -1) dp[0][i] = i;
for (int i=1; i<20; i++){
for (int j=0; j<N; j++){
dp[i][j] = dp[i-1][dp[i-1][j]];
}
}
int Q, s, t;
cin >> Q;
while(Q--){
cin >> s >> t;
s--; t--;
int ans=0;
if (H[t] <= T[s]) cout << 1 << '\n';
else{
for (int i=19; i>=0; i--){
if (T[dp[i][s]] < H[t]){
s = dp[i][s];
ans += (1<<i);
}
}
if (ans > N) cout << -1 << '\n';
else cout << ans+2 << '\n';
}
}
return 0;
}
srjywrdnprkt