#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=200005;
const int INF=1e9;
struct segtree{
int v[N<<2];
segtree(){
memset(v,0x3f,sizeof(v));
}
void add(int id,int l,int r,int a,int b,int x){
if(a<=l&&b>=r){v[id]=min(v[id],x); return;}
int m=(l+r)>>1;
if(a<m)add(id<<1,l,m,a,b,x);
if(b>m)add(id<<1|1,m,r,a,b,x);
v[id]=min(v[id],v[id<<1]);
v[id]=min(v[id],v[id<<1|1]);
}
int ask(int id,int l,int r,int a,int b){
if(a<=l&&b>=r)return v[id];
int ans=INF;
int m=(l+r)>>1;
if(a<m)ans=min(ans,ask(id<<1,l,m,a,b));
if(b>m)ans=min(ans,ask(id<<1|1,m,r,a,b));
return ans;
}
};
int32_t main() {
LCBorz;
int n,q;cin>>n>>q;
vector<int> a(n),b(n),a1(n),b1(n);
vector<int> li;
for(int i=0;i<n;i++){
cin>>a[i]>>b[i];
li.push_back(a[i]);
li.push_back(b[i]);
}
sort(all(li));
li.resize(unique(all(li))-li.begin());
auto get=[&](int k)->int {
return lower_bound(all(li),k)-li.begin();
};
for(int i=0;i<n;i++){
a[i]=get(a[i]);
b[i]=get(b[i]);
a1[i]=a[i],b1[i]=b[i];
}
segtree st[18]{};
for(int t=0;t<18;t++){
for(int i=0;i<n;i++){
st[t].add(1,0,N,b1[i],b1[i]+1,a1[i]);
}
for(int i=0;i<n;i++){
a1[i]=st[t].ask(1,0,N,a1[i],b1[i]+1);
}
}
for(int i=0;i<q;i++){
int c,d;cin>>c>>d;
c--;d--;
if(c==d){cout<<0<<endl;continue;}
if(b[c]>b[d]){cout<<"impossible";continue;}
if(b[c]>=a[d]){cout<<1<<endl;continue;}
int ans=0;
int p=a[d];
for(int t=17;t>=0;t--){
int tmp=st[t].ask(1,0,N,p,b[d]+1);
if(tmp>b[c]){
ans+=1<<t;
p=tmp;
}
}
if(ans==262143){
cout<<"impossible"<<endl;
continue;
}
cout<<ans+2<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
56660 KB |
Output is correct |
2 |
Correct |
926 ms |
59804 KB |
Output is correct |
3 |
Correct |
898 ms |
59836 KB |
Output is correct |
4 |
Correct |
951 ms |
59732 KB |
Output is correct |
5 |
Correct |
906 ms |
60108 KB |
Output is correct |
6 |
Correct |
875 ms |
59852 KB |
Output is correct |
7 |
Correct |
867 ms |
60408 KB |
Output is correct |
8 |
Correct |
678 ms |
60360 KB |
Output is correct |
9 |
Correct |
683 ms |
63472 KB |
Output is correct |
10 |
Incorrect |
806 ms |
63256 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56656 KB |
Output is correct |
2 |
Correct |
36 ms |
56564 KB |
Output is correct |
3 |
Correct |
43 ms |
56752 KB |
Output is correct |
4 |
Incorrect |
41 ms |
56660 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56656 KB |
Output is correct |
2 |
Correct |
36 ms |
56564 KB |
Output is correct |
3 |
Correct |
43 ms |
56752 KB |
Output is correct |
4 |
Incorrect |
41 ms |
56660 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56656 KB |
Output is correct |
2 |
Correct |
36 ms |
56564 KB |
Output is correct |
3 |
Correct |
43 ms |
56752 KB |
Output is correct |
4 |
Incorrect |
41 ms |
56660 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
932 ms |
59804 KB |
Output is correct |
2 |
Correct |
964 ms |
59856 KB |
Output is correct |
3 |
Correct |
940 ms |
59752 KB |
Output is correct |
4 |
Correct |
676 ms |
60312 KB |
Output is correct |
5 |
Incorrect |
805 ms |
60032 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
56660 KB |
Output is correct |
2 |
Correct |
926 ms |
59804 KB |
Output is correct |
3 |
Correct |
898 ms |
59836 KB |
Output is correct |
4 |
Correct |
951 ms |
59732 KB |
Output is correct |
5 |
Correct |
906 ms |
60108 KB |
Output is correct |
6 |
Correct |
875 ms |
59852 KB |
Output is correct |
7 |
Correct |
867 ms |
60408 KB |
Output is correct |
8 |
Correct |
678 ms |
60360 KB |
Output is correct |
9 |
Correct |
683 ms |
63472 KB |
Output is correct |
10 |
Incorrect |
806 ms |
63256 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |