#include<bits/stdc++.h>
using namespace std;
#define ll long long
int N, Q;
vector<ll> S, E;
vector<array<ll, 3>> ranges;
vector<vector<ll>> dist;
void solve(){
cin>>N>>Q;
S.assign(N, 0);
E.assign(N, 0);
ranges.assign(N, array<ll, 3>());
dist.assign(N, vector<ll>(N, INT_MAX));
// graph.assign(N, vector<int>());
for(int i = 0; i < N; i++){
int s, e;
cin>>s>>e;
S[i] = s;
E[i] = e;
ranges[i] = {s, e, i};
}
sort(ranges.begin(), ranges.end(), [&](array<ll, 3> a, array<ll, 3> b){
return a[1] > b[1];
});
for(int q = 0; q < Q; q++){
int sq, eq;
cin>>sq>>eq;
sq--;
eq--;
queue<int> que;
que.push(sq);
dist[sq][sq] = 0;
while(!que.empty()){
int curr = que.front();
que.pop();
// vector<int> adj = graph[curr];
// cerco il primo nodo tale che end di quel nodo è < se
// posso trovarlo in O(logn)
int node = -1;
for(int j = 0; j < N; j++){
if(dist[sq][ranges[j][2]] == INT_MAX && ranges[j][0] <= E[curr] && ranges[j][1] >= E[curr] && ranges[j][1] <= E[eq]) {
node = ranges[j][2];
if(node != -1){
dist[sq][node] = dist[sq][curr] +1;
que.push(node);
}
}
}
/*
for(int a : adj){
if(dist[sq][a] != INT_MAX) continue;
dist[sq][a] = dist[sq][curr]+1;
que.push(a);
}
*/
}
if(dist[sq][eq] == INT_MAX)
cout<<"impossible"<<endl;
else cout<<dist[sq][eq]<<endl;
}
}
int main(){
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
189 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
36 ms |
8160 KB |
Output is correct |
4 |
Correct |
33 ms |
8192 KB |
Output is correct |
5 |
Correct |
46 ms |
8200 KB |
Output is correct |
6 |
Correct |
24 ms |
8148 KB |
Output is correct |
7 |
Correct |
48 ms |
8224 KB |
Output is correct |
8 |
Correct |
43 ms |
8148 KB |
Output is correct |
9 |
Correct |
7 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
36 ms |
8160 KB |
Output is correct |
4 |
Correct |
33 ms |
8192 KB |
Output is correct |
5 |
Correct |
46 ms |
8200 KB |
Output is correct |
6 |
Correct |
24 ms |
8148 KB |
Output is correct |
7 |
Correct |
48 ms |
8224 KB |
Output is correct |
8 |
Correct |
43 ms |
8148 KB |
Output is correct |
9 |
Correct |
7 ms |
8148 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
36 ms |
8212 KB |
Output is correct |
13 |
Correct |
29 ms |
8152 KB |
Output is correct |
14 |
Correct |
44 ms |
8148 KB |
Output is correct |
15 |
Correct |
26 ms |
8228 KB |
Output is correct |
16 |
Correct |
50 ms |
8228 KB |
Output is correct |
17 |
Correct |
46 ms |
8148 KB |
Output is correct |
18 |
Correct |
6 ms |
8148 KB |
Output is correct |
19 |
Execution timed out |
1600 ms |
196444 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
36 ms |
8160 KB |
Output is correct |
4 |
Correct |
33 ms |
8192 KB |
Output is correct |
5 |
Correct |
46 ms |
8200 KB |
Output is correct |
6 |
Correct |
24 ms |
8148 KB |
Output is correct |
7 |
Correct |
48 ms |
8224 KB |
Output is correct |
8 |
Correct |
43 ms |
8148 KB |
Output is correct |
9 |
Correct |
7 ms |
8148 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
41 ms |
8228 KB |
Output is correct |
13 |
Correct |
30 ms |
8148 KB |
Output is correct |
14 |
Correct |
44 ms |
8224 KB |
Output is correct |
15 |
Correct |
27 ms |
8148 KB |
Output is correct |
16 |
Correct |
56 ms |
8208 KB |
Output is correct |
17 |
Correct |
44 ms |
8148 KB |
Output is correct |
18 |
Correct |
7 ms |
8220 KB |
Output is correct |
19 |
Runtime error |
205 ms |
524288 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
197 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
189 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |