#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);
const int MAXN = 1e5+1;
int s[MAXN],e[MAXN];
bool check(int i,int j){ //got edge i->j?
return s[j] <= e[i] && e[i] <= e[j];
}
int main()
{
owo
int n,q;
cin>>n>>q;
vector<int>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(check(i,j) && e[j] > e[pre[i]])pre[i] = j;
}
}
//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]
//for(int i=0;i<n;i++)cout<<pre[i]<<" ";
//cout<<'\n';
while(q--){
int l,r;
cin>>l>>r;
l--;
r--;
int ans = 0;
while(l != r){
if(check(l,r)){
l=r;
ans++;
break;
}
if(e[l] == e[pre[l]])break;
if(e[pre[l]] < s[r])l = pre[l];
else break;
ans++;
}
if(l!=r){
for(int i=0;i<n;i++){
if(check(l,i) && check(i,r)){
ans+=2;
l = r;
break;
}
}
}
if(l!=r)cout<<"impossible"<<'\n';
else cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1586 ms |
2268 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1586 ms |
2276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1586 ms |
2268 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |