Submission #653536

#TimeUsernameProblemLanguageResultExecution timeMemory
653536prvocisloEvent Hopping (BOI22_events)C++17
100 / 100
168 ms24392 KiB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;

const int maxn = 2e5 + 5, logn = 18, inf = 1e9 + 5;
//const int maxn = 16, logn = 18, inf = 1e9 + 5;
vector<pair<int, int> > st(maxn * 2, { inf, inf });
int mini(int l, int r)
{
    pair<int, int> p = { inf, inf };
    for (l += maxn, r += maxn + 1; l < r; l >>= 1, r >>= 1)
    {
        if (l & 1) p = min(st[l++], p);
        if (r & 1) p = min(st[--r], p);
    }
    return p.second;
}
vector<vector<int> > l(logn, vector<int>(maxn, -1));
struct usek
{
    int l, r;
};
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    vector<usek> u(n);
    vector<int> s;
    for (int i = 0; i < n; i++)
    {
        cin >> u[i].l >> u[i].r;
        s.push_back(u[i].l);
        s.push_back(u[i].r);
    }
    sort(s.begin(), s.end());
    for (int i = 0; i < n; i++)
    {
        u[i].l = lower_bound(s.begin(), s.end(), u[i].l) - s.begin();
        u[i].r = lower_bound(s.begin(), s.end(), u[i].r) - s.begin();
        st[maxn + u[i].r] = min(st[maxn + u[i].r], { u[i].l, i });
    }
    for (int i = maxn - 1; i > 0; i--) st[i] = min(st[i << 1], st[(i << 1) | 1]);
    for (int i = 0; i < n; i++)
    {
        int mi = mini(u[i].l, u[i].r);
        l[0][i] = mi;
    }
    for (int j = 1; j < logn; j++)
    {
        for (int i = 0; i < n; i++) l[j][i] = l[j - 1][l[j - 1][i]];
    }
    while (q--)
    {
        int s, e;
        cin >> s >> e;
        s--, e--;
        //cout << "                   ";
        if (s == e)
        {
            cout << "0\n";
            continue;
        }
        if (u[s].r > u[e].r)
        {
            cout << "impossible\n";
            continue;
        }
        if (u[e].l <= u[s].r)
        {
            cout << "1\n";
            continue;
        }
        int ans = 1;
        for (int j = logn - 1; j >= 0; j--)
        {
            if (u[l[j][e]].l > u[s].r) ans += (1 << j), e = l[j][e];
        }
        if (u[l[0][e]].l <= u[s].r) cout << ans + 1 << "\n";
        else cout << "impossible\n";
    }
    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...