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 int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
vector<int> lmao[100005];
int S[100005], E[100005], dist[5005][5005];
int n, q;
void solve(){
cin >> n >> q;
for(int i=1;i<=n;i++)cin >> S[i] >> E[i];
set <pi> st;
for(int i=1;i<=n;i++)st.insert({E[i], i});
for(int i=1;i<=n;i++){
set<pi> :: iterator it = st.lower_bound({S[i], 0});
while(it != st.end()){
pi tmp = *it;
if(tmp.fi > E[i])break;
lmao[tmp.se].push_back(i);
it++;
}
}
for(int i=1;i<=n;i++){
queue<int> qu;
for(int j=1;j<=n;j++)dist[i][j] = 1e9;
dist[i][i] = 0;
qu.push(i);
while(!qu.empty()){
int a = qu.front(); qu.pop();
for(auto j : lmao[a]){
if(dist[i][j] > dist[i][a] + 1)dist[i][j] = dist[i][a] + 1, qu.push(j);
}
}
}
while(q--){
int x, y; cin >> x >> y;
if(dist[x][y] != 1e9)cout << dist[x][y] << '\n';
else cout << "impossible\n";
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
events.cpp:50:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
50 | main(){
| ^~~~
# | 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... |