#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
const int LG=18;
const int INF=INT_MAX/2;
int n,m;
int l[N],r[N],vals[2*N];
int ans=INF;
struct Fenwick{
int t[2*N];
void update(int i,int v){
for(;i<2*N;i+=i&-i)t[i]=max(t[i],v);
}
int query(int i){
int res=0;
for(;i>0;i-=i&-i)res=max(res,t[i]);
return res;
}
}f[LG];
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(int i=1;i<=n;i++){
cin >> l[i] >> r[i];
vals[i*2-1]=l[i],vals[i*2]=r[i];
}
sort(vals+1,vals+2*n+1);
for(int i=1;i<=n;i++){
l[i]=lower_bound(vals+1,vals+2*n+1,l[i])-vals;
r[i]=lower_bound(vals+1,vals+2*n+1,r[i])-vals;
}
for(int i=1;i<=n;i++)if(l[i]<r[i])f[0].update(l[i],r[i]);
for(int k=1;k<LG;k++){
for(int i=1;i<=2*n;i++){
f[k].update(i,f[k-1].query(f[k-1].query(i)));
}
}
for(int i=1;i<=n;i++)if(r[i]<l[i]){
int p=r[i],res=1;
for(int j=LG-1;j>=0;j--){
int x=f[j].query(p);
if(x>=l[i])continue;
p=x;
res+=1<<j;
}
if(f[0].query(p)<l[i])continue;
ans=min(ans,res+1);
}
cout << (ans==INF?-1:ans) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5208 KB |
Output is correct |
3 |
Correct |
1 ms |
5208 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
5208 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
1 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
5212 KB |
Output is correct |
17 |
Correct |
1 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
5212 KB |
Output is correct |
19 |
Correct |
1 ms |
5208 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5208 KB |
Output is correct |
23 |
Correct |
1 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5208 KB |
Output is correct |
3 |
Correct |
1 ms |
5208 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
5208 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
1 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
5212 KB |
Output is correct |
17 |
Correct |
1 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
5212 KB |
Output is correct |
19 |
Correct |
1 ms |
5208 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5208 KB |
Output is correct |
23 |
Correct |
1 ms |
5212 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
1 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
1 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
1 ms |
5212 KB |
Output is correct |
33 |
Correct |
1 ms |
5212 KB |
Output is correct |
34 |
Correct |
1 ms |
5212 KB |
Output is correct |
35 |
Correct |
1 ms |
5212 KB |
Output is correct |
36 |
Correct |
1 ms |
5212 KB |
Output is correct |
37 |
Correct |
1 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5208 KB |
Output is correct |
3 |
Correct |
1 ms |
5208 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
5208 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
1 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
5212 KB |
Output is correct |
17 |
Correct |
1 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
5212 KB |
Output is correct |
19 |
Correct |
1 ms |
5208 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5208 KB |
Output is correct |
23 |
Correct |
1 ms |
5212 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
1 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
1 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
1 ms |
5212 KB |
Output is correct |
33 |
Correct |
1 ms |
5212 KB |
Output is correct |
34 |
Correct |
1 ms |
5212 KB |
Output is correct |
35 |
Correct |
1 ms |
5212 KB |
Output is correct |
36 |
Correct |
1 ms |
5212 KB |
Output is correct |
37 |
Correct |
1 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
8 ms |
5720 KB |
Output is correct |
40 |
Correct |
7 ms |
5724 KB |
Output is correct |
41 |
Correct |
8 ms |
5768 KB |
Output is correct |
42 |
Correct |
8 ms |
5864 KB |
Output is correct |
43 |
Correct |
8 ms |
5720 KB |
Output is correct |
44 |
Correct |
8 ms |
5724 KB |
Output is correct |
45 |
Correct |
8 ms |
5692 KB |
Output is correct |
46 |
Correct |
7 ms |
5724 KB |
Output is correct |
47 |
Correct |
8 ms |
5724 KB |
Output is correct |
48 |
Correct |
8 ms |
5720 KB |
Output is correct |
49 |
Correct |
9 ms |
5724 KB |
Output is correct |
50 |
Correct |
8 ms |
5844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5212 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Correct |
1 ms |
5324 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5320 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
7 ms |
5724 KB |
Output is correct |
16 |
Correct |
7 ms |
5724 KB |
Output is correct |
17 |
Correct |
7 ms |
5748 KB |
Output is correct |
18 |
Correct |
9 ms |
5820 KB |
Output is correct |
19 |
Correct |
10 ms |
5724 KB |
Output is correct |
20 |
Correct |
216 ms |
30768 KB |
Output is correct |
21 |
Correct |
290 ms |
35504 KB |
Output is correct |
22 |
Correct |
288 ms |
35388 KB |
Output is correct |
23 |
Correct |
283 ms |
34640 KB |
Output is correct |
24 |
Correct |
360 ms |
35372 KB |
Output is correct |
25 |
Correct |
292 ms |
34640 KB |
Output is correct |
26 |
Correct |
337 ms |
35408 KB |
Output is correct |
27 |
Correct |
292 ms |
35412 KB |
Output is correct |
28 |
Correct |
280 ms |
35380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
1 ms |
5080 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
9 ms |
5720 KB |
Output is correct |
13 |
Correct |
10 ms |
5724 KB |
Output is correct |
14 |
Correct |
8 ms |
5776 KB |
Output is correct |
15 |
Correct |
235 ms |
31024 KB |
Output is correct |
16 |
Correct |
289 ms |
34508 KB |
Output is correct |
17 |
Correct |
289 ms |
35408 KB |
Output is correct |
18 |
Correct |
322 ms |
35412 KB |
Output is correct |
19 |
Correct |
313 ms |
35516 KB |
Output is correct |
20 |
Correct |
312 ms |
34644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5208 KB |
Output is correct |
3 |
Correct |
1 ms |
5208 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
5208 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
1 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
5212 KB |
Output is correct |
17 |
Correct |
1 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
5212 KB |
Output is correct |
19 |
Correct |
1 ms |
5208 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
5212 KB |
Output is correct |
22 |
Correct |
1 ms |
5208 KB |
Output is correct |
23 |
Correct |
1 ms |
5212 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
1 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
1 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
1 ms |
5212 KB |
Output is correct |
33 |
Correct |
1 ms |
5212 KB |
Output is correct |
34 |
Correct |
1 ms |
5212 KB |
Output is correct |
35 |
Correct |
1 ms |
5212 KB |
Output is correct |
36 |
Correct |
1 ms |
5212 KB |
Output is correct |
37 |
Correct |
1 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
8 ms |
5720 KB |
Output is correct |
40 |
Correct |
7 ms |
5724 KB |
Output is correct |
41 |
Correct |
8 ms |
5768 KB |
Output is correct |
42 |
Correct |
8 ms |
5864 KB |
Output is correct |
43 |
Correct |
8 ms |
5720 KB |
Output is correct |
44 |
Correct |
8 ms |
5724 KB |
Output is correct |
45 |
Correct |
8 ms |
5692 KB |
Output is correct |
46 |
Correct |
7 ms |
5724 KB |
Output is correct |
47 |
Correct |
8 ms |
5724 KB |
Output is correct |
48 |
Correct |
8 ms |
5720 KB |
Output is correct |
49 |
Correct |
9 ms |
5724 KB |
Output is correct |
50 |
Correct |
8 ms |
5844 KB |
Output is correct |
51 |
Correct |
1 ms |
5212 KB |
Output is correct |
52 |
Correct |
1 ms |
5212 KB |
Output is correct |
53 |
Correct |
1 ms |
5324 KB |
Output is correct |
54 |
Correct |
1 ms |
5212 KB |
Output is correct |
55 |
Correct |
1 ms |
5212 KB |
Output is correct |
56 |
Correct |
1 ms |
5212 KB |
Output is correct |
57 |
Correct |
1 ms |
5212 KB |
Output is correct |
58 |
Correct |
1 ms |
5320 KB |
Output is correct |
59 |
Correct |
1 ms |
5212 KB |
Output is correct |
60 |
Correct |
1 ms |
5212 KB |
Output is correct |
61 |
Correct |
1 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5212 KB |
Output is correct |
63 |
Correct |
1 ms |
5212 KB |
Output is correct |
64 |
Correct |
1 ms |
5212 KB |
Output is correct |
65 |
Correct |
7 ms |
5724 KB |
Output is correct |
66 |
Correct |
7 ms |
5724 KB |
Output is correct |
67 |
Correct |
7 ms |
5748 KB |
Output is correct |
68 |
Correct |
9 ms |
5820 KB |
Output is correct |
69 |
Correct |
10 ms |
5724 KB |
Output is correct |
70 |
Correct |
216 ms |
30768 KB |
Output is correct |
71 |
Correct |
290 ms |
35504 KB |
Output is correct |
72 |
Correct |
288 ms |
35388 KB |
Output is correct |
73 |
Correct |
283 ms |
34640 KB |
Output is correct |
74 |
Correct |
360 ms |
35372 KB |
Output is correct |
75 |
Correct |
292 ms |
34640 KB |
Output is correct |
76 |
Correct |
337 ms |
35408 KB |
Output is correct |
77 |
Correct |
292 ms |
35412 KB |
Output is correct |
78 |
Correct |
280 ms |
35380 KB |
Output is correct |
79 |
Correct |
1 ms |
5212 KB |
Output is correct |
80 |
Correct |
2 ms |
5212 KB |
Output is correct |
81 |
Correct |
1 ms |
5080 KB |
Output is correct |
82 |
Correct |
1 ms |
5212 KB |
Output is correct |
83 |
Correct |
1 ms |
5212 KB |
Output is correct |
84 |
Correct |
1 ms |
5212 KB |
Output is correct |
85 |
Correct |
1 ms |
5212 KB |
Output is correct |
86 |
Correct |
1 ms |
5212 KB |
Output is correct |
87 |
Correct |
1 ms |
5212 KB |
Output is correct |
88 |
Correct |
1 ms |
5212 KB |
Output is correct |
89 |
Correct |
1 ms |
5212 KB |
Output is correct |
90 |
Correct |
9 ms |
5720 KB |
Output is correct |
91 |
Correct |
10 ms |
5724 KB |
Output is correct |
92 |
Correct |
8 ms |
5776 KB |
Output is correct |
93 |
Correct |
235 ms |
31024 KB |
Output is correct |
94 |
Correct |
289 ms |
34508 KB |
Output is correct |
95 |
Correct |
289 ms |
35408 KB |
Output is correct |
96 |
Correct |
322 ms |
35412 KB |
Output is correct |
97 |
Correct |
313 ms |
35516 KB |
Output is correct |
98 |
Correct |
312 ms |
34644 KB |
Output is correct |
99 |
Correct |
308 ms |
35408 KB |
Output is correct |
100 |
Correct |
298 ms |
35528 KB |
Output is correct |
101 |
Correct |
334 ms |
35408 KB |
Output is correct |
102 |
Correct |
314 ms |
35412 KB |
Output is correct |
103 |
Correct |
305 ms |
35376 KB |
Output is correct |
104 |
Correct |
333 ms |
35412 KB |
Output is correct |
105 |
Correct |
263 ms |
34132 KB |
Output is correct |