結果
| 問題 |
No.2242 Cities and Teleporters
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2023-03-10 22:02:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 903 ms / 3,000 ms |
| コード長 | 3,387 bytes |
| コンパイル時間 | 1,381 ms |
| コンパイル使用メモリ | 126,704 KB |
| 最終ジャッジ日時 | 2025-02-11 08:20:23 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
return vector<vector<T>>(n, vector<T>(m, v));
}
template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}
template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
if(v.empty()) {
cout << endl;
return;
}
for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
cout << v.back() << endl;
}
template<typename T>
class Compress{
public:
vector<T> data;
int offset;
Compress(vector<T> data_, int offset=0): offset(offset){
data = data_;
sort(begin(data), end(data));
data.erase(unique(begin(data), end(data)), end(data));
};
int operator[](T x) {
auto p = lower_bound(data.begin(), data.end(), x);
assert(x == *p);
return offset+(p-data.begin());
}
T inv(int x){
return data[x-offset];
}
int size(){
return data.size();
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n; cin >> n;
vector<int> h(n), t(n);
for(int i = 0; i < n; i++) cin >> h[i];
for(int i = 0; i < n; i++) cin >> t[i];
vector<int> v = h;
for(int x: t) v.push_back(x);
auto cp = Compress<int>(v);
int m = cp.size();
vector<int> mx(m, -1);
for(int i = 0; i < n; i++){
int j = cp[h[i]];
chmax(mx[j], cp[t[i]]);
}
for(int i = 1; i < m; i++) chmax(mx[i], mx[i-1]);
auto nx = vec2d(20, n+m, -1);
for(int i = 0; i < m; i++){
nx[0][i] = mx[i];
}
for(int j = 1; j < 20; j++){
for(int i = 0; i < m; i++){
if(nx[j-1][i] == -1){
nx[j][i] = -1;
}else{
nx[j][i] = max(nx[j-1][nx[j-1][i]], nx[j-1][i]);
}
}
}
int q; cin >> q;
while(q--){
int a, b; cin >> a >> b; a--; b--;
if(t[a] >= h[b]){
cout << 1 << endl;
continue;
}
int s = cp[t[a]];
int t = cp[h[b]];
int ans = 1;
int cur = s;
if(cur == -1){
cout << -1 << endl;
continue;
}
int max_go = cur;
for(int j = 19; j >= 0; j--){
max_go = max_go == -1 ? max_go : nx[j][max_go];
}
if(max_go < t){
cout << -1 << endl;
continue;
}
for(int j = 19; j >= 0; j--){
if(nx[j][cur] >= t){
}else{
ans += 1<<j;
cur = nx[j][cur];
}
}
cout << ans+1 << endl;
}
}
milanis48663220