#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;
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<pair<int, int>> eve(n);
for (int i = 0; i < n; i++) {
eve[i].fi = nval[evinit[i].fi];
eve[i].se = nval[evinit[i].se];
}
sort(eve.begin(), eve.end());
vector<int> ending(cur);
for (int i = 0; i < n - 1; i++) {
if (eve[i].se < eve[i + 1].fi) {
ending[eve[i].se] = 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--;
if (s > e) {
cout << "impossible" << '\n';
} else if (pr[eve[e].se] - pr[eve[s].fi] == 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... |