#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
int main()
{
owo
int n,q;
cin>>n>>q;
vector<int>s(n),e(n),crd;
for(int i=0;i<n;i++)cin>>s[i]>>e[i];
for(int i=0;i<n;i++){
crd.push_back(s[i]);
crd.push_back(e[i]);
}
sort(all(crd));
crd.resize(unique(all(crd)) - crd.begin());
for(int i=0;i<n;i++){
s[i] = lower_bound(all(crd),s[i]) - crd.begin()+1;
e[i] = lower_bound(all(crd),e[i]) - crd.begin()+1;
}
vector<int>pre(n);
for(int i=0;i<n;i++){
pre[i] = i;
for(int j=0;j<n;j++){
if(s[j] <= e[i] && e[i] < e[j] && e[j] > e[pre[i]])pre[i] = j;
}
//cout<<pre[i]<<" ";
}
//cout<<'\n';
//binary lift to the nearest guy with e[j] < e[target]
//three case
//1)can reach target,ans+1
//2)no guy touches target range ,impossible
//3)some guy touches ans+2
//always optimal to go max guy if e[j] < e[target]
while(q--){
int l,r;
cin>>l>>r;
l--;
r--;
int ans = 0;
while(l != r){
if(s[r] <= e[l] && e[l]<=e[r]){
l=r;
ans++;
break;
}
if(l==pre[l])break;
if(e[pre[l]] < e[r])l = pre[l];
else break;
ans++;
}
if(l!=r){
for(int i=0;i<n;i++){
if(s[i] <= e[l] && e[l] <= e[i] && s[r] <= e[i] && e[i] <= e[r]){
ans+=2;
l = r;
break;
}
}
}
if(l!=r)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 |
1584 ms |
4408 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 |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Incorrect |
6 ms |
348 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 |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Incorrect |
6 ms |
348 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 |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Incorrect |
6 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1541 ms |
4384 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 |
1584 ms |
4408 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |