#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int MAX = 3e5 + 5;
vector<int> adj[100001];
int color[100001], dis[100001];
void bfs(int s){
queue<int> q;
q.push(s);
dis[s] = 0;
color[s] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
for(auto v : adj[u]){
if(color[v]) continue;
color[v] = 1;
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
struct event{
int s, e, i;
bool operator<(event other){
if(s == other.s){
return e > other.e;
}
return s < other.s;
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q;
cin>>n>>q;
vector<int> S(n + 1), E(n + 1);
vector<event> arr;
for (int i = 1; i <= n; i++)
{
cin>>S[i]>>E[i];
arr.push_back({S[i], E[i], i});
}
sort(arr.begin(), arr.end());
for(int i = 0; i < n - 1; i++){
int first = arr[i].i;
int second = arr[i + 1].i;
if(S[second] <= E[first] && E[first] <= E[second]) adj[first].push_back(second);
}
while(q--){
memset(color, 0, sizeof(color));
memset(dis, -1, sizeof(dis));
int s, e;
cin>>s>>e;
bfs(s);
if(dis[e] != -1) cout<<dis[e]<<endl;
else cout<<"impossible\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1548 ms |
8548 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |