#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vec vector
const int MXN = 200'005;
const int LOG = 20;
const int INF = 1e9;
int32_t main() {
int N, M;
cin >> N >> M;
vec<pair<int, int>> rs(N);
for(int i = 0; i<N; i++) {
cin >> rs[i].first >> rs[i].second;
if(rs[i].second < rs[i].first) rs[i].second += M;
}
sort(rs.begin(), rs.end());
set<pair<int, int>> rightmost;
vec<vec<pair<int, int>>> binjmps(N, vec<pair<int, int>>(LOG));
for(int i = 0; i<N; i++) {
cerr << rs[i].first << ' ' << rs[i].second << '\n';
}
for(int i = 0; i<N; i++) {
rs.push_back({rs[i].first+M, rs[i].second+M});
}
int j = 0;
for(int i = 0; i<N; i++) {
while(rs[j].first <= rs[i].second) {
rightmost.insert({rs[j].second, j});
cerr << "J: " << j;
j++;
}
pair<int, int> r = *rightmost.rbegin();
assert(r.first >= rs[i].second);
binjmps[i][0] = {r.first-rs[i].second, r.second%N};
cerr << binjmps[i][0].first << ", " << binjmps[i][0].second << ';';
rightmost.erase({rs[i].second, i});
}
cerr << '\n';
int mxlog = LOG;
for(int i = 1; i<LOG && i<mxlog; i++) {
for(int j = 0; j<N; j++) {
int k = binjmps[j][i-1].second;
binjmps[j][i].first = binjmps[j][i-1].first+binjmps[k][i-1].first;
binjmps[j][i].second = binjmps[k][i-1].second;
if(binjmps[j][i].first > M) {
mxlog = i+1;
}
}
}
for(int i = 0; i<mxlog; i++) {
cerr << binjmps[0][i].first << ' ';
}
cerr << '\n';
int ans = INF;
for(int i = 0; i<N; i++) {
int d = rs[i].second-rs[i].first;
int cur = i;
int res = 0;
for(int j = mxlog-1; j>=0; j--) {
int nd = binjmps[cur][j].first + d;
if(nd < M) {
d = nd;
cur = binjmps[cur][j].second;
res += (1<<j);
}
}
res += 1;
d += binjmps[cur][0].first;
if(d >= M) {
ans = min(ans, res+1);
}
}
if(ans == INF) cout << -1 << '\n';
else {
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
436 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 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 |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
436 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 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 |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
564 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
572 KB |
Output is correct |
29 |
Correct |
4 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
4 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
560 KB |
Output is correct |
33 |
Correct |
4 ms |
348 KB |
Output is correct |
34 |
Correct |
3 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
552 KB |
Output is correct |
36 |
Correct |
6 ms |
348 KB |
Output is correct |
37 |
Correct |
3 ms |
348 KB |
Output is correct |
38 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
436 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 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 |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
564 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
572 KB |
Output is correct |
29 |
Correct |
4 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
4 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
560 KB |
Output is correct |
33 |
Correct |
4 ms |
348 KB |
Output is correct |
34 |
Correct |
3 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
552 KB |
Output is correct |
36 |
Correct |
6 ms |
348 KB |
Output is correct |
37 |
Correct |
3 ms |
348 KB |
Output is correct |
38 |
Correct |
3 ms |
348 KB |
Output is correct |
39 |
Correct |
54 ms |
2648 KB |
Output is correct |
40 |
Correct |
62 ms |
2896 KB |
Output is correct |
41 |
Correct |
54 ms |
2644 KB |
Output is correct |
42 |
Correct |
52 ms |
2396 KB |
Output is correct |
43 |
Correct |
56 ms |
2884 KB |
Output is correct |
44 |
Correct |
67 ms |
2648 KB |
Output is correct |
45 |
Correct |
62 ms |
2648 KB |
Output is correct |
46 |
Correct |
55 ms |
2640 KB |
Output is correct |
47 |
Correct |
54 ms |
2708 KB |
Output is correct |
48 |
Correct |
55 ms |
2652 KB |
Output is correct |
49 |
Correct |
52 ms |
2396 KB |
Output is correct |
50 |
Correct |
52 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
436 KB |
Output is correct |
15 |
Correct |
54 ms |
2708 KB |
Output is correct |
16 |
Correct |
61 ms |
2824 KB |
Output is correct |
17 |
Correct |
62 ms |
2664 KB |
Output is correct |
18 |
Correct |
54 ms |
2652 KB |
Output is correct |
19 |
Correct |
55 ms |
2648 KB |
Output is correct |
20 |
Execution timed out |
2074 ms |
94508 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
59 ms |
2640 KB |
Output is correct |
13 |
Correct |
52 ms |
2600 KB |
Output is correct |
14 |
Correct |
56 ms |
2592 KB |
Output is correct |
15 |
Execution timed out |
2098 ms |
95000 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
436 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 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 |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
564 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
572 KB |
Output is correct |
29 |
Correct |
4 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
4 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
560 KB |
Output is correct |
33 |
Correct |
4 ms |
348 KB |
Output is correct |
34 |
Correct |
3 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
552 KB |
Output is correct |
36 |
Correct |
6 ms |
348 KB |
Output is correct |
37 |
Correct |
3 ms |
348 KB |
Output is correct |
38 |
Correct |
3 ms |
348 KB |
Output is correct |
39 |
Correct |
54 ms |
2648 KB |
Output is correct |
40 |
Correct |
62 ms |
2896 KB |
Output is correct |
41 |
Correct |
54 ms |
2644 KB |
Output is correct |
42 |
Correct |
52 ms |
2396 KB |
Output is correct |
43 |
Correct |
56 ms |
2884 KB |
Output is correct |
44 |
Correct |
67 ms |
2648 KB |
Output is correct |
45 |
Correct |
62 ms |
2648 KB |
Output is correct |
46 |
Correct |
55 ms |
2640 KB |
Output is correct |
47 |
Correct |
54 ms |
2708 KB |
Output is correct |
48 |
Correct |
55 ms |
2652 KB |
Output is correct |
49 |
Correct |
52 ms |
2396 KB |
Output is correct |
50 |
Correct |
52 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
3 ms |
348 KB |
Output is correct |
60 |
Correct |
4 ms |
348 KB |
Output is correct |
61 |
Correct |
3 ms |
348 KB |
Output is correct |
62 |
Correct |
4 ms |
348 KB |
Output is correct |
63 |
Correct |
3 ms |
348 KB |
Output is correct |
64 |
Correct |
3 ms |
436 KB |
Output is correct |
65 |
Correct |
54 ms |
2708 KB |
Output is correct |
66 |
Correct |
61 ms |
2824 KB |
Output is correct |
67 |
Correct |
62 ms |
2664 KB |
Output is correct |
68 |
Correct |
54 ms |
2652 KB |
Output is correct |
69 |
Correct |
55 ms |
2648 KB |
Output is correct |
70 |
Execution timed out |
2074 ms |
94508 KB |
Time limit exceeded |
71 |
Halted |
0 ms |
0 KB |
- |