#include <bits/stdc++.h>
#define int long long
using namespace std;
struct interval {
int l, r;
};
struct maxpos {
int max, pos;
bool operator < ( const maxpos &x ) const {
return max < x.max;
}
};
const int MAX_N = 2e5;
const int MAX_VAL = 4 * MAX_N;
const int MAX_LOG_N = 17;
const int MAX_LOG_VAL = MAX_LOG_N + 2;
int nxt[MAX_LOG_N + 2][2 * MAX_N];
interval v[2 * MAX_N];
maxpos maxR[MAX_LOG_VAL + 1][MAX_VAL];
map<int, int> normal;
signed main() {
int n, m;
cin >> n >> m;
for ( int i = 0; i < n; i++ ) {
int l, r;
cin >> l >> r;
if ( l > r )
r += m;
v[i] = { l, r };
v[i + n] = { l + m, r + m };
normal[l] = 0;
normal[l + m] = 0;
normal[r] = 0;
normal[r + m] = 0;
}
n *= 2;
int val = 0;
for ( auto x: normal )
normal[x.first] = val++;
//for ( int i = 0; i < n; i++ )
// printf( "%d %d\n", v[i].l, v[i].r );
for ( int i = 0; i < n; i++ )
maxR[0][normal[v[i].l]] = max( maxR[0][normal[v[i].l]], { v[i].r, i } );
for ( int l = 1; (1 << l) <= val; l++ ) {
for ( int i = 0; i <= val - (1 << l); i++ )
maxR[l][i] = max( maxR[l - 1][i], maxR[l - 1][i + (1 << (l - 1))] );
}
for ( int i = 0; i < n; i++ ) {
int l = log2( normal[v[i].r] - normal[v[i].l] );
nxt[0][i] = max( maxR[l][normal[v[i].l]], maxR[l][normal[v[i].r] - (1 << l) + 1] ).pos;
}
for ( int l = 1; (1 << l) <= n; l++ ) {
for ( int i = 0; i < n; i++ )
nxt[l][i] = nxt[l - 1][nxt[l - 1][i]];
}
int minLen = n + 1;
for ( int i = 0; i < n; i++ ) {
int len = 1, p = i;
for ( int l = log2( n ); l >= 0; l-- ) {
if ( v[nxt[l][p]].r < v[i].l + m ) {
p = nxt[l][p];
len += (1 << l);
}
}
if ( v[p].r < v[i].l + m ) {
p = nxt[0][p];
len++;
}
if ( v[p].r >= v[i].l + m )
minLen = min( minLen, len );
}
cout << (minLen == n + 1 ? -1 : minLen);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
14796 KB |
Output is correct |
4 |
Correct |
2 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
1 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
26972 KB |
Output is correct |
9 |
Correct |
2 ms |
24924 KB |
Output is correct |
10 |
Correct |
3 ms |
26972 KB |
Output is correct |
11 |
Correct |
2 ms |
26972 KB |
Output is correct |
12 |
Correct |
2 ms |
24924 KB |
Output is correct |
13 |
Correct |
2 ms |
24924 KB |
Output is correct |
14 |
Correct |
2 ms |
26972 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
24924 KB |
Output is correct |
17 |
Correct |
2 ms |
26972 KB |
Output is correct |
18 |
Correct |
2 ms |
26972 KB |
Output is correct |
19 |
Correct |
2 ms |
26972 KB |
Output is correct |
20 |
Correct |
2 ms |
24924 KB |
Output is correct |
21 |
Correct |
2 ms |
26972 KB |
Output is correct |
22 |
Correct |
2 ms |
26972 KB |
Output is correct |
23 |
Correct |
3 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
14796 KB |
Output is correct |
4 |
Correct |
2 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
1 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
26972 KB |
Output is correct |
9 |
Correct |
2 ms |
24924 KB |
Output is correct |
10 |
Correct |
3 ms |
26972 KB |
Output is correct |
11 |
Correct |
2 ms |
26972 KB |
Output is correct |
12 |
Correct |
2 ms |
24924 KB |
Output is correct |
13 |
Correct |
2 ms |
24924 KB |
Output is correct |
14 |
Correct |
2 ms |
26972 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
24924 KB |
Output is correct |
17 |
Correct |
2 ms |
26972 KB |
Output is correct |
18 |
Correct |
2 ms |
26972 KB |
Output is correct |
19 |
Correct |
2 ms |
26972 KB |
Output is correct |
20 |
Correct |
2 ms |
24924 KB |
Output is correct |
21 |
Correct |
2 ms |
26972 KB |
Output is correct |
22 |
Correct |
2 ms |
26972 KB |
Output is correct |
23 |
Correct |
3 ms |
26972 KB |
Output is correct |
24 |
Correct |
2 ms |
31068 KB |
Output is correct |
25 |
Correct |
4 ms |
43612 KB |
Output is correct |
26 |
Correct |
4 ms |
43612 KB |
Output is correct |
27 |
Correct |
4 ms |
41520 KB |
Output is correct |
28 |
Correct |
4 ms |
43608 KB |
Output is correct |
29 |
Correct |
4 ms |
43608 KB |
Output is correct |
30 |
Correct |
3 ms |
41308 KB |
Output is correct |
31 |
Correct |
3 ms |
39260 KB |
Output is correct |
32 |
Correct |
3 ms |
41308 KB |
Output is correct |
33 |
Correct |
4 ms |
41304 KB |
Output is correct |
34 |
Correct |
3 ms |
41308 KB |
Output is correct |
35 |
Correct |
4 ms |
43612 KB |
Output is correct |
36 |
Correct |
4 ms |
43612 KB |
Output is correct |
37 |
Correct |
3 ms |
43612 KB |
Output is correct |
38 |
Correct |
3 ms |
41308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
14796 KB |
Output is correct |
4 |
Correct |
2 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
1 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
26972 KB |
Output is correct |
9 |
Correct |
2 ms |
24924 KB |
Output is correct |
10 |
Correct |
3 ms |
26972 KB |
Output is correct |
11 |
Correct |
2 ms |
26972 KB |
Output is correct |
12 |
Correct |
2 ms |
24924 KB |
Output is correct |
13 |
Correct |
2 ms |
24924 KB |
Output is correct |
14 |
Correct |
2 ms |
26972 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
24924 KB |
Output is correct |
17 |
Correct |
2 ms |
26972 KB |
Output is correct |
18 |
Correct |
2 ms |
26972 KB |
Output is correct |
19 |
Correct |
2 ms |
26972 KB |
Output is correct |
20 |
Correct |
2 ms |
24924 KB |
Output is correct |
21 |
Correct |
2 ms |
26972 KB |
Output is correct |
22 |
Correct |
2 ms |
26972 KB |
Output is correct |
23 |
Correct |
3 ms |
26972 KB |
Output is correct |
24 |
Correct |
2 ms |
31068 KB |
Output is correct |
25 |
Correct |
4 ms |
43612 KB |
Output is correct |
26 |
Correct |
4 ms |
43612 KB |
Output is correct |
27 |
Correct |
4 ms |
41520 KB |
Output is correct |
28 |
Correct |
4 ms |
43608 KB |
Output is correct |
29 |
Correct |
4 ms |
43608 KB |
Output is correct |
30 |
Correct |
3 ms |
41308 KB |
Output is correct |
31 |
Correct |
3 ms |
39260 KB |
Output is correct |
32 |
Correct |
3 ms |
41308 KB |
Output is correct |
33 |
Correct |
4 ms |
41304 KB |
Output is correct |
34 |
Correct |
3 ms |
41308 KB |
Output is correct |
35 |
Correct |
4 ms |
43612 KB |
Output is correct |
36 |
Correct |
4 ms |
43612 KB |
Output is correct |
37 |
Correct |
3 ms |
43612 KB |
Output is correct |
38 |
Correct |
3 ms |
41308 KB |
Output is correct |
39 |
Correct |
17 ms |
65372 KB |
Output is correct |
40 |
Correct |
16 ms |
65548 KB |
Output is correct |
41 |
Correct |
17 ms |
65576 KB |
Output is correct |
42 |
Correct |
13 ms |
62556 KB |
Output is correct |
43 |
Correct |
13 ms |
60508 KB |
Output is correct |
44 |
Correct |
15 ms |
62804 KB |
Output is correct |
45 |
Correct |
13 ms |
62568 KB |
Output is correct |
46 |
Correct |
14 ms |
62556 KB |
Output is correct |
47 |
Correct |
16 ms |
65560 KB |
Output is correct |
48 |
Correct |
16 ms |
65596 KB |
Output is correct |
49 |
Correct |
12 ms |
65372 KB |
Output is correct |
50 |
Correct |
13 ms |
62724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
16984 KB |
Output is correct |
3 |
Correct |
2 ms |
24924 KB |
Output is correct |
4 |
Correct |
2 ms |
23000 KB |
Output is correct |
5 |
Correct |
2 ms |
24924 KB |
Output is correct |
6 |
Correct |
2 ms |
26968 KB |
Output is correct |
7 |
Correct |
2 ms |
26972 KB |
Output is correct |
8 |
Correct |
2 ms |
26972 KB |
Output is correct |
9 |
Correct |
3 ms |
41308 KB |
Output is correct |
10 |
Correct |
4 ms |
43608 KB |
Output is correct |
11 |
Correct |
4 ms |
41308 KB |
Output is correct |
12 |
Correct |
4 ms |
39260 KB |
Output is correct |
13 |
Correct |
4 ms |
41308 KB |
Output is correct |
14 |
Correct |
4 ms |
43612 KB |
Output is correct |
15 |
Correct |
16 ms |
65552 KB |
Output is correct |
16 |
Correct |
14 ms |
62556 KB |
Output is correct |
17 |
Correct |
12 ms |
60504 KB |
Output is correct |
18 |
Correct |
13 ms |
62744 KB |
Output is correct |
19 |
Correct |
16 ms |
65564 KB |
Output is correct |
20 |
Correct |
62 ms |
72276 KB |
Output is correct |
21 |
Correct |
1186 ms |
352336 KB |
Output is correct |
22 |
Correct |
1205 ms |
356176 KB |
Output is correct |
23 |
Correct |
787 ms |
235536 KB |
Output is correct |
24 |
Correct |
1144 ms |
356180 KB |
Output is correct |
25 |
Correct |
694 ms |
213652 KB |
Output is correct |
26 |
Correct |
789 ms |
236508 KB |
Output is correct |
27 |
Correct |
804 ms |
236376 KB |
Output is correct |
28 |
Correct |
1126 ms |
351424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
20828 KB |
Output is correct |
4 |
Correct |
2 ms |
24924 KB |
Output is correct |
5 |
Correct |
2 ms |
26972 KB |
Output is correct |
6 |
Correct |
2 ms |
26972 KB |
Output is correct |
7 |
Correct |
2 ms |
26972 KB |
Output is correct |
8 |
Correct |
4 ms |
43612 KB |
Output is correct |
9 |
Correct |
3 ms |
41308 KB |
Output is correct |
10 |
Correct |
4 ms |
43612 KB |
Output is correct |
11 |
Correct |
3 ms |
41308 KB |
Output is correct |
12 |
Correct |
17 ms |
65372 KB |
Output is correct |
13 |
Correct |
13 ms |
62552 KB |
Output is correct |
14 |
Correct |
13 ms |
62556 KB |
Output is correct |
15 |
Correct |
61 ms |
72296 KB |
Output is correct |
16 |
Correct |
772 ms |
231756 KB |
Output is correct |
17 |
Correct |
1184 ms |
356180 KB |
Output is correct |
18 |
Correct |
1173 ms |
356080 KB |
Output is correct |
19 |
Correct |
1107 ms |
356180 KB |
Output is correct |
20 |
Correct |
809 ms |
233100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
14796 KB |
Output is correct |
4 |
Correct |
2 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
1 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
26972 KB |
Output is correct |
9 |
Correct |
2 ms |
24924 KB |
Output is correct |
10 |
Correct |
3 ms |
26972 KB |
Output is correct |
11 |
Correct |
2 ms |
26972 KB |
Output is correct |
12 |
Correct |
2 ms |
24924 KB |
Output is correct |
13 |
Correct |
2 ms |
24924 KB |
Output is correct |
14 |
Correct |
2 ms |
26972 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
24924 KB |
Output is correct |
17 |
Correct |
2 ms |
26972 KB |
Output is correct |
18 |
Correct |
2 ms |
26972 KB |
Output is correct |
19 |
Correct |
2 ms |
26972 KB |
Output is correct |
20 |
Correct |
2 ms |
24924 KB |
Output is correct |
21 |
Correct |
2 ms |
26972 KB |
Output is correct |
22 |
Correct |
2 ms |
26972 KB |
Output is correct |
23 |
Correct |
3 ms |
26972 KB |
Output is correct |
24 |
Correct |
2 ms |
31068 KB |
Output is correct |
25 |
Correct |
4 ms |
43612 KB |
Output is correct |
26 |
Correct |
4 ms |
43612 KB |
Output is correct |
27 |
Correct |
4 ms |
41520 KB |
Output is correct |
28 |
Correct |
4 ms |
43608 KB |
Output is correct |
29 |
Correct |
4 ms |
43608 KB |
Output is correct |
30 |
Correct |
3 ms |
41308 KB |
Output is correct |
31 |
Correct |
3 ms |
39260 KB |
Output is correct |
32 |
Correct |
3 ms |
41308 KB |
Output is correct |
33 |
Correct |
4 ms |
41304 KB |
Output is correct |
34 |
Correct |
3 ms |
41308 KB |
Output is correct |
35 |
Correct |
4 ms |
43612 KB |
Output is correct |
36 |
Correct |
4 ms |
43612 KB |
Output is correct |
37 |
Correct |
3 ms |
43612 KB |
Output is correct |
38 |
Correct |
3 ms |
41308 KB |
Output is correct |
39 |
Correct |
17 ms |
65372 KB |
Output is correct |
40 |
Correct |
16 ms |
65548 KB |
Output is correct |
41 |
Correct |
17 ms |
65576 KB |
Output is correct |
42 |
Correct |
13 ms |
62556 KB |
Output is correct |
43 |
Correct |
13 ms |
60508 KB |
Output is correct |
44 |
Correct |
15 ms |
62804 KB |
Output is correct |
45 |
Correct |
13 ms |
62568 KB |
Output is correct |
46 |
Correct |
14 ms |
62556 KB |
Output is correct |
47 |
Correct |
16 ms |
65560 KB |
Output is correct |
48 |
Correct |
16 ms |
65596 KB |
Output is correct |
49 |
Correct |
12 ms |
65372 KB |
Output is correct |
50 |
Correct |
13 ms |
62724 KB |
Output is correct |
51 |
Correct |
1 ms |
10588 KB |
Output is correct |
52 |
Correct |
1 ms |
16984 KB |
Output is correct |
53 |
Correct |
2 ms |
24924 KB |
Output is correct |
54 |
Correct |
2 ms |
23000 KB |
Output is correct |
55 |
Correct |
2 ms |
24924 KB |
Output is correct |
56 |
Correct |
2 ms |
26968 KB |
Output is correct |
57 |
Correct |
2 ms |
26972 KB |
Output is correct |
58 |
Correct |
2 ms |
26972 KB |
Output is correct |
59 |
Correct |
3 ms |
41308 KB |
Output is correct |
60 |
Correct |
4 ms |
43608 KB |
Output is correct |
61 |
Correct |
4 ms |
41308 KB |
Output is correct |
62 |
Correct |
4 ms |
39260 KB |
Output is correct |
63 |
Correct |
4 ms |
41308 KB |
Output is correct |
64 |
Correct |
4 ms |
43612 KB |
Output is correct |
65 |
Correct |
16 ms |
65552 KB |
Output is correct |
66 |
Correct |
14 ms |
62556 KB |
Output is correct |
67 |
Correct |
12 ms |
60504 KB |
Output is correct |
68 |
Correct |
13 ms |
62744 KB |
Output is correct |
69 |
Correct |
16 ms |
65564 KB |
Output is correct |
70 |
Correct |
62 ms |
72276 KB |
Output is correct |
71 |
Correct |
1186 ms |
352336 KB |
Output is correct |
72 |
Correct |
1205 ms |
356176 KB |
Output is correct |
73 |
Correct |
787 ms |
235536 KB |
Output is correct |
74 |
Correct |
1144 ms |
356180 KB |
Output is correct |
75 |
Correct |
694 ms |
213652 KB |
Output is correct |
76 |
Correct |
789 ms |
236508 KB |
Output is correct |
77 |
Correct |
804 ms |
236376 KB |
Output is correct |
78 |
Correct |
1126 ms |
351424 KB |
Output is correct |
79 |
Correct |
1 ms |
10588 KB |
Output is correct |
80 |
Correct |
2 ms |
16988 KB |
Output is correct |
81 |
Correct |
2 ms |
20828 KB |
Output is correct |
82 |
Correct |
2 ms |
24924 KB |
Output is correct |
83 |
Correct |
2 ms |
26972 KB |
Output is correct |
84 |
Correct |
2 ms |
26972 KB |
Output is correct |
85 |
Correct |
2 ms |
26972 KB |
Output is correct |
86 |
Correct |
4 ms |
43612 KB |
Output is correct |
87 |
Correct |
3 ms |
41308 KB |
Output is correct |
88 |
Correct |
4 ms |
43612 KB |
Output is correct |
89 |
Correct |
3 ms |
41308 KB |
Output is correct |
90 |
Correct |
17 ms |
65372 KB |
Output is correct |
91 |
Correct |
13 ms |
62552 KB |
Output is correct |
92 |
Correct |
13 ms |
62556 KB |
Output is correct |
93 |
Correct |
61 ms |
72296 KB |
Output is correct |
94 |
Correct |
772 ms |
231756 KB |
Output is correct |
95 |
Correct |
1184 ms |
356180 KB |
Output is correct |
96 |
Correct |
1173 ms |
356080 KB |
Output is correct |
97 |
Correct |
1107 ms |
356180 KB |
Output is correct |
98 |
Correct |
809 ms |
233100 KB |
Output is correct |
99 |
Correct |
1184 ms |
356176 KB |
Output is correct |
100 |
Correct |
1357 ms |
356216 KB |
Output is correct |
101 |
Correct |
1217 ms |
356176 KB |
Output is correct |
102 |
Correct |
1225 ms |
356184 KB |
Output is correct |
103 |
Correct |
908 ms |
234064 KB |
Output is correct |
104 |
Correct |
1268 ms |
346772 KB |
Output is correct |
105 |
Correct |
517 ms |
354900 KB |
Output is correct |