#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
struct event{
int start,end,pos;
};
bool cmp(event a,event b){
if(a.end==b.end) return a.start<b.start;
return a.end<b.end;
}
struct st{
vector<pair<int,int> > seg;
st(vector<int> &vec,int n){
seg.resize(4*n+4);
build(1,n,1,vec);
}
void build(int l,int r,int ind,vector<int> &vec){
if(l==r){
if(vec[l-1]==-1) seg[ind]=make_pair(1e9+1,l-1);
else seg[ind]=make_pair(vec[l-1],l-1);
return;
}
int mid=(l+r)>>1;
build(l,mid,ind*2,vec);
build(mid+1,r,ind*2+1,vec);
seg[ind]=min(seg[ind*2],seg[ind*2+1]);
}
pair<int,int> query(int l,int r,int start,int end,int ind){
if(r<start||end<l) return make_pair(1e9+1,1e9+1);
if(start<=l&&r<=end) return seg[ind];
int mid=(l+r)>>1;
return min(query(l,mid,start,end,ind*2),query(mid+1,r,start,end,ind*2+1));
}};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,q;
cin>>n>>q;
vector<event> vec(n);
int a,b;
for(int i=0;i<n;i++){
cin>>a>>b;
vec[i].start=a;
vec[i].end=b;
vec[i].pos=i;
}
vector<int> ori(n+1);
sort(vec.begin(),vec.end(),cmp);
for(int i=0;i<n;i++) ori[vec[i].pos+1]=i;
vector<vector<int> > anc(17,vector<int>(n));
anc[0][0]=-1;
//for(int i=0;i<n;i++) cout<<"\n"<<vec[i].start<<" "<<vec[i].end;
for(int i=1;i<n;i++){
if(vec[i-1].end<vec[i].start){
anc[0][i]=-1;
continue;
}
int l=0,r=i-1;
while(l<r){
int mid=(l+r)>>1;
if(vec[mid].end<vec[i].start){
l=mid+1;
}
else{
r=mid;
}
}
anc[0][i]=l;
}
vector<int> emp(1,1);
vector<st> omg(17,st(emp,1));
for(int i=1;i<17;i++){
omg[i-1]=st(anc[i-1],n);
for(int j=0;j<n;j++){
if(anc[i-1][j]==-1) {
anc[i][j]=-1;
continue;
}
int tmp=omg[i-1].query(1,n,anc[i-1][j]+1,j+1,1).first;
if(tmp==1e9+1) anc[i][j]=-1;
else anc[i][j]=tmp;
}
}
vector<int> bruv(n);
for(int i=0;i<n;i++) bruv[i]=vec[i].start;
omg[16]=st(bruv,n);
/*for(int i=0;i<17;i++){
for(int j=0;j<n;j++){
cout<<anc[i][j]<<" ";
}
cout<<"\n";
}*/
for(int i=0;i<q;i++){
cin>>a>>b;
if(a==b){
cout<<"0\n";
continue;
}
a=ori[a],b=ori[b];
if(vec[a].end==vec[b].end){
cout<<"1\n";
continue;
}
if(a>b){
cout<<"impossible\n";
continue;
}
int ans=0;
for(int j=16;j>=0;j--){
if(anc[j][b]>a&&anc[j][b]!=-1) b=omg[16].query(1,n,anc[j][b]+1,b+1,1).second,ans+=(1<<j);
}
if(anc[0][b]<=a&&anc[0][b]!=-1){
cout<<ans+1<<"\n";
}
else cout<<"impossible\n";
}
}
/*1 3
2 4
3 7
4 7
7 9*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
381 ms |
66032 KB |
Output is correct |
3 |
Correct |
411 ms |
66068 KB |
Output is correct |
4 |
Correct |
446 ms |
65820 KB |
Output is correct |
5 |
Correct |
436 ms |
65972 KB |
Output is correct |
6 |
Correct |
391 ms |
66408 KB |
Output is correct |
7 |
Correct |
378 ms |
65996 KB |
Output is correct |
8 |
Correct |
73 ms |
66564 KB |
Output is correct |
9 |
Correct |
80 ms |
66524 KB |
Output is correct |
10 |
Correct |
336 ms |
66260 KB |
Output is correct |
11 |
Correct |
296 ms |
66760 KB |
Output is correct |
12 |
Correct |
144 ms |
65740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
1008 KB |
Output is correct |
7 |
Correct |
2 ms |
1004 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
1008 KB |
Output is correct |
7 |
Correct |
2 ms |
1004 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
99 ms |
5072 KB |
Output is correct |
20 |
Correct |
101 ms |
5136 KB |
Output is correct |
21 |
Correct |
75 ms |
5300 KB |
Output is correct |
22 |
Correct |
31 ms |
5468 KB |
Output is correct |
23 |
Correct |
31 ms |
5200 KB |
Output is correct |
24 |
Correct |
26 ms |
5200 KB |
Output is correct |
25 |
Correct |
57 ms |
4884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
1008 KB |
Output is correct |
7 |
Correct |
2 ms |
1004 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
1004 KB |
Output is correct |
14 |
Correct |
3 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
277 ms |
64132 KB |
Output is correct |
20 |
Correct |
266 ms |
63436 KB |
Output is correct |
21 |
Correct |
190 ms |
64200 KB |
Output is correct |
22 |
Correct |
206 ms |
64208 KB |
Output is correct |
23 |
Correct |
216 ms |
64044 KB |
Output is correct |
24 |
Correct |
258 ms |
64384 KB |
Output is correct |
25 |
Correct |
184 ms |
63548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
379 ms |
65992 KB |
Output is correct |
2 |
Correct |
417 ms |
65844 KB |
Output is correct |
3 |
Correct |
442 ms |
65992 KB |
Output is correct |
4 |
Correct |
75 ms |
66512 KB |
Output is correct |
5 |
Correct |
337 ms |
66336 KB |
Output is correct |
6 |
Correct |
366 ms |
66172 KB |
Output is correct |
7 |
Correct |
346 ms |
66000 KB |
Output is correct |
8 |
Correct |
355 ms |
66216 KB |
Output is correct |
9 |
Correct |
197 ms |
64204 KB |
Output is correct |
10 |
Correct |
378 ms |
66016 KB |
Output is correct |
11 |
Correct |
269 ms |
65480 KB |
Output is correct |
12 |
Correct |
386 ms |
65672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
381 ms |
66032 KB |
Output is correct |
3 |
Correct |
411 ms |
66068 KB |
Output is correct |
4 |
Correct |
446 ms |
65820 KB |
Output is correct |
5 |
Correct |
436 ms |
65972 KB |
Output is correct |
6 |
Correct |
391 ms |
66408 KB |
Output is correct |
7 |
Correct |
378 ms |
65996 KB |
Output is correct |
8 |
Correct |
73 ms |
66564 KB |
Output is correct |
9 |
Correct |
80 ms |
66524 KB |
Output is correct |
10 |
Correct |
336 ms |
66260 KB |
Output is correct |
11 |
Correct |
296 ms |
66760 KB |
Output is correct |
12 |
Correct |
144 ms |
65740 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
1008 KB |
Output is correct |
19 |
Correct |
2 ms |
1004 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
99 ms |
5072 KB |
Output is correct |
32 |
Correct |
101 ms |
5136 KB |
Output is correct |
33 |
Correct |
75 ms |
5300 KB |
Output is correct |
34 |
Correct |
31 ms |
5468 KB |
Output is correct |
35 |
Correct |
31 ms |
5200 KB |
Output is correct |
36 |
Correct |
26 ms |
5200 KB |
Output is correct |
37 |
Correct |
57 ms |
4884 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
1004 KB |
Output is correct |
42 |
Correct |
3 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
2 ms |
860 KB |
Output is correct |
47 |
Correct |
277 ms |
64132 KB |
Output is correct |
48 |
Correct |
266 ms |
63436 KB |
Output is correct |
49 |
Correct |
190 ms |
64200 KB |
Output is correct |
50 |
Correct |
206 ms |
64208 KB |
Output is correct |
51 |
Correct |
216 ms |
64044 KB |
Output is correct |
52 |
Correct |
258 ms |
64384 KB |
Output is correct |
53 |
Correct |
184 ms |
63548 KB |
Output is correct |
54 |
Correct |
379 ms |
65992 KB |
Output is correct |
55 |
Correct |
417 ms |
65844 KB |
Output is correct |
56 |
Correct |
442 ms |
65992 KB |
Output is correct |
57 |
Correct |
75 ms |
66512 KB |
Output is correct |
58 |
Correct |
337 ms |
66336 KB |
Output is correct |
59 |
Correct |
366 ms |
66172 KB |
Output is correct |
60 |
Correct |
346 ms |
66000 KB |
Output is correct |
61 |
Correct |
355 ms |
66216 KB |
Output is correct |
62 |
Correct |
197 ms |
64204 KB |
Output is correct |
63 |
Correct |
378 ms |
66016 KB |
Output is correct |
64 |
Correct |
269 ms |
65480 KB |
Output is correct |
65 |
Correct |
386 ms |
65672 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
386 ms |
66000 KB |
Output is correct |
68 |
Correct |
418 ms |
65892 KB |
Output is correct |
69 |
Correct |
434 ms |
66144 KB |
Output is correct |
70 |
Correct |
431 ms |
66080 KB |
Output is correct |
71 |
Correct |
389 ms |
66208 KB |
Output is correct |
72 |
Correct |
381 ms |
66136 KB |
Output is correct |
73 |
Correct |
80 ms |
66536 KB |
Output is correct |
74 |
Correct |
76 ms |
66504 KB |
Output is correct |
75 |
Correct |
349 ms |
66248 KB |
Output is correct |
76 |
Correct |
286 ms |
66508 KB |
Output is correct |
77 |
Correct |
146 ms |
65568 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
2 ms |
1000 KB |
Output is correct |
80 |
Correct |
2 ms |
980 KB |
Output is correct |
81 |
Correct |
2 ms |
860 KB |
Output is correct |
82 |
Correct |
2 ms |
1072 KB |
Output is correct |
83 |
Correct |
2 ms |
976 KB |
Output is correct |
84 |
Correct |
2 ms |
1008 KB |
Output is correct |
85 |
Correct |
2 ms |
860 KB |
Output is correct |
86 |
Correct |
90 ms |
4948 KB |
Output is correct |
87 |
Correct |
103 ms |
4948 KB |
Output is correct |
88 |
Correct |
75 ms |
5208 KB |
Output is correct |
89 |
Correct |
32 ms |
5284 KB |
Output is correct |
90 |
Correct |
31 ms |
5164 KB |
Output is correct |
91 |
Correct |
26 ms |
5236 KB |
Output is correct |
92 |
Correct |
73 ms |
4876 KB |
Output is correct |
93 |
Correct |
274 ms |
64200 KB |
Output is correct |
94 |
Correct |
269 ms |
63492 KB |
Output is correct |
95 |
Correct |
194 ms |
64160 KB |
Output is correct |
96 |
Correct |
223 ms |
64120 KB |
Output is correct |
97 |
Correct |
196 ms |
64200 KB |
Output is correct |
98 |
Correct |
252 ms |
64152 KB |
Output is correct |
99 |
Correct |
194 ms |
63456 KB |
Output is correct |
100 |
Correct |
357 ms |
65992 KB |
Output is correct |
101 |
Correct |
354 ms |
66128 KB |
Output is correct |
102 |
Correct |
353 ms |
66204 KB |
Output is correct |
103 |
Correct |
378 ms |
65856 KB |
Output is correct |
104 |
Correct |
269 ms |
65532 KB |
Output is correct |
105 |
Correct |
388 ms |
65800 KB |
Output is correct |
106 |
Correct |
351 ms |
65480 KB |
Output is correct |
107 |
Correct |
385 ms |
65480 KB |
Output is correct |
108 |
Correct |
263 ms |
66136 KB |
Output is correct |
109 |
Correct |
236 ms |
66144 KB |
Output is correct |
110 |
Correct |
249 ms |
65992 KB |
Output is correct |
111 |
Correct |
264 ms |
65996 KB |
Output is correct |