#include<bits/stdc++.h>
using namespace std;
#define rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 1e6;
const int inf = 1e9;
int lg[N], up[N][20];
void solve() {
int n, m;
cin >> n >> m;
int l[n + 1], r[n + 1];
vector<array<int, 3>> s;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
if (r[i] < l[i]) r[i] += m;
s.push_back({l[i], 0, i});
s.push_back({r[i], 1, i});
}
sort(all(s));
int mx = -1, vt;
for (auto [p, is, id]: s) {
if (!is) {
if (r[id] > mx) {
mx = r[id];
vt = id;
}
}else up[id][0] = vt;
}
for (int k = 1; k <= lg[n]; k++) {
for (int i = 1; i <= n; i++) {
up[i][k] = up[up[i][k - 1]][k - 1];
}
}
int mn = inf;
for (int i = 1; i <= n; i++) {
if (r[i] - l[i] + 1 >= m) {
mn = 1;
break;
}
int tl = l[i], v = i, ans = 2;
for (int k = lg[n]; k >= 0; k--) {
int tr = r[up[v][k]];
if (tr - tl + 1 >= m) continue;
v = up[v][k];
ans += (1 << k);
}
v = up[v][0];
if (r[v] - tl + 1 < m) continue;
mn = min(mn, ans);
}
if (mn > n) cout << -1;
else cout << mn;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 1; i < N; i++) lg[i] = lg[i / 2] + 1;
solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:39:19: warning: 'vt' may be used uninitialized in this function [-Wmaybe-uninitialized]
39 | }else up[id][0] = vt;
| ~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
Output is correct |
2 |
Correct |
2 ms |
4184 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
4 ms |
4236 KB |
Output is correct |
6 |
Correct |
4 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4188 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
4 ms |
4376 KB |
Output is correct |
12 |
Correct |
4 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
2 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
4 ms |
4380 KB |
Output is correct |
18 |
Correct |
4 ms |
4188 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4376 KB |
Output is correct |
21 |
Correct |
3 ms |
4376 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
Output is correct |
2 |
Correct |
2 ms |
4184 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
4 ms |
4236 KB |
Output is correct |
6 |
Correct |
4 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4188 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
4 ms |
4376 KB |
Output is correct |
12 |
Correct |
4 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
2 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
4 ms |
4380 KB |
Output is correct |
18 |
Correct |
4 ms |
4188 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4376 KB |
Output is correct |
21 |
Correct |
3 ms |
4376 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
3 ms |
4188 KB |
Output is correct |
26 |
Correct |
3 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4324 KB |
Output is correct |
28 |
Correct |
3 ms |
4188 KB |
Output is correct |
29 |
Correct |
2 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
4188 KB |
Output is correct |
31 |
Correct |
3 ms |
4376 KB |
Output is correct |
32 |
Correct |
3 ms |
4320 KB |
Output is correct |
33 |
Correct |
2 ms |
4188 KB |
Output is correct |
34 |
Correct |
2 ms |
4380 KB |
Output is correct |
35 |
Correct |
2 ms |
4188 KB |
Output is correct |
36 |
Correct |
2 ms |
4188 KB |
Output is correct |
37 |
Correct |
2 ms |
4284 KB |
Output is correct |
38 |
Correct |
2 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
Output is correct |
2 |
Correct |
2 ms |
4184 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
4 ms |
4236 KB |
Output is correct |
6 |
Correct |
4 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4188 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
4 ms |
4376 KB |
Output is correct |
12 |
Correct |
4 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
2 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
4 ms |
4380 KB |
Output is correct |
18 |
Correct |
4 ms |
4188 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4376 KB |
Output is correct |
21 |
Correct |
3 ms |
4376 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
3 ms |
4188 KB |
Output is correct |
26 |
Correct |
3 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4324 KB |
Output is correct |
28 |
Correct |
3 ms |
4188 KB |
Output is correct |
29 |
Correct |
2 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
4188 KB |
Output is correct |
31 |
Correct |
3 ms |
4376 KB |
Output is correct |
32 |
Correct |
3 ms |
4320 KB |
Output is correct |
33 |
Correct |
2 ms |
4188 KB |
Output is correct |
34 |
Correct |
2 ms |
4380 KB |
Output is correct |
35 |
Correct |
2 ms |
4188 KB |
Output is correct |
36 |
Correct |
2 ms |
4188 KB |
Output is correct |
37 |
Correct |
2 ms |
4284 KB |
Output is correct |
38 |
Correct |
2 ms |
4184 KB |
Output is correct |
39 |
Correct |
6 ms |
6936 KB |
Output is correct |
40 |
Correct |
4 ms |
6940 KB |
Output is correct |
41 |
Correct |
4 ms |
6940 KB |
Output is correct |
42 |
Correct |
5 ms |
6944 KB |
Output is correct |
43 |
Correct |
4 ms |
6940 KB |
Output is correct |
44 |
Correct |
4 ms |
6940 KB |
Output is correct |
45 |
Correct |
4 ms |
6936 KB |
Output is correct |
46 |
Correct |
5 ms |
6968 KB |
Output is correct |
47 |
Correct |
5 ms |
6940 KB |
Output is correct |
48 |
Correct |
4 ms |
6940 KB |
Output is correct |
49 |
Correct |
3 ms |
6936 KB |
Output is correct |
50 |
Correct |
3 ms |
6776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
3 ms |
4376 KB |
Output is correct |
4 |
Correct |
2 ms |
4328 KB |
Output is correct |
5 |
Correct |
3 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4184 KB |
Output is correct |
7 |
Correct |
4 ms |
4188 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
4 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
3 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
3 ms |
4188 KB |
Output is correct |
15 |
Correct |
5 ms |
6764 KB |
Output is correct |
16 |
Correct |
4 ms |
6880 KB |
Output is correct |
17 |
Correct |
5 ms |
6736 KB |
Output is correct |
18 |
Correct |
4 ms |
6940 KB |
Output is correct |
19 |
Correct |
4 ms |
6940 KB |
Output is correct |
20 |
Incorrect |
71 ms |
28132 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
3 ms |
4372 KB |
Output is correct |
4 |
Correct |
3 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4188 KB |
Output is correct |
6 |
Correct |
3 ms |
4184 KB |
Output is correct |
7 |
Correct |
2 ms |
4184 KB |
Output is correct |
8 |
Correct |
3 ms |
4184 KB |
Output is correct |
9 |
Correct |
3 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
5 ms |
6940 KB |
Output is correct |
13 |
Correct |
4 ms |
6940 KB |
Output is correct |
14 |
Correct |
5 ms |
6832 KB |
Output is correct |
15 |
Incorrect |
72 ms |
28084 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
Output is correct |
2 |
Correct |
2 ms |
4184 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
4 ms |
4236 KB |
Output is correct |
6 |
Correct |
4 ms |
4188 KB |
Output is correct |
7 |
Correct |
3 ms |
4188 KB |
Output is correct |
8 |
Correct |
3 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
4 ms |
4376 KB |
Output is correct |
12 |
Correct |
4 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
2 ms |
4188 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
4 ms |
4380 KB |
Output is correct |
18 |
Correct |
4 ms |
4188 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4376 KB |
Output is correct |
21 |
Correct |
3 ms |
4376 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
3 ms |
4188 KB |
Output is correct |
26 |
Correct |
3 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4324 KB |
Output is correct |
28 |
Correct |
3 ms |
4188 KB |
Output is correct |
29 |
Correct |
2 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
4188 KB |
Output is correct |
31 |
Correct |
3 ms |
4376 KB |
Output is correct |
32 |
Correct |
3 ms |
4320 KB |
Output is correct |
33 |
Correct |
2 ms |
4188 KB |
Output is correct |
34 |
Correct |
2 ms |
4380 KB |
Output is correct |
35 |
Correct |
2 ms |
4188 KB |
Output is correct |
36 |
Correct |
2 ms |
4188 KB |
Output is correct |
37 |
Correct |
2 ms |
4284 KB |
Output is correct |
38 |
Correct |
2 ms |
4184 KB |
Output is correct |
39 |
Correct |
6 ms |
6936 KB |
Output is correct |
40 |
Correct |
4 ms |
6940 KB |
Output is correct |
41 |
Correct |
4 ms |
6940 KB |
Output is correct |
42 |
Correct |
5 ms |
6944 KB |
Output is correct |
43 |
Correct |
4 ms |
6940 KB |
Output is correct |
44 |
Correct |
4 ms |
6940 KB |
Output is correct |
45 |
Correct |
4 ms |
6936 KB |
Output is correct |
46 |
Correct |
5 ms |
6968 KB |
Output is correct |
47 |
Correct |
5 ms |
6940 KB |
Output is correct |
48 |
Correct |
4 ms |
6940 KB |
Output is correct |
49 |
Correct |
3 ms |
6936 KB |
Output is correct |
50 |
Correct |
3 ms |
6776 KB |
Output is correct |
51 |
Correct |
2 ms |
4188 KB |
Output is correct |
52 |
Correct |
2 ms |
4188 KB |
Output is correct |
53 |
Correct |
3 ms |
4376 KB |
Output is correct |
54 |
Correct |
2 ms |
4328 KB |
Output is correct |
55 |
Correct |
3 ms |
4188 KB |
Output is correct |
56 |
Correct |
2 ms |
4184 KB |
Output is correct |
57 |
Correct |
4 ms |
4188 KB |
Output is correct |
58 |
Correct |
2 ms |
4188 KB |
Output is correct |
59 |
Correct |
2 ms |
4188 KB |
Output is correct |
60 |
Correct |
4 ms |
4188 KB |
Output is correct |
61 |
Correct |
2 ms |
4188 KB |
Output is correct |
62 |
Correct |
3 ms |
4188 KB |
Output is correct |
63 |
Correct |
2 ms |
4188 KB |
Output is correct |
64 |
Correct |
3 ms |
4188 KB |
Output is correct |
65 |
Correct |
5 ms |
6764 KB |
Output is correct |
66 |
Correct |
4 ms |
6880 KB |
Output is correct |
67 |
Correct |
5 ms |
6736 KB |
Output is correct |
68 |
Correct |
4 ms |
6940 KB |
Output is correct |
69 |
Correct |
4 ms |
6940 KB |
Output is correct |
70 |
Incorrect |
71 ms |
28132 KB |
Output isn't correct |
71 |
Halted |
0 ms |
0 KB |
- |