#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
const int MXN = 1e5 + 5;
const int LOG = 20;
int n, Q;
int p[LOG][MXN];
int l[MXN], r[MXN];
int go(int i, int j)
{
return l[j] <= r[i] && r[i] <= r[j];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> Q;
for (int i = 0; i < n; i++) cin >> l[i] >> r[i];
int o[n];
iota(o, o + n, 0);
sort(o, o + n, [&](const int &x, const int &y)
{
return r[x] < r[y];
});
vector<int> v;
for (int &i : o)
{
while (!v.empty() && l[v.back()] > l[i]) v.pop_back();
v.push_back(i);
int lx = 0, rx = (int)v.size() - 1;
while (lx < rx)
{
int mid = (lx + rx) >> 1;
if (r[v[mid]] >= l[i]) rx = mid;
else lx = mid + 1;
}
p[0][i] = v[lx];
}
for (int i = 1; i < LOG; i++) for (int j = 0; j < n; j++) p[i][j] = p[i - 1][p[i - 1][j]];
while (Q--)
{
int s, e;
cin >> s >> e;
s--, e--;
if (s == e)
{
cout << 0 << '\n';
continue;
}
if (go(s, e))
{
cout << 1 << '\n';
continue;
}
int res = 0;
for (int i = LOG - 1; i >= 0; i--)
{
if (l[p[i][e]] <= r[s]) continue;
res += (1 << i);
e = p[i][e];
}
if (go(s, p[0][e])) cout << res + 2 << '\n';
else cout << "impossible\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |