#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int inf = 1e15;
bool cmp(vector<int> a, vector<int> b) {
return a[1] < b[1];
}
signed main() {
int n, q;
cin >> n >> q;
vector<pair<int, int>> evinit(n);
set<int> comst;
map<int, int> nval, inival;
for (int i = 0; i < n; i++) {
cin >> evinit[i].fi >> evinit[i].se;
comst.insert(evinit[i].fi);
comst.insert(evinit[i].se);
}
int cur = 0;
for (auto el : comst) {
nval[el] = cur;
inival[cur] = el;
cur++;
}
vector<vector<int>> eve(n, vector<int>(3));
for (int i = 0; i < n; i++) {
eve[i][0] = nval[evinit[i].fi];
eve[i][1] = nval[evinit[i].se];
eve[i][2] = i;
}
vector<int> nind(n);
sort(eve.begin(), eve.end(), cmp);
for (int i = 0; i < n; i++) {
nind[eve[i][2]] = i;
}
vector<int> ending(cur);
for (int i = 0; i < n - 1; i++) {
if (eve[i][1] < eve[i + 1][0]) {
ending[eve[i][1]] = 1;
}
}
vector<int> pr(cur + 1);
for (int i = 0; i < cur; i++) {
pr[i + 1] = ending[i] + pr[i];
}
for (int qw = 0; qw < q; qw++) {
int s, e;
cin >> s >> e;
s--;
e--;
s = nind[s];
e = nind[e];
if (s > e) {
cout << "impossible" << '\n';
} else if (pr[eve[e][1]] - pr[eve[s][0]] == 0) {
cout << e - s << '\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... |