#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int L = 20;
ll n, m;
vector<ll> s, e;
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n >> m;
s.resize(2 * n + 1);
e.resize(2 * n + 1);
map<ll, pair<ll, ll>> f;
for (int i = 1; i <= n; i += 1) {
cin >> s[i] >> e[i];
if (s[i] > e[i]) {
e[i] += m;
}
f[s[i]] = max(f[s[i]], {e[i], i});
f[e[i]] = max(f[e[i]], {-1, -1});
}
for (int i = n + 1; i <= 2 * n; i += 1) {
s[i] = s[i - n] + m;
e[i] = e[i - n] + m;
f[s[i]] = max(f[s[i]], {e[i], i});
f[e[i]] = max(f[e[i]], {-1, -1});
}
vector<pair<ll, pair<ll, ll>>> vals;
for (auto it : f) {
vals.push_back({it.first, it.second});
}
map<ll, ll> where;
vector<vector<pair<ll, ll>>> rmq(L, vector<pair<ll, ll>>(vals.size() + 1));
for (int i = 1; i <= vals.size(); i += 1) {
rmq[0][i] = vals[i - 1].second;
where[vals[i - 1].first] = i;
}
for (int h = 1; h < L; h += 1) {
for (int i = 1; i + (1 << h) - 1 <= vals.size(); i += 1) {
rmq[h][i] = max(rmq[h - 1][i], rmq[h - 1][i + (1 << (h - 1))]);
}
}
vector<int> lg(vals.size() + 1);
for (int i = 2; i <= vals.size(); i += 1) {
lg[i] = lg[i / 2] + 1;
}
auto query = [&](int l, int r) {
l = where[l];
r = where[r];
int h = lg[r - l + 1];
return max(rmq[h][l], rmq[h][r - (1 << h) + 1]);
};
vector<vector<int>> nxt(L, vector<int>(2 * n + 1));
for (int i = 1; i <= 2 * n; i += 1) {
nxt[0][i] = query(s[i], e[i]).second;
}
for (int h = 1; h < L; h += 1) {
for (int i = 1; i <= 2 * n; i += 1) {
nxt[h][i] = nxt[h - 1][nxt[h - 1][i]];
}
}
int ans = n + 1;
for (int i = 1; i <= n; i += 1) {
int cur = 1;
int x = i;
for (int h = L - 1; h >= 0; h -= 1) {
if (e[nxt[h][x]] < s[i] + m) {
cur += (1 << h);
x = nxt[h][x];
}
}
x = nxt[0][x];
cur += 1;
if (e[x] >= s[i] + m) {
ans = min(ans, cur);
}
}
if (ans == n + 1) {
cout << -1 << "\n";
return 0;
}
cout << ans << "\n";
return 0;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 1; i <= vals.size(); i += 1) {
| ~~^~~~~~~~~~~~~~
Main.cpp:55:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 1; i + (1 << h) - 1 <= vals.size(); i += 1) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
Main.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 2; i <= vals.size(); i += 1) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
972 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
972 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
16 ms |
10708 KB |
Output is correct |
40 |
Correct |
15 ms |
10704 KB |
Output is correct |
41 |
Correct |
15 ms |
10704 KB |
Output is correct |
42 |
Correct |
9 ms |
6104 KB |
Output is correct |
43 |
Correct |
9 ms |
5152 KB |
Output is correct |
44 |
Correct |
11 ms |
6216 KB |
Output is correct |
45 |
Correct |
10 ms |
5972 KB |
Output is correct |
46 |
Correct |
10 ms |
6232 KB |
Output is correct |
47 |
Correct |
15 ms |
10708 KB |
Output is correct |
48 |
Correct |
15 ms |
10572 KB |
Output is correct |
49 |
Correct |
11 ms |
10708 KB |
Output is correct |
50 |
Correct |
11 ms |
5980 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 |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
16 ms |
10576 KB |
Output is correct |
16 |
Correct |
10 ms |
5980 KB |
Output is correct |
17 |
Correct |
9 ms |
5152 KB |
Output is correct |
18 |
Correct |
12 ms |
6220 KB |
Output is correct |
19 |
Correct |
15 ms |
10704 KB |
Output is correct |
20 |
Correct |
58 ms |
40328 KB |
Output is correct |
21 |
Correct |
1320 ms |
415900 KB |
Output is correct |
22 |
Correct |
1299 ms |
415908 KB |
Output is correct |
23 |
Correct |
729 ms |
228672 KB |
Output is correct |
24 |
Correct |
1123 ms |
415900 KB |
Output is correct |
25 |
Correct |
653 ms |
191652 KB |
Output is correct |
26 |
Correct |
755 ms |
229680 KB |
Output is correct |
27 |
Correct |
720 ms |
229684 KB |
Output is correct |
28 |
Correct |
1000 ms |
406636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
19 ms |
10708 KB |
Output is correct |
13 |
Correct |
12 ms |
6168 KB |
Output is correct |
14 |
Correct |
11 ms |
5980 KB |
Output is correct |
15 |
Correct |
55 ms |
40248 KB |
Output is correct |
16 |
Correct |
761 ms |
228656 KB |
Output is correct |
17 |
Correct |
1237 ms |
415900 KB |
Output is correct |
18 |
Correct |
1411 ms |
415908 KB |
Output is correct |
19 |
Correct |
1038 ms |
415840 KB |
Output is correct |
20 |
Correct |
778 ms |
228652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
972 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
16 ms |
10708 KB |
Output is correct |
40 |
Correct |
15 ms |
10704 KB |
Output is correct |
41 |
Correct |
15 ms |
10704 KB |
Output is correct |
42 |
Correct |
9 ms |
6104 KB |
Output is correct |
43 |
Correct |
9 ms |
5152 KB |
Output is correct |
44 |
Correct |
11 ms |
6216 KB |
Output is correct |
45 |
Correct |
10 ms |
5972 KB |
Output is correct |
46 |
Correct |
10 ms |
6232 KB |
Output is correct |
47 |
Correct |
15 ms |
10708 KB |
Output is correct |
48 |
Correct |
15 ms |
10572 KB |
Output is correct |
49 |
Correct |
11 ms |
10708 KB |
Output is correct |
50 |
Correct |
11 ms |
5980 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 |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 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 |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
860 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
856 KB |
Output is correct |
65 |
Correct |
16 ms |
10576 KB |
Output is correct |
66 |
Correct |
10 ms |
5980 KB |
Output is correct |
67 |
Correct |
9 ms |
5152 KB |
Output is correct |
68 |
Correct |
12 ms |
6220 KB |
Output is correct |
69 |
Correct |
15 ms |
10704 KB |
Output is correct |
70 |
Correct |
58 ms |
40328 KB |
Output is correct |
71 |
Correct |
1320 ms |
415900 KB |
Output is correct |
72 |
Correct |
1299 ms |
415908 KB |
Output is correct |
73 |
Correct |
729 ms |
228672 KB |
Output is correct |
74 |
Correct |
1123 ms |
415900 KB |
Output is correct |
75 |
Correct |
653 ms |
191652 KB |
Output is correct |
76 |
Correct |
755 ms |
229680 KB |
Output is correct |
77 |
Correct |
720 ms |
229684 KB |
Output is correct |
78 |
Correct |
1000 ms |
406636 KB |
Output is correct |
79 |
Correct |
0 ms |
344 KB |
Output is correct |
80 |
Correct |
0 ms |
344 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 |
860 KB |
Output is correct |
87 |
Correct |
1 ms |
604 KB |
Output is correct |
88 |
Correct |
1 ms |
860 KB |
Output is correct |
89 |
Correct |
1 ms |
604 KB |
Output is correct |
90 |
Correct |
19 ms |
10708 KB |
Output is correct |
91 |
Correct |
12 ms |
6168 KB |
Output is correct |
92 |
Correct |
11 ms |
5980 KB |
Output is correct |
93 |
Correct |
55 ms |
40248 KB |
Output is correct |
94 |
Correct |
761 ms |
228656 KB |
Output is correct |
95 |
Correct |
1237 ms |
415900 KB |
Output is correct |
96 |
Correct |
1411 ms |
415908 KB |
Output is correct |
97 |
Correct |
1038 ms |
415840 KB |
Output is correct |
98 |
Correct |
778 ms |
228652 KB |
Output is correct |
99 |
Correct |
1143 ms |
415908 KB |
Output is correct |
100 |
Correct |
1162 ms |
415908 KB |
Output is correct |
101 |
Correct |
1060 ms |
415812 KB |
Output is correct |
102 |
Correct |
1072 ms |
415916 KB |
Output is correct |
103 |
Correct |
724 ms |
229608 KB |
Output is correct |
104 |
Correct |
1056 ms |
397192 KB |
Output is correct |
105 |
Correct |
641 ms |
414700 KB |
Output is correct |