Submission #1344227

#TimeUsernameProblemLanguageResultExecution timeMemory
1344227nikaa123Event Hopping (BOI22_events)C++20
30 / 100
263 ms28872 KiB
#include<bits/stdc++.h>
using namespace std;

signed main() {
    ios :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define endl "\n"

    int n,q;
    cin >> n >> q;
    vector <int> s(n+1,-1),e(n+1,-1);
    
    for (int i = 1; i <= n; i++) {
        cin >> s[i] >> e[i];
        
    }




    vector <pair<int,int>> t(4*n+5,{-1,-1});
    vector <pair<int,int>> lazy(4*n+5,{-1,-1});

    auto applay = [&](int node, pair<int,int> val) {
        lazy[node] = max(lazy[node],val);
        t[node] = max(t[node],val);
    };

    auto push = [&](int node) {
        if (lazy[node] != make_pair(-1,-1)) {
            applay(node*2,lazy[node]);
            applay(node*2+1,lazy[node]);
            lazy[node] = {-1,-1};
        }
    };

    function<void(int,int,int,int,int,pair<int,int>)> update = [&](int node, int l, int r, int L, int R, pair<int,int> val) {
        if (l > R || r < L) return;
        if (L <= l && r <= R) {
            applay(node,val);
            return;
        }
        push(node);
        int mid = (l+r)/2;
        update(node*2,l,mid,L,R,val);
        update(node*2+1,mid+1,r,L,R,val);
        t[node] = max(t[node*2],t[node*2+1]);
    };

    function <pair<int,int>(int,int,int,int)> getmax = [&](int node, int l, int r, int pos) {
        if (pos > r || pos < l) return make_pair(-1,-1);
        if (l == r) return t[node];
        push(node);
        int mid = (l+r)/2;
        return max(getmax(node*2,l,mid,pos),getmax(node*2+1,mid+1,r,pos));
    };




    vector <pair<int,int>> r;
    for (int i = 1; i <= n; i++) {
        r.emplace_back(e[i],i);
    }
    sort(r.begin(),r.end());
    for (int i = 0; i < n; i++) {
        int l = lower_bound(r.begin(),r.end(),make_pair(s[r[i].second],-1)) - r.begin();
        update(1,0,n,l,n-1,r[i]);
    }
    vector <vector<pair<int,int>>> up(n+1,vector<pair<int,int>>(20,{-1,-1}));
    for (int i = 0; i < n; i++) {
        pair<int, int> res = getmax(1, 0, n, i);
        
        if (res.second != -1 && res.first > r[i].first) {
            up[r[i].second][0] = res;
        } else {
            up[r[i].second][0] = {-1, -1};
        }
    }
    for (int i = 1; i <= 19; i++) {
        for (int j = 1; j <= n; j++) {
            if (up[j][i-1].second == -1) continue;
            up[j][i] = up[up[j][i-1].second][i-1];
        }
    }

    while (q--) {
        int S,E;
        cin >> S >> E;
        if (s[S] > e[E]) {
            cout << "impossible" << endl;
            continue;
        }
        if (S == E) {
            cout << 0 << endl;
            continue;
        }
        int cur = 0;

        if (s[E] <= e[S] && e[S] <= e[E]) {
            cout << cur+1 << endl;
            continue;
        }

        for (int i = 19; i >= 0; i--) {
            if (up[S][i] == make_pair(-1,-1)) continue;
            if (up[S][i].first < s[E]) {
                cur += (1<<i);
                S = up[S][i].second;
            }
        }

        if (s[E] <= e[S] && e[S] <= e[E]) {
            cout << cur+1 << endl;
            continue;
        }

        cur++;
        S = up[S][0].second;

        if (s[E] <= e[S] && e[S] <= e[E]) {
            cout << cur+1 << endl;
            continue;
        }
        cout << "impossible" << endl;
    }


}
#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...