#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
int find_dist(int v, int ende, vector<vector<int> >& adi, vector<pair<int,int> >& e){
if(v==ende) return 0;
int best=v;
for(auto u: adi[v]){
if(u==ende){
best=u;
break;
}
if(e[best].second<e[u].second){
best=u;
}
}
if(best==v) return inf;
int ans=find_dist(best, ende, adi, e);
if(ans==inf) return inf;
return ans+1;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<pair<int,int> > e(n);
map<int, pair<vector<int>, vector<int> > > time;
for(int i=0; i<n; i++){
int s, t;
cin>> s >> t;
e[i]={s, t};
time[s].first.push_back(i);
time[t].second.push_back(i);
}
vector<vector<int> > adi(n);
set<int> active;
for(auto m: time){
for(auto e: m.second.first){
active.insert(e);
}
for(auto e: m.second.second){
for(auto x: active){
adi[e].push_back(x);
}
}
for(auto e: m.second.second){
active.erase(e);
}
}
for(int i=0; i<q; i++){
int s, t;
cin >> s >> t;
int ans=find_dist(s-1, t-1, adi, e);
if(ans==inf){
cout << "impossible\n";
}
else{
cout << ans << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1595 ms |
26380 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 |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1587 ms |
26100 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 |
1595 ms |
26380 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |