#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;}
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(b[c]>b[d]||p<a[c]||a[c]>a[d]){
cout<<"impossible"<<endl;
continue;
}
cout<<ans+1+(p>b[c])<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
56668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
56660 KB |
Output is correct |
2 |
Incorrect |
36 ms |
56668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
56660 KB |
Output is correct |
2 |
Incorrect |
36 ms |
56668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
56660 KB |
Output is correct |
2 |
Incorrect |
36 ms |
56668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1034 ms |
62988 KB |
Output is correct |
2 |
Correct |
1002 ms |
62988 KB |
Output is correct |
3 |
Correct |
1076 ms |
63100 KB |
Output is correct |
4 |
Incorrect |
797 ms |
63024 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
56668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |