#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) int((x).size())
#define pb push_back
#define s second
#define f first
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
const int INF=1e9;
const int N=5e3+10;
int n, q, s[N], e[N], d[N][N];
vector<pii> inf;
void precalc()
{
sort(inf.begin(), inf.end(), greater<pii>());
set<tiii> st; vi vis;
while(!inf.empty()){
int now_end=(inf.back()).f;
vi ins;
while(!inf.empty() && (inf.back()).f==now_end){
pii it=inf.back(); inf.pop_back();
int in=it.s; ins.pb(in); vis.pb(in);
st.insert({in,now_end,0});
}
for(auto i : ins){
for(auto x : vis){
if(i==x) continue;
auto it=st.lower_bound({x,s[i],0});
if(it!=st.end()){
int in, nd, mn_dist; tie(in, nd, mn_dist)=*it;
if(in==x){
d[x][i]=min(d[x][i], mn_dist+1);
st.insert({in,now_end,d[x][i]});
}
}
}
}
}
}
int main()
{
FAST_IO;
cin >> n >> q;
FOR(i, 0, n)
{
cin >> s[i] >> e[i];
inf.pb({e[i],i});
}
FOR(i, 0, n) FOR(j, 0, n) if(i!=j) d[i][j]=INF;
precalc();
// FOR(i, 0, n)
// {
// FOR(j, 0, n)
// {
// cout << i << " " << j << ": " << d[i][j] << '\n';
// }
// }
while(q--){
int a, b; cin >> a >> b; --a; --b;
if(d[a][b]==INF){
cout << "impossible\n";
}
else{
cout << d[a][b] << '\n';
}
}
}
/*
2 1
1 4
2 3
5 2
1 3
2 4
4 7
7 9
3 7
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
4 ms |
1116 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
314 ms |
52348 KB |
Output is correct |
4 |
Correct |
196 ms |
36796 KB |
Output is correct |
5 |
Correct |
308 ms |
52308 KB |
Output is correct |
6 |
Incorrect |
127 ms |
32020 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
314 ms |
52348 KB |
Output is correct |
4 |
Correct |
196 ms |
36796 KB |
Output is correct |
5 |
Correct |
308 ms |
52308 KB |
Output is correct |
6 |
Incorrect |
127 ms |
32020 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
314 ms |
52348 KB |
Output is correct |
4 |
Correct |
196 ms |
36796 KB |
Output is correct |
5 |
Correct |
308 ms |
52308 KB |
Output is correct |
6 |
Incorrect |
127 ms |
32020 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1368 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
4 ms |
1116 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |