結果
| 問題 |
No.2242 Cities and Teleporters
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-03-10 21:50:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,184 ms / 3,000 ms |
| コード長 | 1,202 bytes |
| コンパイル時間 | 2,097 ms |
| コンパイル使用メモリ | 199,236 KB |
| 最終ジャッジ日時 | 2025-02-11 08:10:03 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 11;
int h[N], t[N];
const int M = N * 2;
int b[M];
void update(int p, int c) {
for(int i = p; i < M; i += i & (-i))
b[i] = max(b[i], c);
}
int query(int p) {
int r = 0;
for(int i = p; i; i -= i & (-i))
r = max(r, b[i]);
return r;
}
int st[20][M];
int main() {
ios::sync_with_stdio(0);
int n; cin >> n;
map <int, int> mp;
for(int i = 1; i <= n; i ++) {
cin >> h[i];
mp[h[i]] = 1;
}
for(int i = 1; i <= n; i ++) {
cin >> t[i];
mp[t[i]] = 1;
}
int cnt = 0;
for(auto &p : mp)
p.second = ++ cnt;
for(int i = 1; i <= n; i ++) {
h[i] = mp[h[i]];
t[i] = mp[t[i]];
update(h[i], t[i]);
}
for(int i = 1; i < M; i ++) {
st[0][i] = max(i, query(i));
}
for(int k = 1; k < 20; k ++)
for(int i = 1; i < M; i ++) {
st[k][i] = st[k - 1][st[k - 1][i]];
}
int q; cin >> q;
while(q --) {
int a, b; cin >> a >> b;
int ans = 0;
int x = t[a];
for(int k = 19; k >= 0; k --)
if(st[k][x] < h[b]) {
ans += (1 << k);
x = st[k][x];
}
if(x < h[b]) {
ans ++;
x = st[0][x];
}
ans ++;
if(x < h[b])
cout << -1 << '\n';
else
cout << ans << '\n';
}
}