#include<bits/stdc++.h>
using namespace std;
const int k=(1<<30);
map<int,pair<int,int> >maxv;
void upd(int x,int va,int ind) {
pair<int,int>val={va,ind};
while(x>0) {
maxv[x]=max(maxv[x],val);
x>>=1;
}
}
pair<int,int> ask(int from,int to,int cur=1,int beg=0,int en=k-1) {
if(from<=beg && en<=to)return maxv[cur];
if(to<beg || en<from)return {-1,-1};
int mid=(beg+en)/2;
return max(ask(from,to,cur*2,beg,mid),ask(from,to,cur*2+1,mid+1,en));
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n,q;cin>>n>>q;
pair<int,int>a[n];
for(int i=0;i<n;i++) {
int x,y;
cin>>x>>y;
a[i]={x,y};
upd(y+k,k-x,i);
}
pair<int,int> prev[n][25];
for(int i=0;i<n;i++) {
pair<int,int>d=ask(a[i].first,a[i].second);
d.first=k-d.first;
prev[i][0]=d;
}
for(int j=1;j<25;j++) {
for(int i=0;i<n;i++) {
prev[i][j]=prev[prev[i][j-1].second][j-1];
}
}
for(int i=0;i<q;i++) {
int x,y;cin>>x>>y;x--;y--;
if(x==y)cout<<0<<'\n';
else if(a[x].second>=a[y].first && a[x].second<=a[y].second)cout<<1<<'\n';
else if(prev[x][24].first!=prev[y][24].first || a[x].second>a[y].second){cout<<"impossible"<<'\n';}
else {
int cnt=0;
int cur=y;
int en=a[x].second;
for(int j=24;j>=0;j--) {
if(prev[cur][j].first>en) {
cur=prev[cur][j].second;
cnt+=(1<<j);
}
}
cout<<cnt+2<<'\n';
}
}
return 0;
}
///9:30
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1535 ms |
168476 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
11 ms |
2676 KB |
Output is correct |
4 |
Correct |
12 ms |
2760 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Incorrect |
8 ms |
1620 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
11 ms |
2676 KB |
Output is correct |
4 |
Correct |
12 ms |
2760 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Incorrect |
8 ms |
1620 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
11 ms |
2676 KB |
Output is correct |
4 |
Correct |
12 ms |
2760 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Incorrect |
8 ms |
1620 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1529 ms |
165292 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1535 ms |
168476 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |