#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 p = i, len = 1;
while ( v[p].r < v[i].l + m && p != nxt[0][p] ) {
p = nxt[0][p];
len++;
}
if ( v[p].r >= v[i].l + m )
minLen = min( minLen, len );
}
/*for ( int i = 0; i < n; i++ ) {
int len = 0, 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 << p);
}
}
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 |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
1 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 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 |
2 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 |
26968 KB |
Output is correct |
23 |
Correct |
2 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 |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
1 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 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 |
2 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 |
26968 KB |
Output is correct |
23 |
Correct |
2 ms |
26972 KB |
Output is correct |
24 |
Correct |
3 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 |
41524 KB |
Output is correct |
28 |
Correct |
4 ms |
43560 KB |
Output is correct |
29 |
Correct |
4 ms |
43612 KB |
Output is correct |
30 |
Correct |
3 ms |
41308 KB |
Output is correct |
31 |
Correct |
3 ms |
39404 KB |
Output is correct |
32 |
Correct |
4 ms |
41536 KB |
Output is correct |
33 |
Correct |
4 ms |
41308 KB |
Output is correct |
34 |
Correct |
4 ms |
41408 KB |
Output is correct |
35 |
Correct |
4 ms |
39516 KB |
Output is correct |
36 |
Correct |
3 ms |
41408 KB |
Output is correct |
37 |
Correct |
3 ms |
41564 KB |
Output is correct |
38 |
Correct |
3 ms |
41304 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 |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
1 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 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 |
2 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 |
26968 KB |
Output is correct |
23 |
Correct |
2 ms |
26972 KB |
Output is correct |
24 |
Correct |
3 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 |
41524 KB |
Output is correct |
28 |
Correct |
4 ms |
43560 KB |
Output is correct |
29 |
Correct |
4 ms |
43612 KB |
Output is correct |
30 |
Correct |
3 ms |
41308 KB |
Output is correct |
31 |
Correct |
3 ms |
39404 KB |
Output is correct |
32 |
Correct |
4 ms |
41536 KB |
Output is correct |
33 |
Correct |
4 ms |
41308 KB |
Output is correct |
34 |
Correct |
4 ms |
41408 KB |
Output is correct |
35 |
Correct |
4 ms |
39516 KB |
Output is correct |
36 |
Correct |
3 ms |
41408 KB |
Output is correct |
37 |
Correct |
3 ms |
41564 KB |
Output is correct |
38 |
Correct |
3 ms |
41304 KB |
Output is correct |
39 |
Correct |
17 ms |
49756 KB |
Output is correct |
40 |
Correct |
92 ms |
47964 KB |
Output is correct |
41 |
Correct |
17 ms |
47964 KB |
Output is correct |
42 |
Correct |
118 ms |
46940 KB |
Output is correct |
43 |
Correct |
12 ms |
44888 KB |
Output is correct |
44 |
Correct |
118 ms |
45156 KB |
Output is correct |
45 |
Correct |
119 ms |
47132 KB |
Output is correct |
46 |
Correct |
117 ms |
47140 KB |
Output is correct |
47 |
Correct |
15 ms |
49756 KB |
Output is correct |
48 |
Correct |
15 ms |
45964 KB |
Output is correct |
49 |
Correct |
25 ms |
45916 KB |
Output is correct |
50 |
Correct |
32 ms |
46940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
2 ms |
24924 KB |
Output is correct |
4 |
Correct |
2 ms |
22876 KB |
Output is correct |
5 |
Correct |
2 ms |
24924 KB |
Output is correct |
6 |
Correct |
2 ms |
26972 KB |
Output is correct |
7 |
Correct |
2 ms |
26972 KB |
Output is correct |
8 |
Correct |
2 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 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 |
3 ms |
39472 KB |
Output is correct |
13 |
Correct |
3 ms |
41308 KB |
Output is correct |
14 |
Correct |
4 ms |
43612 KB |
Output is correct |
15 |
Correct |
17 ms |
53848 KB |
Output is correct |
16 |
Correct |
119 ms |
52988 KB |
Output is correct |
17 |
Correct |
12 ms |
52568 KB |
Output is correct |
18 |
Correct |
121 ms |
52828 KB |
Output is correct |
19 |
Correct |
15 ms |
53848 KB |
Output is correct |
20 |
Correct |
55 ms |
73040 KB |
Output is correct |
21 |
Execution timed out |
2059 ms |
356180 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
16732 KB |
Output is correct |
3 |
Correct |
2 ms |
20828 KB |
Output is correct |
4 |
Correct |
2 ms |
24924 KB |
Output is correct |
5 |
Correct |
3 ms |
26968 KB |
Output is correct |
6 |
Correct |
2 ms |
26972 KB |
Output is correct |
7 |
Correct |
3 ms |
26968 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 |
41404 KB |
Output is correct |
12 |
Correct |
16 ms |
49756 KB |
Output is correct |
13 |
Correct |
117 ms |
51028 KB |
Output is correct |
14 |
Correct |
28 ms |
48988 KB |
Output is correct |
15 |
Correct |
55 ms |
72912 KB |
Output is correct |
16 |
Execution timed out |
2037 ms |
215632 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
18780 KB |
Output is correct |
5 |
Correct |
1 ms |
18780 KB |
Output is correct |
6 |
Correct |
1 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 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 |
2 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 |
26968 KB |
Output is correct |
23 |
Correct |
2 ms |
26972 KB |
Output is correct |
24 |
Correct |
3 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 |
41524 KB |
Output is correct |
28 |
Correct |
4 ms |
43560 KB |
Output is correct |
29 |
Correct |
4 ms |
43612 KB |
Output is correct |
30 |
Correct |
3 ms |
41308 KB |
Output is correct |
31 |
Correct |
3 ms |
39404 KB |
Output is correct |
32 |
Correct |
4 ms |
41536 KB |
Output is correct |
33 |
Correct |
4 ms |
41308 KB |
Output is correct |
34 |
Correct |
4 ms |
41408 KB |
Output is correct |
35 |
Correct |
4 ms |
39516 KB |
Output is correct |
36 |
Correct |
3 ms |
41408 KB |
Output is correct |
37 |
Correct |
3 ms |
41564 KB |
Output is correct |
38 |
Correct |
3 ms |
41304 KB |
Output is correct |
39 |
Correct |
17 ms |
49756 KB |
Output is correct |
40 |
Correct |
92 ms |
47964 KB |
Output is correct |
41 |
Correct |
17 ms |
47964 KB |
Output is correct |
42 |
Correct |
118 ms |
46940 KB |
Output is correct |
43 |
Correct |
12 ms |
44888 KB |
Output is correct |
44 |
Correct |
118 ms |
45156 KB |
Output is correct |
45 |
Correct |
119 ms |
47132 KB |
Output is correct |
46 |
Correct |
117 ms |
47140 KB |
Output is correct |
47 |
Correct |
15 ms |
49756 KB |
Output is correct |
48 |
Correct |
15 ms |
45964 KB |
Output is correct |
49 |
Correct |
25 ms |
45916 KB |
Output is correct |
50 |
Correct |
32 ms |
46940 KB |
Output is correct |
51 |
Correct |
1 ms |
10584 KB |
Output is correct |
52 |
Correct |
2 ms |
16732 KB |
Output is correct |
53 |
Correct |
2 ms |
24924 KB |
Output is correct |
54 |
Correct |
2 ms |
22876 KB |
Output is correct |
55 |
Correct |
2 ms |
24924 KB |
Output is correct |
56 |
Correct |
2 ms |
26972 KB |
Output is correct |
57 |
Correct |
2 ms |
26972 KB |
Output is correct |
58 |
Correct |
2 ms |
26972 KB |
Output is correct |
59 |
Correct |
4 ms |
41308 KB |
Output is correct |
60 |
Correct |
4 ms |
43612 KB |
Output is correct |
61 |
Correct |
3 ms |
41308 KB |
Output is correct |
62 |
Correct |
3 ms |
39472 KB |
Output is correct |
63 |
Correct |
3 ms |
41308 KB |
Output is correct |
64 |
Correct |
4 ms |
43612 KB |
Output is correct |
65 |
Correct |
17 ms |
53848 KB |
Output is correct |
66 |
Correct |
119 ms |
52988 KB |
Output is correct |
67 |
Correct |
12 ms |
52568 KB |
Output is correct |
68 |
Correct |
121 ms |
52828 KB |
Output is correct |
69 |
Correct |
15 ms |
53848 KB |
Output is correct |
70 |
Correct |
55 ms |
73040 KB |
Output is correct |
71 |
Execution timed out |
2059 ms |
356180 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |