This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef int ll;
vector<ll> g[200100];
ll d[200100];
bool ok(pair<ll, pair<ll, ll>> a, pair<ll, pair<ll, ll>> b) {
return (b.s.f <= a.f);
}
void solve() {
ll n; cin>>n;
ll Q; cin>>Q;
vector<pair<ll, pair<ll, ll>>> v;
for(ll i=1; i<=n; i++) {
ll l,r; cin>>l>>r;
v.push_back({r, {l, i}});
}
sort(v.begin(), v.end());
for(ll i=0; i<(ll)v.size(); i++) {
for(ll j=i+1; j<(ll)v.size(); j++) {
if(ok(v[i], v[j])) {
g[v[i].s.s].push_back(v[j].s.s);
}
}
}
while(Q--) {
ll x,y; cin>>x>>y;
queue<ll> q;
for(ll i=1; i<=n; i++) d[i]=1e9;
d[x]=0;
q.push(x);
while(!q.empty()) {
ll v=q.front();
q.pop();
for(auto to: g[v]) {
if(d[to] > d[v]+1) {
d[to]=d[v]+1;
q.push(to);
}
}
}
if(d[y] == 1e9) cout<<"impossible\n";
else cout<<d[y]<<'\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
// cin>>t;
while(t--) {
solve();
cout<<'\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... |