#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> bruv(n);
for(int i=0;i<n;i++) bruv[i]=vec[i].start;
st omg(bruv,n);
for(int i=1;i<17;i++){
for(int j=0;j<n;j++){
if(anc[i-1][j]==-1) {
anc[i][j]=-1;
continue;
}
int tmp=omg.query(1,n,anc[i-1][j]+1,j+1,1).second;
if(tmp==1e9+1) anc[i][j]=-1;
else anc[i][j]=anc[i-1][tmp];
}
}
/*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.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*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
343 ms |
12740 KB |
Output is correct |
3 |
Correct |
407 ms |
12744 KB |
Output is correct |
4 |
Correct |
405 ms |
12732 KB |
Output is correct |
5 |
Correct |
382 ms |
12748 KB |
Output is correct |
6 |
Correct |
347 ms |
12744 KB |
Output is correct |
7 |
Correct |
335 ms |
12728 KB |
Output is correct |
8 |
Correct |
52 ms |
13260 KB |
Output is correct |
9 |
Correct |
48 ms |
13260 KB |
Output is correct |
10 |
Correct |
329 ms |
12984 KB |
Output is correct |
11 |
Correct |
245 ms |
12992 KB |
Output is correct |
12 |
Correct |
56 ms |
13280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
81 ms |
1500 KB |
Output is correct |
20 |
Correct |
105 ms |
1372 KB |
Output is correct |
21 |
Correct |
73 ms |
1620 KB |
Output is correct |
22 |
Correct |
24 ms |
1820 KB |
Output is correct |
23 |
Correct |
29 ms |
1584 KB |
Output is correct |
24 |
Correct |
19 ms |
1624 KB |
Output is correct |
25 |
Correct |
52 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
247 ms |
12420 KB |
Output is correct |
20 |
Correct |
236 ms |
12212 KB |
Output is correct |
21 |
Correct |
56 ms |
12204 KB |
Output is correct |
22 |
Correct |
166 ms |
12132 KB |
Output is correct |
23 |
Correct |
41 ms |
12132 KB |
Output is correct |
24 |
Correct |
149 ms |
12000 KB |
Output is correct |
25 |
Correct |
32 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
12568 KB |
Output is correct |
2 |
Correct |
383 ms |
12732 KB |
Output is correct |
3 |
Correct |
411 ms |
12624 KB |
Output is correct |
4 |
Correct |
47 ms |
13272 KB |
Output is correct |
5 |
Correct |
324 ms |
12984 KB |
Output is correct |
6 |
Correct |
315 ms |
12748 KB |
Output is correct |
7 |
Correct |
253 ms |
12728 KB |
Output is correct |
8 |
Correct |
241 ms |
13060 KB |
Output is correct |
9 |
Correct |
40 ms |
12072 KB |
Output is correct |
10 |
Correct |
267 ms |
12472 KB |
Output is correct |
11 |
Correct |
131 ms |
12412 KB |
Output is correct |
12 |
Correct |
282 ms |
12492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
343 ms |
12740 KB |
Output is correct |
3 |
Correct |
407 ms |
12744 KB |
Output is correct |
4 |
Correct |
405 ms |
12732 KB |
Output is correct |
5 |
Correct |
382 ms |
12748 KB |
Output is correct |
6 |
Correct |
347 ms |
12744 KB |
Output is correct |
7 |
Correct |
335 ms |
12728 KB |
Output is correct |
8 |
Correct |
52 ms |
13260 KB |
Output is correct |
9 |
Correct |
48 ms |
13260 KB |
Output is correct |
10 |
Correct |
329 ms |
12984 KB |
Output is correct |
11 |
Correct |
245 ms |
12992 KB |
Output is correct |
12 |
Correct |
56 ms |
13280 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
2 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
81 ms |
1500 KB |
Output is correct |
32 |
Correct |
105 ms |
1372 KB |
Output is correct |
33 |
Correct |
73 ms |
1620 KB |
Output is correct |
34 |
Correct |
24 ms |
1820 KB |
Output is correct |
35 |
Correct |
29 ms |
1584 KB |
Output is correct |
36 |
Correct |
19 ms |
1624 KB |
Output is correct |
37 |
Correct |
52 ms |
1116 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
360 KB |
Output is correct |
40 |
Correct |
2 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
2 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
2 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
247 ms |
12420 KB |
Output is correct |
48 |
Correct |
236 ms |
12212 KB |
Output is correct |
49 |
Correct |
56 ms |
12204 KB |
Output is correct |
50 |
Correct |
166 ms |
12132 KB |
Output is correct |
51 |
Correct |
41 ms |
12132 KB |
Output is correct |
52 |
Correct |
149 ms |
12000 KB |
Output is correct |
53 |
Correct |
32 ms |
11988 KB |
Output is correct |
54 |
Correct |
344 ms |
12568 KB |
Output is correct |
55 |
Correct |
383 ms |
12732 KB |
Output is correct |
56 |
Correct |
411 ms |
12624 KB |
Output is correct |
57 |
Correct |
47 ms |
13272 KB |
Output is correct |
58 |
Correct |
324 ms |
12984 KB |
Output is correct |
59 |
Correct |
315 ms |
12748 KB |
Output is correct |
60 |
Correct |
253 ms |
12728 KB |
Output is correct |
61 |
Correct |
241 ms |
13060 KB |
Output is correct |
62 |
Correct |
40 ms |
12072 KB |
Output is correct |
63 |
Correct |
267 ms |
12472 KB |
Output is correct |
64 |
Correct |
131 ms |
12412 KB |
Output is correct |
65 |
Correct |
282 ms |
12492 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
340 ms |
12748 KB |
Output is correct |
68 |
Correct |
384 ms |
12732 KB |
Output is correct |
69 |
Correct |
414 ms |
13236 KB |
Output is correct |
70 |
Correct |
382 ms |
12724 KB |
Output is correct |
71 |
Correct |
350 ms |
12888 KB |
Output is correct |
72 |
Correct |
340 ms |
12776 KB |
Output is correct |
73 |
Correct |
50 ms |
13260 KB |
Output is correct |
74 |
Correct |
63 ms |
13260 KB |
Output is correct |
75 |
Correct |
328 ms |
13100 KB |
Output is correct |
76 |
Correct |
237 ms |
12988 KB |
Output is correct |
77 |
Correct |
47 ms |
13256 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
2 ms |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
344 KB |
Output is correct |
81 |
Correct |
2 ms |
348 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
2 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
348 KB |
Output is correct |
85 |
Correct |
1 ms |
348 KB |
Output is correct |
86 |
Correct |
84 ms |
1312 KB |
Output is correct |
87 |
Correct |
99 ms |
1364 KB |
Output is correct |
88 |
Correct |
70 ms |
1560 KB |
Output is correct |
89 |
Correct |
24 ms |
1628 KB |
Output is correct |
90 |
Correct |
29 ms |
1496 KB |
Output is correct |
91 |
Correct |
19 ms |
1860 KB |
Output is correct |
92 |
Correct |
52 ms |
1112 KB |
Output is correct |
93 |
Correct |
241 ms |
11976 KB |
Output is correct |
94 |
Correct |
236 ms |
12212 KB |
Output is correct |
95 |
Correct |
57 ms |
11976 KB |
Output is correct |
96 |
Correct |
162 ms |
12116 KB |
Output is correct |
97 |
Correct |
42 ms |
11976 KB |
Output is correct |
98 |
Correct |
149 ms |
12212 KB |
Output is correct |
99 |
Correct |
40 ms |
12000 KB |
Output is correct |
100 |
Correct |
304 ms |
12728 KB |
Output is correct |
101 |
Correct |
252 ms |
12724 KB |
Output is correct |
102 |
Correct |
216 ms |
13048 KB |
Output is correct |
103 |
Correct |
266 ms |
12492 KB |
Output is correct |
104 |
Correct |
134 ms |
12236 KB |
Output is correct |
105 |
Correct |
282 ms |
12588 KB |
Output is correct |
106 |
Correct |
269 ms |
12728 KB |
Output is correct |
107 |
Correct |
396 ms |
12984 KB |
Output is correct |
108 |
Correct |
86 ms |
13000 KB |
Output is correct |
109 |
Correct |
85 ms |
13004 KB |
Output is correct |
110 |
Correct |
251 ms |
12724 KB |
Output is correct |
111 |
Correct |
246 ms |
12728 KB |
Output is correct |