#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]||ans==262143){
cout<<"impossible"<<endl;
continue;
}
cout<<ans+1+(p>b[c])<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
56656 KB |
Output is correct |
2 |
Correct |
1030 ms |
63144 KB |
Output is correct |
3 |
Correct |
1034 ms |
63056 KB |
Output is correct |
4 |
Correct |
1138 ms |
62988 KB |
Output is correct |
5 |
Correct |
1074 ms |
63000 KB |
Output is correct |
6 |
Correct |
1024 ms |
62992 KB |
Output is correct |
7 |
Correct |
1063 ms |
62984 KB |
Output is correct |
8 |
Incorrect |
856 ms |
63240 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
56656 KB |
Output is correct |
2 |
Correct |
38 ms |
56660 KB |
Output is correct |
3 |
Correct |
47 ms |
56656 KB |
Output is correct |
4 |
Correct |
42 ms |
56668 KB |
Output is correct |
5 |
Correct |
46 ms |
56668 KB |
Output is correct |
6 |
Incorrect |
40 ms |
56836 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
56656 KB |
Output is correct |
2 |
Correct |
38 ms |
56660 KB |
Output is correct |
3 |
Correct |
47 ms |
56656 KB |
Output is correct |
4 |
Correct |
42 ms |
56668 KB |
Output is correct |
5 |
Correct |
46 ms |
56668 KB |
Output is correct |
6 |
Incorrect |
40 ms |
56836 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
56656 KB |
Output is correct |
2 |
Correct |
38 ms |
56660 KB |
Output is correct |
3 |
Correct |
47 ms |
56656 KB |
Output is correct |
4 |
Correct |
42 ms |
56668 KB |
Output is correct |
5 |
Correct |
46 ms |
56668 KB |
Output is correct |
6 |
Incorrect |
40 ms |
56836 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1022 ms |
61664 KB |
Output is correct |
2 |
Correct |
1034 ms |
61580 KB |
Output is correct |
3 |
Correct |
1085 ms |
61568 KB |
Output is correct |
4 |
Correct |
819 ms |
62168 KB |
Output is correct |
5 |
Correct |
1226 ms |
63356 KB |
Output is correct |
6 |
Execution timed out |
1557 ms |
62776 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
56656 KB |
Output is correct |
2 |
Correct |
1030 ms |
63144 KB |
Output is correct |
3 |
Correct |
1034 ms |
63056 KB |
Output is correct |
4 |
Correct |
1138 ms |
62988 KB |
Output is correct |
5 |
Correct |
1074 ms |
63000 KB |
Output is correct |
6 |
Correct |
1024 ms |
62992 KB |
Output is correct |
7 |
Correct |
1063 ms |
62984 KB |
Output is correct |
8 |
Incorrect |
856 ms |
63240 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |