#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int base=1<<13;
int inf=1e9+50;
vector<pair<int, int>> T(2*base, {inf,0});
pair<int,int> query(int a, int b) {
if(a>b) return {inf,0};
if(a==b) return T[a+base];
a+=base-1;
b+=base+1;
pair<int, int> ans = {inf,0};
while(a/2!=b/2) {
if(a%2==0) ans = min(ans, T[a+1]);
if(b%2==1) ans = min(ans, T[b-1]);
a/=2; b/=2;
}
return ans;
}
void update(int x, pair<int, int> val) {
x += base;
T[x] = val;
x/=2;
while(x>0) {
T[x] = min(T[x+x], T[x+x+1]);
x/=2;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q;
cin >> n >> q;
vector<pair<pair<int, int>, int>> seg(n);
vector<int> left(n), right(n), pozycja(n);
vector<int> Y;
for(int i=0; i<n; ++i) {
cin >> left[i] >> right[i];
seg[i].first = {right[i], left[i]};
seg[i].second = i;
}
sort(seg.begin(), seg.end());
for(int i=0; i<n; ++i) {
pozycja[seg[i].second] = i;
Y.push_back(seg[i].first.first);
}
auto check = [&](int v, int j) {
return (left[j] <= right[v] && right[v] <= right[j]);
};
while(q--) {
int s, b;
cin >> s >> b;
--s;--b;
vector<int> ans(n, inf);
queue<int> Q;
T.assign(2*base, {inf,0});
for(int i=0; i<n; ++i) {
update(i, {seg[i].first.second, seg[i].second});
}
ans[s] = 0;
Q.push(s);
while(Q.size()) {
int v = Q.front(); Q.pop();
int poz = lower_bound(Y.begin(), Y.end(), right[v]) - Y.begin();
while(1) {
auto[val, j] = query(poz, base-1);
if(val > right[v]) break;
update(pozycja[j], {inf,j});
if(check(v,j) && ans[j] == inf) {
ans[j] = ans[v] + 1;
Q.push(j);
}
}
}
if(ans[b]==inf) cout << "impossible\n";
else cout << ans[b] << "\n";
}
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... |