結果

問題 No.2242 Cities and Teleporters
ユーザー milanis48663220milanis48663220
提出日時 2023-03-10 22:02:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 871 ms / 3,000 ms
コード長 3,387 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 129,524 KB
実行使用メモリ 58,812 KB
最終ジャッジ日時 2023-10-18 07:49:20
合計ジャッジ時間 19,275 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 677 ms
41,592 KB
testcase_06 AC 652 ms
41,592 KB
testcase_07 AC 679 ms
41,592 KB
testcase_08 AC 871 ms
41,592 KB
testcase_09 AC 697 ms
41,592 KB
testcase_10 AC 521 ms
58,812 KB
testcase_11 AC 606 ms
58,812 KB
testcase_12 AC 597 ms
58,812 KB
testcase_13 AC 634 ms
58,812 KB
testcase_14 AC 700 ms
58,812 KB
testcase_15 AC 629 ms
58,812 KB
testcase_16 AC 738 ms
58,812 KB
testcase_17 AC 861 ms
58,812 KB
testcase_18 AC 634 ms
57,956 KB
testcase_19 AC 766 ms
57,668 KB
testcase_20 AC 608 ms
56,296 KB
testcase_21 AC 607 ms
56,584 KB
testcase_22 AC 761 ms
56,296 KB
testcase_23 AC 757 ms
58,812 KB
testcase_24 AC 722 ms
58,812 KB
testcase_25 AC 763 ms
58,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;

    }
}
0