#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const long long k=(1<<30);
map<long long,pair<long long,long long> >maxv;
void upd(long long x,long long va,long long ind) {
pair<long long,long long>val={va,ind};
while(x>0) {
maxv[x]=max(maxv[x],val);
x>>=1;
}
}
pair<long long,long long> ask(long long from,long long to,long long cur=1,long long beg=0,long long en=k-1) {
if(from<=beg && en<=to)return maxv[cur];
if(to<beg || en<from)return {-1,-1};
long long 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);cout.tie(0);
long long n,q;cin>>n>>q;
pair<long long,long long>a[n];
for(long long i=0;i<n;i++) {
long long x,y;
cin>>x>>y;
a[i]={x,y};
upd(y+k,k-x,i);
}
pair<long long,long long> prev[n][25];
for(long long i=0;i<n;i++) {
pair<long long,long long>d=ask(a[i].first,a[i].second);
d.first=k-d.first;
prev[i][0]=d;
}
for(long long j=1;j<25;j++) {
for(long long i=0;i<n;i++) {
prev[i][j]=prev[prev[i][j-1].second][j-1];
}
}
for(long long i=0;i<q;i++) {
long long x,y;cin>>x>>y;x--;y--;
if(x==y)cout<<0<<endl;
else if(a[x].second>=a[y].first && a[x].second<=a[y].second)cout<<1<<endl;
else if(a[x].second>a[y].second){cout<<"impossible"<<endl;}
else {
long long cnt=0;
long long cur=y;
long long en=a[x].second;
for(long long j=24;j>=0;j--) {
if(prev[cur][j].first>en) {
cur=prev[cur][j].second;
cnt+=(1<<j);
}
}
if(prev[cur][0].first>en)cout<<"impossible"<<endl;
else cout<<cnt+2<<endl;
}
}
return 0;
}
///9:30
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1454 ms |
185648 KB |
Output is correct |
3 |
Correct |
1495 ms |
194836 KB |
Output is correct |
4 |
Execution timed out |
1589 ms |
198144 KB |
Time limit exceeded |
5 |
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 |
9 ms |
2900 KB |
Output is correct |
4 |
Correct |
10 ms |
3016 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
9 ms |
1864 KB |
Output is correct |
7 |
Correct |
12 ms |
3156 KB |
Output is correct |
8 |
Correct |
12 ms |
3028 KB |
Output is correct |
9 |
Correct |
3 ms |
724 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 |
9 ms |
2900 KB |
Output is correct |
4 |
Correct |
10 ms |
3016 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
9 ms |
1864 KB |
Output is correct |
7 |
Correct |
12 ms |
3156 KB |
Output is correct |
8 |
Correct |
12 ms |
3028 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
2900 KB |
Output is correct |
13 |
Correct |
10 ms |
3020 KB |
Output is correct |
14 |
Correct |
3 ms |
852 KB |
Output is correct |
15 |
Correct |
8 ms |
1912 KB |
Output is correct |
16 |
Correct |
11 ms |
3104 KB |
Output is correct |
17 |
Correct |
12 ms |
3008 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
47 ms |
4532 KB |
Output is correct |
20 |
Correct |
90 ms |
13344 KB |
Output is correct |
21 |
Correct |
50 ms |
4728 KB |
Output is correct |
22 |
Correct |
70 ms |
9408 KB |
Output is correct |
23 |
Correct |
95 ms |
14640 KB |
Output is correct |
24 |
Correct |
97 ms |
14464 KB |
Output is correct |
25 |
Correct |
90 ms |
14596 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 |
9 ms |
2900 KB |
Output is correct |
4 |
Correct |
10 ms |
3016 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
9 ms |
1864 KB |
Output is correct |
7 |
Correct |
12 ms |
3156 KB |
Output is correct |
8 |
Correct |
12 ms |
3028 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
10 ms |
2900 KB |
Output is correct |
13 |
Correct |
11 ms |
3028 KB |
Output is correct |
14 |
Correct |
3 ms |
852 KB |
Output is correct |
15 |
Correct |
8 ms |
1912 KB |
Output is correct |
16 |
Correct |
12 ms |
3148 KB |
Output is correct |
17 |
Correct |
13 ms |
3036 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
1468 ms |
188760 KB |
Output is correct |
20 |
Correct |
534 ms |
54704 KB |
Output is correct |
21 |
Correct |
1334 ms |
120076 KB |
Output is correct |
22 |
Correct |
1370 ms |
172252 KB |
Output is correct |
23 |
Execution timed out |
1587 ms |
180584 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1465 ms |
185768 KB |
Output is correct |
2 |
Correct |
1478 ms |
194784 KB |
Output is correct |
3 |
Execution timed out |
1572 ms |
197980 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1454 ms |
185648 KB |
Output is correct |
3 |
Correct |
1495 ms |
194836 KB |
Output is correct |
4 |
Execution timed out |
1589 ms |
198144 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |