結果

問題 No.2242 Cities and Teleporters
ユーザー t98slidert98slider
提出日時 2023-03-10 23:10:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 451 ms / 3,000 ms
コード長 5,768 bytes
コンパイル時間 2,075 ms
コンパイル使用メモリ 178,936 KB
実行使用メモリ 40,816 KB
最終ジャッジ日時 2023-10-18 08:53:51
合計ジャッジ時間 11,724 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 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 341 ms
23,656 KB
testcase_06 AC 311 ms
23,656 KB
testcase_07 AC 327 ms
23,656 KB
testcase_08 AC 448 ms
23,656 KB
testcase_09 AC 303 ms
23,656 KB
testcase_10 AC 222 ms
40,816 KB
testcase_11 AC 278 ms
40,816 KB
testcase_12 AC 275 ms
40,816 KB
testcase_13 AC 296 ms
40,816 KB
testcase_14 AC 343 ms
40,816 KB
testcase_15 AC 290 ms
40,816 KB
testcase_16 AC 334 ms
40,816 KB
testcase_17 AC 432 ms
40,816 KB
testcase_18 AC 287 ms
40,244 KB
testcase_19 AC 451 ms
40,228 KB
testcase_20 AC 278 ms
39,092 KB
testcase_21 AC 280 ms
39,372 KB
testcase_22 AC 447 ms
39,092 KB
testcase_23 AC 422 ms
40,816 KB
testcase_24 AC 306 ms
40,816 KB
testcase_25 AC 244 ms
40,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define codefor int test;cin>>test;while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
template<class T> istream& operator>>(istream& is, vector<T>& vec) {for(T& x : vec)is >> x;return is;}
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) {if(vec.empty())return os;os << vec[0];for(auto it = vec.begin(); ++it!= vec.end();)os << ' ' << *it;return os;}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){cin >> head;in(tail...);}
void out(){cout << '\n';}
template<class T>void out(const T& a){cout << a << '\n';}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){cout << head << ' ';out(tail...);}
const int INF = 1 << 30;
const long long INF2 = 1ll << 60;
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
    public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }
    const S operator[](int p) const { return get(p); }
    S operator[](int p) { return get(p); }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

    private:
    int _n, size, log;
    std::vector<S> d;
    int ceil_pow2(int n) {
        int x = 0;
        while ((1U << x) < (unsigned int)(n)) x++;
        return x;
    }
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

int op(int lhs, int rhs){
    return max(lhs, rhs);
}

int e(){
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> ca, h(n), t(n);
    cin >> h >> t;
    ca = h;
    ca.insert(ca.end(), all(t));
    sort(all(ca));
    ca.erase(unique(all(ca)), ca.end());
    vector<int> mx(ca.size());
    for(int i = 0; i < n; i++){
        h[i] = lower_bound(ca.begin(), ca.end(), h[i]) - ca.begin();
        t[i] = lower_bound(ca.begin(), ca.end(), t[i]) - ca.begin();
        chmax(mx[h[i]], t[i]);
    }
    //cerr << h << '\n';
    //cerr << t << '\n';
    //segtree<int, op, e> seg(mx);
    vector<vector<int>> dp(20, vector<int>(ca.size()));
    int mxv = 0;
    for(int i = 0; i < ca.size(); i++){
        chmax(mxv, mx[i]);
        dp[0][i] = mxv;
    }
    for(int i = 1; i < 20; i++){
        for(int j = 0; j < ca.size(); j++){
            dp[i][j] = dp[i - 1][dp[i - 1][j]];
        }
    }

    int Q;
    cin >> Q;
    while(Q--){
        int u, v;
        cin >> u >> v;
        u--, v--;
        if(h[v] <= t[u]){
            out(1);
            continue;
        }
        int ans = 0, cur = t[u];
        for(int i = 19; i >= 0; i--){
            if(dp[i][cur] < h[v]){
                cur = dp[i][cur];
                ans += 1 << i;
            }
        }
        if(ans >= n) ans = -1;
        else ans += 2;
        out(ans);
    }
}
0