#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);
}
}
}
vector<vector<int>> ans(n + 1, vector<int>(n + 1, inf));
function<void(int)> fill = [&](int x){
ans[x][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[x][i] > d){
ans[x][i] = d;
pq.push({d, i});
}
}
}
};
for (int i = 1; i <= n; i++) fill(i);
while (q--){
int s, f; cin>>s>>f;
int out = ans[s][f];
if (out == inf){
cout<<"impossible"<<"\n";
}
else {
cout<<out<<"\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1520 ms |
5120 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
4444 KB |
Output is correct |
4 |
Correct |
10 ms |
4444 KB |
Output is correct |
5 |
Correct |
16 ms |
4440 KB |
Output is correct |
6 |
Correct |
78 ms |
5256 KB |
Output is correct |
7 |
Correct |
269 ms |
5916 KB |
Output is correct |
8 |
Correct |
233 ms |
7132 KB |
Output is correct |
9 |
Correct |
1160 ms |
8472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
4444 KB |
Output is correct |
4 |
Correct |
10 ms |
4444 KB |
Output is correct |
5 |
Correct |
16 ms |
4440 KB |
Output is correct |
6 |
Correct |
78 ms |
5256 KB |
Output is correct |
7 |
Correct |
269 ms |
5916 KB |
Output is correct |
8 |
Correct |
233 ms |
7132 KB |
Output is correct |
9 |
Correct |
1160 ms |
8472 KB |
Output is correct |
10 |
Correct |
0 ms |
708 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
14 ms |
4440 KB |
Output is correct |
13 |
Correct |
10 ms |
4440 KB |
Output is correct |
14 |
Correct |
16 ms |
4444 KB |
Output is correct |
15 |
Correct |
74 ms |
5160 KB |
Output is correct |
16 |
Correct |
286 ms |
6076 KB |
Output is correct |
17 |
Correct |
231 ms |
7136 KB |
Output is correct |
18 |
Correct |
1126 ms |
8476 KB |
Output is correct |
19 |
Execution timed out |
1528 ms |
128600 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
4444 KB |
Output is correct |
4 |
Correct |
10 ms |
4444 KB |
Output is correct |
5 |
Correct |
16 ms |
4440 KB |
Output is correct |
6 |
Correct |
78 ms |
5256 KB |
Output is correct |
7 |
Correct |
269 ms |
5916 KB |
Output is correct |
8 |
Correct |
233 ms |
7132 KB |
Output is correct |
9 |
Correct |
1160 ms |
8472 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
15 ms |
4444 KB |
Output is correct |
13 |
Correct |
10 ms |
4444 KB |
Output is correct |
14 |
Correct |
17 ms |
4444 KB |
Output is correct |
15 |
Correct |
78 ms |
5248 KB |
Output is correct |
16 |
Correct |
270 ms |
6076 KB |
Output is correct |
17 |
Correct |
216 ms |
7004 KB |
Output is correct |
18 |
Correct |
1132 ms |
8476 KB |
Output is correct |
19 |
Execution timed out |
1579 ms |
4956 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1510 ms |
4968 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1520 ms |
5120 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |