#include "bits/stdc++.h"
using namespace std;
#ifdef duc_debug
#include "bits/debug.h"
#else
#define debug(...)
#endif
const int maxn = 3e5 + 5;
const int LG = 19;
int n, q;
int sl[maxn], sr[maxn];
int ord[maxn];
int up[maxn][LG + 1];
void solve() {
cin >> n >> q;
vector<pair<int, int>> ev;
for (int i = 1; i <= n; ++i) {
cin >> sl[i] >> sr[i];
ev.emplace_back(sr[i], i);
ev.emplace_back(sl[i] - 1, -i);
}
sort(ev.begin(), ev.end(), [](const pair<int, int> &x, const pair<int, int> &y) -> bool {
if (x.first != y.first) return x.first > y.first;
return (x.second < y.second);
});
set<pair<int, int>> s;
for (int i = 0; i < (int)ev.size(); ++i) {
int cur = 0;
while (i + cur < (int)ev.size() and ev[i].first == ev[i + cur].first) ++cur;
for (int j = i; j < i + cur; ++j) {
if (ev[j].second < 0) {
int t = -ev[j].second;
s.erase(s.find(make_pair(sr[t], t)));
} else {
s.insert(make_pair(sr[ev[j].second], ev[j].second));
auto it = s.rbegin();
if (ev[j].second != it->second) {
up[ev[j].second][0] = it->second;
}
}
}
i = i + cur - 1;
}
for (int j = 1; j <= LG; ++j) {
for (int i = 1; i <= n; ++i) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
while (q--) {
int s, e; cin >> s >> e;
if (s == e) {
cout << 0 << '\n';
continue;
}
if (sr[e] < sr[s]) {
cout << "impossible\n";
continue;
}
int ans = 0;
int cur = s;
for (int i = LG; i >= 0; --i) {
if (up[cur][i] and sr[up[cur][i]] < sr[e]) {
ans ^= (1 << i);
cur = up[cur][i];
}
}
if (sl[e] <= sr[cur]) cout << ans + 1 << '\n';
else {
// debug(cur, up[cur][0]);
cur = up[cur][0];
if (!cur || sr[cur] > sr[e]) cout << "impossible\n";
else cout << ans + 2 << '\n';
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# | 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... |