#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];
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;
}
int m = crd.size();
vector<int>mn(m+1);
for(int i=1;i<=m;i++)mn[i] = i;
for(int i=0;i<n;i++){
mn[e[i]] = min(mn[e[i]],s[i]);
}
//cout<<"range"<<'\n';
//for(int i=0;i<n;i++)cout<<s[i]<<" "<<e[i]<<'\n';
//for(int i=1;i<=m;i++)cout<<mn[i]<<" ";
//cout<<'\n';
//greedy backwards
//forward fails because we are going to exact nodes
while(q--){
int l,r;
cin>>l>>r;
l--;
r--;
if(l==r){
cout<<0<<'\n';
continue;
}
if(e[l] > e[r]){
cout<<"impossible"<<'\n';
continue;
}
int cur = s[r];
int last = e[r];
int ans = 1;
while(cur > e[l]){
int best = cur;
for(int i=cur;i<=last;i++)best = min(best,mn[i]);
if(best == cur){
ans = -1;
break;
}
last = cur;
cur = best;
ans++;
}
if(ans==-1)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 |
1558 ms |
2904 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
2 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
2 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
244 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1227 ms |
1904 KB |
Output is correct |
20 |
Execution timed out |
1594 ms |
1824 KB |
Time limit exceeded |
21 |
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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
2 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
352 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
99 ms |
2660 KB |
Output is correct |
20 |
Correct |
60 ms |
3364 KB |
Output is correct |
21 |
Correct |
68 ms |
3976 KB |
Output is correct |
22 |
Correct |
69 ms |
4248 KB |
Output is correct |
23 |
Correct |
64 ms |
4644 KB |
Output is correct |
24 |
Correct |
87 ms |
4652 KB |
Output is correct |
25 |
Correct |
23 ms |
3428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1544 ms |
2976 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 |
1558 ms |
2904 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |