#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (ll)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 4e5 + 10;
const int L = sqrt(N) + 10;
int nxt[N][20], is[N][20];
ll solve() {
int n, m;
cin >> n >> m;
map <int, int> mp;
vector <pair <int, int>> seg(n);
for (auto &x : seg) {
cin >> x.f >> x.s;
mp[x.s], mp[x.f];
}
int timer = 0;
for (auto &x : mp) {
timer++;
x.s = timer;
}
for (auto &x : seg) {
x.f = mp[x.f];
x.s = mp[x.s];
}
sort(all(seg));
int j = 0, ov = 0, mx = 0;
for (int i = 1; i <= timer; i++) {
while (j < sz(seg) && seg[j].f <= i) {
if (ov) {
if (seg[j].f > seg[j].s) {
mx = max(mx, seg[j].s);
}
} else {
if (seg[j].f > seg[j].s) {
ov = 1;
mx = seg[j].s;
} else {
mx = max(mx, seg[j].s);
}
}
j++;
}
if (mx) {
nxt[i][0] = mx;
is[i][0] = ov;
} else {
nxt[i][0] = i;
}
}
if (ov) {
for (int i = 1; i <= mx; i++) {
if (is[i][0] == 0) {
nxt[i][0] = max(nxt[i][0], mx);
}
}
}
for (int j = 1; j < 20; j++) {
for (int i = 1; i <= timer; i++) {
nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
is[i][j] = is[i][j - 1] | is[nxt[i][j - 1]][j - 1];
}
}
ll ans = 1e18;
for (auto x : seg) {
int l = 0, r = 0;
ll cc = 1;
if (x.f < x.s) {
l = x.f, r = x.s;
for (int j = 19; j >= 0; j--) {
if (is[r][j] == 0) {
r = nxt[r][j];
cc = cc + ((ll)1 << j);
}
}
if (is[r][0] == 0)
continue;
r = nxt[r][0];
cc++;
} else {
r = x.s, l = x.f;
}
for (int j = 19; j >= 0; j--) {
if (is[r][j] == 0 && nxt[r][j] < l) {
r = nxt[r][j];
cc = cc + ((ll)1 << j);
}
}
if (is[r][0] || nxt[r][0] >= l) {
ans = min(ans, cc + 1);
}
}
if (ans > n)
return -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
cout << solve() << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
432 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
432 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
0 ms |
484 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
432 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
0 ms |
484 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
6 ms |
2396 KB |
Output is correct |
40 |
Correct |
6 ms |
2500 KB |
Output is correct |
41 |
Correct |
5 ms |
2392 KB |
Output is correct |
42 |
Correct |
4 ms |
1372 KB |
Output is correct |
43 |
Correct |
4 ms |
1296 KB |
Output is correct |
44 |
Correct |
4 ms |
1628 KB |
Output is correct |
45 |
Correct |
4 ms |
1628 KB |
Output is correct |
46 |
Correct |
4 ms |
1504 KB |
Output is correct |
47 |
Correct |
5 ms |
2396 KB |
Output is correct |
48 |
Correct |
4 ms |
2528 KB |
Output is correct |
49 |
Correct |
4 ms |
2396 KB |
Output is correct |
50 |
Correct |
4 ms |
1588 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
2480 KB |
Output is correct |
16 |
Correct |
4 ms |
1372 KB |
Output is correct |
17 |
Correct |
3 ms |
1376 KB |
Output is correct |
18 |
Correct |
4 ms |
1632 KB |
Output is correct |
19 |
Correct |
5 ms |
2400 KB |
Output is correct |
20 |
Correct |
25 ms |
2836 KB |
Output is correct |
21 |
Correct |
405 ms |
87128 KB |
Output is correct |
22 |
Correct |
394 ms |
87124 KB |
Output is correct |
23 |
Correct |
254 ms |
45564 KB |
Output is correct |
24 |
Correct |
369 ms |
87100 KB |
Output is correct |
25 |
Correct |
224 ms |
37632 KB |
Output is correct |
26 |
Correct |
258 ms |
46416 KB |
Output is correct |
27 |
Correct |
242 ms |
46416 KB |
Output is correct |
28 |
Correct |
360 ms |
85348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
2632 KB |
Output is correct |
13 |
Correct |
4 ms |
1372 KB |
Output is correct |
14 |
Correct |
4 ms |
1372 KB |
Output is correct |
15 |
Correct |
25 ms |
2668 KB |
Output is correct |
16 |
Correct |
256 ms |
45396 KB |
Output is correct |
17 |
Correct |
379 ms |
87124 KB |
Output is correct |
18 |
Correct |
376 ms |
87120 KB |
Output is correct |
19 |
Correct |
384 ms |
87120 KB |
Output is correct |
20 |
Correct |
244 ms |
45564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
432 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
0 ms |
484 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
6 ms |
2396 KB |
Output is correct |
40 |
Correct |
6 ms |
2500 KB |
Output is correct |
41 |
Correct |
5 ms |
2392 KB |
Output is correct |
42 |
Correct |
4 ms |
1372 KB |
Output is correct |
43 |
Correct |
4 ms |
1296 KB |
Output is correct |
44 |
Correct |
4 ms |
1628 KB |
Output is correct |
45 |
Correct |
4 ms |
1628 KB |
Output is correct |
46 |
Correct |
4 ms |
1504 KB |
Output is correct |
47 |
Correct |
5 ms |
2396 KB |
Output is correct |
48 |
Correct |
4 ms |
2528 KB |
Output is correct |
49 |
Correct |
4 ms |
2396 KB |
Output is correct |
50 |
Correct |
4 ms |
1588 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
460 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
5 ms |
2480 KB |
Output is correct |
66 |
Correct |
4 ms |
1372 KB |
Output is correct |
67 |
Correct |
3 ms |
1376 KB |
Output is correct |
68 |
Correct |
4 ms |
1632 KB |
Output is correct |
69 |
Correct |
5 ms |
2400 KB |
Output is correct |
70 |
Correct |
25 ms |
2836 KB |
Output is correct |
71 |
Correct |
405 ms |
87128 KB |
Output is correct |
72 |
Correct |
394 ms |
87124 KB |
Output is correct |
73 |
Correct |
254 ms |
45564 KB |
Output is correct |
74 |
Correct |
369 ms |
87100 KB |
Output is correct |
75 |
Correct |
224 ms |
37632 KB |
Output is correct |
76 |
Correct |
258 ms |
46416 KB |
Output is correct |
77 |
Correct |
242 ms |
46416 KB |
Output is correct |
78 |
Correct |
360 ms |
85348 KB |
Output is correct |
79 |
Correct |
0 ms |
600 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
604 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
1 ms |
344 KB |
Output is correct |
90 |
Correct |
5 ms |
2632 KB |
Output is correct |
91 |
Correct |
4 ms |
1372 KB |
Output is correct |
92 |
Correct |
4 ms |
1372 KB |
Output is correct |
93 |
Correct |
25 ms |
2668 KB |
Output is correct |
94 |
Correct |
256 ms |
45396 KB |
Output is correct |
95 |
Correct |
379 ms |
87124 KB |
Output is correct |
96 |
Correct |
376 ms |
87120 KB |
Output is correct |
97 |
Correct |
384 ms |
87120 KB |
Output is correct |
98 |
Correct |
244 ms |
45564 KB |
Output is correct |
99 |
Correct |
331 ms |
87120 KB |
Output is correct |
100 |
Correct |
368 ms |
87120 KB |
Output is correct |
101 |
Correct |
346 ms |
87124 KB |
Output is correct |
102 |
Correct |
362 ms |
87020 KB |
Output is correct |
103 |
Correct |
250 ms |
46544 KB |
Output is correct |
104 |
Correct |
310 ms |
83120 KB |
Output is correct |
105 |
Correct |
207 ms |
85840 KB |
Output is correct |