Submission #723329

#TimeUsernameProblemLanguageResultExecution timeMemory
723329Mr_HusanboyEvent Hopping (BOI22_events)C++17
25 / 100
1579 ms36532 KiB
#include<bits/stdc++.h>
using namespace std;
 
#define all(a) (a).begin(), (a).end()
template<typename T>
int len(T &a){
    return a.size();
}
using ll = long long;
const int inf = 1e9;
const ll infl = 1e18;
#define ff first
#define ss second
 
struct SparseT{
    vector<vector<pair<ll,ll>>> t;
    int n, logn;
    pair<ll,ll> join(pair<ll,ll> a, pair<ll,ll> b){
        return min(a,b);
    }

    pair<ll,ll> neutral(){
        return {infl,infl};
    }

    SparseT (){}

    SparseT (int _n){
        init(_n);
    }

    SparseT (vector<ll> &a){
        build(a);
    }

    void init(int _n){
        n = _n;
        logn = 32 - __builtin_clz(n);
        t.assign(_n, vector<pair<ll,ll>>(logn, neutral()));
    }

    void build(vector<ll> &a){
        init((int)(a.size()));
        for(int i = 0; i < n; i ++){
            t[i][0] = {a[i], i};
        }

        for(int i = 1; i < logn; i ++){
            for(int j = 0; j + (1 << i) <= n; j ++){
                t[j][i] = join(t[j][i - 1], t[j + (1 << (i - 1))][i - 1]);
            }
        }
    }

    ll get(int l, int r){
        if(l > r) return neutral().ss;
        int k = 31 - __builtin_clz(r - l + 1);
        return join(t[l][k], t[r - (1 << k) + 1][k]).ss; 
    }
};
 
 
 
void solve(){
    int n, q; cin >> n >> q;
    vector<tuple<int,int,int>> e(n);
    vector<pair<int,int>> ev(n);
    for(int i = 0; i < n; i ++){
        auto &[r, l, id] = e[i];
        cin >> l >> r;
        ev[i] = {l, r};
        //l = -l;
        id = i; 
    }
    sort(all(e));
    SparseT t;
    vector<ll> lef(n);
    vector<int> pos(n);
    for(int i = 0; i < n; i ++){
        auto [r, l, id] = e[i];
        //l = -l;
        lef[i] = l;
        pos[id] = i;
        //cout << r << ' ' << l << endl;
    }
    t.build(lef);
    //for(auto u : lef) cout << u << ' '; cout << endl;
    vector<int> g(n);
    iota(all(g), 0);
    for(int i = 0; i < n; i ++){
        auto [r, l, id] = e[i];
        //l = -l;
        int ch = lower_bound(all(e), tuple{l, -1, -1}) - e.begin();
        //ch --;
        //cout << i << ' ' << ch << endl;
        int mn = t.get(ch, i);
        auto [rr, ll, idd] = e[mn];
        g[id] = idd;
    }
 //   for(int i = 0; i < n; i ++) cout << i << ' ' << g[i] << endl;
    while(q --){
        int a, b; cin >> a >> b; a --; b --;
        if(a == b){cout << "0\n"; continue;}
        bool ok = 0;
        int d = 0;
        while(g[b] != b){
            d ++;
            if(ev[a].ss > ev[b].ss){break;}
            if(ev[a].ss >= ev[b].ff){
                ok = 1; break;
            }
            b = g[b];
        }
        if(ev[a].ss >= ev[b].ff && ev[a].ss <= ev[b].ss && ok == 0){
            d ++; ok = 1; 
        }
        if(ok) cout << d << '\n'; else cout << "impossible\n";
    }
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int testcases = 1;
    //cin >> testcases;
    while(testcases --){
        solve();
        if(testcases) cout << '\n';
        #ifdef LOCAL
        else cout << '\n';
        cout << "__________________" << endl;
        #endif
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...