#include<bits/stdc++.h>
using namespace std;
#define ll long long
int N, Q;
vector<ll> S, E;
vector<array<ll, 3>> ranges;
void solve(){
cin>>N>>Q;
S.assign(N, 0);
E.assign(N, 0);
ranges.assign(N, array<ll, 3>());
// 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);
vector<ll> dist(N, LLONG_MAX);
dist[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 = (dist[eq] == LLONG_MAX && S[eq] <= E[curr] && E[eq] >= E[curr]) ? eq : -1;
if(node == -1)
for(int j = 0; j < N; j++){
if(dist[ranges[j][2]] == LLONG_MAX && ranges[j][0] <= E[curr] && ranges[j][1] >= E[curr] && ranges[j][1] <= E[eq]) {
node = ranges[j][2];
break;
}
}
if(node != -1){
dist[node] = dist[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[eq] == LLONG_MAX)
cout<<"impossible"<<endl;
else cout<<dist[eq]<<endl;
}
}
int main(){
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
7140 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
272 KB |
Output is correct |
3 |
Correct |
50 ms |
340 KB |
Output is correct |
4 |
Correct |
9 ms |
332 KB |
Output is correct |
5 |
Correct |
10 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
24 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
272 KB |
Output is correct |
3 |
Correct |
50 ms |
340 KB |
Output is correct |
4 |
Correct |
9 ms |
332 KB |
Output is correct |
5 |
Correct |
10 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
24 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
51 ms |
340 KB |
Output is correct |
13 |
Correct |
8 ms |
340 KB |
Output is correct |
14 |
Correct |
9 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
26 ms |
340 KB |
Output is correct |
19 |
Execution timed out |
1575 ms |
468 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
272 KB |
Output is correct |
3 |
Correct |
50 ms |
340 KB |
Output is correct |
4 |
Correct |
9 ms |
332 KB |
Output is correct |
5 |
Correct |
10 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
24 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
50 ms |
340 KB |
Output is correct |
13 |
Correct |
8 ms |
340 KB |
Output is correct |
14 |
Correct |
10 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
33 ms |
340 KB |
Output is correct |
19 |
Execution timed out |
1580 ms |
6864 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1572 ms |
7116 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
7140 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |