#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = numeric_limits<int> :: max();
using pii = pair<int, int>;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q; cin>>n>>q;
vector<int> l(n + 1), r(n + 1);
for (int i = 1; i <= n; i++){
cin>>l[i]>>r[i];
}
vector<int> g[n + 1];
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (i == j) continue;
if (l[j] <= r[i] && r[i] <= r[j]){
g[i].push_back(j);
}
}
}
auto get = [&](int x, int y){
vector<int> ans(n + 1, inf);
ans[x] = 0;
priority_queue<pii, vector<pii>, less<pii>> pq;
pq.push({0, x});
while (!pq.empty()){
auto [d, v] = pq.top(); pq.pop();
d++;
for (int i: g[v]){
if (ans[i] > d){
ans[i] = d;
pq.push({d, i});
}
}
}
return ans[y];
};
while (q--){
int s, f; cin>>s>>f;
int out = get(s, f);
if (out == inf){
cout<<"impossible"<<"\n";
}
else {
cout<<out<<"\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1514 ms |
6112 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
528 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
1116 KB |
Output is correct |
7 |
Correct |
22 ms |
2140 KB |
Output is correct |
8 |
Correct |
19 ms |
3164 KB |
Output is correct |
9 |
Correct |
85 ms |
4520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
528 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
1116 KB |
Output is correct |
7 |
Correct |
22 ms |
2140 KB |
Output is correct |
8 |
Correct |
19 ms |
3164 KB |
Output is correct |
9 |
Correct |
85 ms |
4520 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
5 ms |
532 KB |
Output is correct |
15 |
Correct |
9 ms |
1248 KB |
Output is correct |
16 |
Correct |
22 ms |
1880 KB |
Output is correct |
17 |
Correct |
19 ms |
3040 KB |
Output is correct |
18 |
Correct |
78 ms |
4444 KB |
Output is correct |
19 |
Execution timed out |
1563 ms |
30804 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
528 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
1116 KB |
Output is correct |
7 |
Correct |
22 ms |
2140 KB |
Output is correct |
8 |
Correct |
19 ms |
3164 KB |
Output is correct |
9 |
Correct |
85 ms |
4520 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
9 ms |
1116 KB |
Output is correct |
16 |
Correct |
23 ms |
2140 KB |
Output is correct |
17 |
Correct |
22 ms |
3160 KB |
Output is correct |
18 |
Correct |
79 ms |
4440 KB |
Output is correct |
19 |
Execution timed out |
1559 ms |
5760 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1581 ms |
6220 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1514 ms |
6112 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |