#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;}
if(b[c]>b[d]){cout<<"impossible"<<endl;continue;}
if(b[c]>=a[d]){cout<<1<<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(ans==262143){
cout<<"impossible"<<endl;
continue;
}
cout<<ans+2<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56656 KB |
Output is correct |
2 |
Correct |
930 ms |
59876 KB |
Output is correct |
3 |
Correct |
930 ms |
59692 KB |
Output is correct |
4 |
Correct |
1004 ms |
59800 KB |
Output is correct |
5 |
Correct |
933 ms |
59936 KB |
Output is correct |
6 |
Correct |
909 ms |
59852 KB |
Output is correct |
7 |
Correct |
899 ms |
59808 KB |
Output is correct |
8 |
Correct |
734 ms |
60316 KB |
Output is correct |
9 |
Correct |
698 ms |
60368 KB |
Output is correct |
10 |
Correct |
836 ms |
60112 KB |
Output is correct |
11 |
Correct |
872 ms |
63432 KB |
Output is correct |
12 |
Correct |
488 ms |
62760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56660 KB |
Output is correct |
2 |
Correct |
37 ms |
56668 KB |
Output is correct |
3 |
Correct |
40 ms |
56656 KB |
Output is correct |
4 |
Correct |
44 ms |
56668 KB |
Output is correct |
5 |
Correct |
40 ms |
56820 KB |
Output is correct |
6 |
Correct |
44 ms |
56912 KB |
Output is correct |
7 |
Correct |
41 ms |
56668 KB |
Output is correct |
8 |
Correct |
40 ms |
56628 KB |
Output is correct |
9 |
Correct |
39 ms |
56660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56660 KB |
Output is correct |
2 |
Correct |
37 ms |
56668 KB |
Output is correct |
3 |
Correct |
40 ms |
56656 KB |
Output is correct |
4 |
Correct |
44 ms |
56668 KB |
Output is correct |
5 |
Correct |
40 ms |
56820 KB |
Output is correct |
6 |
Correct |
44 ms |
56912 KB |
Output is correct |
7 |
Correct |
41 ms |
56668 KB |
Output is correct |
8 |
Correct |
40 ms |
56628 KB |
Output is correct |
9 |
Correct |
39 ms |
56660 KB |
Output is correct |
10 |
Correct |
35 ms |
56656 KB |
Output is correct |
11 |
Correct |
37 ms |
56764 KB |
Output is correct |
12 |
Correct |
42 ms |
56680 KB |
Output is correct |
13 |
Correct |
41 ms |
56852 KB |
Output is correct |
14 |
Correct |
41 ms |
56796 KB |
Output is correct |
15 |
Correct |
40 ms |
56660 KB |
Output is correct |
16 |
Correct |
44 ms |
56656 KB |
Output is correct |
17 |
Correct |
40 ms |
56680 KB |
Output is correct |
18 |
Correct |
39 ms |
56656 KB |
Output is correct |
19 |
Correct |
293 ms |
58448 KB |
Output is correct |
20 |
Correct |
273 ms |
58704 KB |
Output is correct |
21 |
Correct |
192 ms |
58708 KB |
Output is correct |
22 |
Correct |
120 ms |
58752 KB |
Output is correct |
23 |
Correct |
112 ms |
58656 KB |
Output is correct |
24 |
Correct |
75 ms |
58448 KB |
Output is correct |
25 |
Correct |
267 ms |
58192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56660 KB |
Output is correct |
2 |
Correct |
37 ms |
56668 KB |
Output is correct |
3 |
Correct |
40 ms |
56656 KB |
Output is correct |
4 |
Correct |
44 ms |
56668 KB |
Output is correct |
5 |
Correct |
40 ms |
56820 KB |
Output is correct |
6 |
Correct |
44 ms |
56912 KB |
Output is correct |
7 |
Correct |
41 ms |
56668 KB |
Output is correct |
8 |
Correct |
40 ms |
56628 KB |
Output is correct |
9 |
Correct |
39 ms |
56660 KB |
Output is correct |
10 |
Correct |
34 ms |
56668 KB |
Output is correct |
11 |
Correct |
41 ms |
56684 KB |
Output is correct |
12 |
Correct |
43 ms |
56816 KB |
Output is correct |
13 |
Correct |
49 ms |
56840 KB |
Output is correct |
14 |
Correct |
41 ms |
56668 KB |
Output is correct |
15 |
Correct |
41 ms |
56736 KB |
Output is correct |
16 |
Correct |
40 ms |
56656 KB |
Output is correct |
17 |
Correct |
42 ms |
56656 KB |
Output is correct |
18 |
Correct |
38 ms |
56668 KB |
Output is correct |
19 |
Correct |
645 ms |
61016 KB |
Output is correct |
20 |
Correct |
634 ms |
60368 KB |
Output is correct |
21 |
Correct |
578 ms |
61136 KB |
Output is correct |
22 |
Correct |
635 ms |
61128 KB |
Output is correct |
23 |
Correct |
585 ms |
61164 KB |
Output is correct |
24 |
Correct |
675 ms |
61132 KB |
Output is correct |
25 |
Correct |
299 ms |
60372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
911 ms |
59844 KB |
Output is correct |
2 |
Correct |
948 ms |
59904 KB |
Output is correct |
3 |
Correct |
970 ms |
59808 KB |
Output is correct |
4 |
Correct |
703 ms |
60316 KB |
Output is correct |
5 |
Correct |
831 ms |
60056 KB |
Output is correct |
6 |
Correct |
1132 ms |
60104 KB |
Output is correct |
7 |
Correct |
1089 ms |
63080 KB |
Output is correct |
8 |
Correct |
1080 ms |
63504 KB |
Output is correct |
9 |
Correct |
578 ms |
61132 KB |
Output is correct |
10 |
Correct |
942 ms |
62784 KB |
Output is correct |
11 |
Correct |
846 ms |
62484 KB |
Output is correct |
12 |
Correct |
933 ms |
62672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
56656 KB |
Output is correct |
2 |
Correct |
930 ms |
59876 KB |
Output is correct |
3 |
Correct |
930 ms |
59692 KB |
Output is correct |
4 |
Correct |
1004 ms |
59800 KB |
Output is correct |
5 |
Correct |
933 ms |
59936 KB |
Output is correct |
6 |
Correct |
909 ms |
59852 KB |
Output is correct |
7 |
Correct |
899 ms |
59808 KB |
Output is correct |
8 |
Correct |
734 ms |
60316 KB |
Output is correct |
9 |
Correct |
698 ms |
60368 KB |
Output is correct |
10 |
Correct |
836 ms |
60112 KB |
Output is correct |
11 |
Correct |
872 ms |
63432 KB |
Output is correct |
12 |
Correct |
488 ms |
62760 KB |
Output is correct |
13 |
Correct |
35 ms |
56660 KB |
Output is correct |
14 |
Correct |
37 ms |
56668 KB |
Output is correct |
15 |
Correct |
40 ms |
56656 KB |
Output is correct |
16 |
Correct |
44 ms |
56668 KB |
Output is correct |
17 |
Correct |
40 ms |
56820 KB |
Output is correct |
18 |
Correct |
44 ms |
56912 KB |
Output is correct |
19 |
Correct |
41 ms |
56668 KB |
Output is correct |
20 |
Correct |
40 ms |
56628 KB |
Output is correct |
21 |
Correct |
39 ms |
56660 KB |
Output is correct |
22 |
Correct |
35 ms |
56656 KB |
Output is correct |
23 |
Correct |
37 ms |
56764 KB |
Output is correct |
24 |
Correct |
42 ms |
56680 KB |
Output is correct |
25 |
Correct |
41 ms |
56852 KB |
Output is correct |
26 |
Correct |
41 ms |
56796 KB |
Output is correct |
27 |
Correct |
40 ms |
56660 KB |
Output is correct |
28 |
Correct |
44 ms |
56656 KB |
Output is correct |
29 |
Correct |
40 ms |
56680 KB |
Output is correct |
30 |
Correct |
39 ms |
56656 KB |
Output is correct |
31 |
Correct |
293 ms |
58448 KB |
Output is correct |
32 |
Correct |
273 ms |
58704 KB |
Output is correct |
33 |
Correct |
192 ms |
58708 KB |
Output is correct |
34 |
Correct |
120 ms |
58752 KB |
Output is correct |
35 |
Correct |
112 ms |
58656 KB |
Output is correct |
36 |
Correct |
75 ms |
58448 KB |
Output is correct |
37 |
Correct |
267 ms |
58192 KB |
Output is correct |
38 |
Correct |
34 ms |
56668 KB |
Output is correct |
39 |
Correct |
41 ms |
56684 KB |
Output is correct |
40 |
Correct |
43 ms |
56816 KB |
Output is correct |
41 |
Correct |
49 ms |
56840 KB |
Output is correct |
42 |
Correct |
41 ms |
56668 KB |
Output is correct |
43 |
Correct |
41 ms |
56736 KB |
Output is correct |
44 |
Correct |
40 ms |
56656 KB |
Output is correct |
45 |
Correct |
42 ms |
56656 KB |
Output is correct |
46 |
Correct |
38 ms |
56668 KB |
Output is correct |
47 |
Correct |
645 ms |
61016 KB |
Output is correct |
48 |
Correct |
634 ms |
60368 KB |
Output is correct |
49 |
Correct |
578 ms |
61136 KB |
Output is correct |
50 |
Correct |
635 ms |
61128 KB |
Output is correct |
51 |
Correct |
585 ms |
61164 KB |
Output is correct |
52 |
Correct |
675 ms |
61132 KB |
Output is correct |
53 |
Correct |
299 ms |
60372 KB |
Output is correct |
54 |
Correct |
911 ms |
59844 KB |
Output is correct |
55 |
Correct |
948 ms |
59904 KB |
Output is correct |
56 |
Correct |
970 ms |
59808 KB |
Output is correct |
57 |
Correct |
703 ms |
60316 KB |
Output is correct |
58 |
Correct |
831 ms |
60056 KB |
Output is correct |
59 |
Correct |
1132 ms |
60104 KB |
Output is correct |
60 |
Correct |
1089 ms |
63080 KB |
Output is correct |
61 |
Correct |
1080 ms |
63504 KB |
Output is correct |
62 |
Correct |
578 ms |
61132 KB |
Output is correct |
63 |
Correct |
942 ms |
62784 KB |
Output is correct |
64 |
Correct |
846 ms |
62484 KB |
Output is correct |
65 |
Correct |
933 ms |
62672 KB |
Output is correct |
66 |
Correct |
37 ms |
56664 KB |
Output is correct |
67 |
Correct |
914 ms |
62892 KB |
Output is correct |
68 |
Correct |
930 ms |
62996 KB |
Output is correct |
69 |
Correct |
1000 ms |
62920 KB |
Output is correct |
70 |
Correct |
896 ms |
62972 KB |
Output is correct |
71 |
Correct |
912 ms |
63056 KB |
Output is correct |
72 |
Correct |
902 ms |
63264 KB |
Output is correct |
73 |
Correct |
705 ms |
63372 KB |
Output is correct |
74 |
Correct |
701 ms |
63436 KB |
Output is correct |
75 |
Correct |
972 ms |
63440 KB |
Output is correct |
76 |
Correct |
1010 ms |
63284 KB |
Output is correct |
77 |
Correct |
468 ms |
62732 KB |
Output is correct |
78 |
Correct |
37 ms |
56656 KB |
Output is correct |
79 |
Correct |
43 ms |
56668 KB |
Output is correct |
80 |
Correct |
44 ms |
56668 KB |
Output is correct |
81 |
Correct |
41 ms |
56820 KB |
Output is correct |
82 |
Correct |
41 ms |
56664 KB |
Output is correct |
83 |
Correct |
42 ms |
56844 KB |
Output is correct |
84 |
Correct |
43 ms |
56668 KB |
Output is correct |
85 |
Correct |
41 ms |
56668 KB |
Output is correct |
86 |
Correct |
295 ms |
58232 KB |
Output is correct |
87 |
Correct |
302 ms |
58840 KB |
Output is correct |
88 |
Correct |
184 ms |
58704 KB |
Output is correct |
89 |
Correct |
119 ms |
58704 KB |
Output is correct |
90 |
Correct |
121 ms |
58460 KB |
Output is correct |
91 |
Correct |
73 ms |
58492 KB |
Output is correct |
92 |
Correct |
271 ms |
58240 KB |
Output is correct |
93 |
Correct |
637 ms |
61024 KB |
Output is correct |
94 |
Correct |
644 ms |
60372 KB |
Output is correct |
95 |
Correct |
574 ms |
61136 KB |
Output is correct |
96 |
Correct |
635 ms |
61036 KB |
Output is correct |
97 |
Correct |
573 ms |
61132 KB |
Output is correct |
98 |
Correct |
676 ms |
61152 KB |
Output is correct |
99 |
Correct |
305 ms |
60560 KB |
Output is correct |
100 |
Correct |
1103 ms |
63164 KB |
Output is correct |
101 |
Correct |
1108 ms |
63188 KB |
Output is correct |
102 |
Correct |
1107 ms |
63256 KB |
Output is correct |
103 |
Correct |
943 ms |
62672 KB |
Output is correct |
104 |
Correct |
843 ms |
62540 KB |
Output is correct |
105 |
Correct |
957 ms |
62752 KB |
Output is correct |
106 |
Correct |
860 ms |
62228 KB |
Output is correct |
107 |
Correct |
1058 ms |
62392 KB |
Output is correct |
108 |
Correct |
810 ms |
63020 KB |
Output is correct |
109 |
Correct |
830 ms |
63056 KB |
Output is correct |
110 |
Correct |
887 ms |
62996 KB |
Output is correct |
111 |
Correct |
843 ms |
63028 KB |
Output is correct |