#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m; cin>>n>>m;
vector<int> l(n + 1), r(n + 1);
for (int i = 1; i <= n; i++){
cin>>l[i]>>r[i];
if (!r[i]) r[i] = m;
r[i]--;
if (l[i] > r[i]){
r[i] += m;
}
}
vector<pii> all;
for (int i = 1; i <= n; i++){
all.pb({l[i], r[i]});
}
sort(all.begin(), all.end());
vector<pii> tmp;
int i = 0;
while (i < n){
int j = i;
while (j < n && all[i].ff == all[j].ff){
j++;
}
tmp.pb({all[j - 1].ss, all[j - 1].ff});
i = j;
}
all = tmp;
sort(all.begin(), all.end());
tmp.clear();
i = 0;
while (i < all.size()){
int j = i;
while (j < all.size() && all[i].ff == all[j].ff){
j++;
}
tmp.pb({all[i].ss, all[i].ff});
i = j;
}
all = tmp;
sort(all.begin(), all.end());
n = (int) all.size();
vector<int> log(n + 1);
for (int i = 2; i <= n; i++){
log[i] = log[i / 2] + 1;
}
const int lg = log[n];
vector<vector<pii>> sp(n, vector<pii>(lg + 1));
for (int i = 0; i < n; i++){
sp[i][0] = {all[i].ss, i};
}
for (int k = 1; k <= lg; k++){
for (int i = 0; i + (1 << k) <= n; i++){
sp[i][k] = max(sp[i][k - 1], sp[i + (1 << (k - 1))][k - 1]);
}
}
auto get = [&](int l, int r){
int k = log[r - l + 1];
return max(sp[l][k], sp[r - (1 << k) + 1][k]);
};
vector<int> ls;
for (int i = 0; i < n; i++){
ls.pb(all[i].ff);
}
vector<int> :: iterator it1, it2;
vector<int> g(n);
for (int i = 0; i < n; i++){
auto &[l, r] = all[i];
it1 = lower_bound(ls.begin(), ls.end(), l);
it2 = upper_bound(ls.begin(), ls.end(), r + 1); it2--;
int lb = max(i + 1, (int) (it1 - ls.begin())), rb = (int) (it2 - ls.begin());
if (lb > rb){
g[i] = -1;
continue;
}
g[i] = get(lb, rb).ss;
if (all[g[i]].ss <= r) g[i] = -1;
}
vector<vector<int>> pw(n, vector<int>(lg + 1, -1));
for (int i = 0; i < n; i++){
pw[i][0] = g[i];
}
for (int k = 1; k <= lg; k++){
for (int i = 0; i + (1 << k) <= n; i++){
if (pw[i][k - 1] == -1) continue;
pw[i][k] = pw[pw[i][k - 1]][k - 1];
}
}
int ans = n + 1;
for (int i = 0; i < n; i++){
int rr = all[i].ff + m - 1, j = i, out = 1;
for (int k = lg; k >= 0; k--){
int v = pw[j][k];
if (v != -1 && all[v].ss <= rr){
j = v;
out += (1 << k);
}
}
while (j != -1 && all[j].ss < rr){
j = g[j]; out++;
}
if (j != -1) ans = min(ans, out);
}
cout<<((ans > n) ? -1 : ans)<<"\n";
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:45:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while (i < all.size()){
| ~~^~~~~~~~~~~~
Main.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | while (j < all.size() && all[i].ff == all[j].ff){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
1628 KB |
Output is correct |
40 |
Correct |
3 ms |
1628 KB |
Output is correct |
41 |
Correct |
3 ms |
1628 KB |
Output is correct |
42 |
Correct |
3 ms |
1628 KB |
Output is correct |
43 |
Correct |
3 ms |
1116 KB |
Output is correct |
44 |
Correct |
3 ms |
1624 KB |
Output is correct |
45 |
Correct |
3 ms |
1628 KB |
Output is correct |
46 |
Correct |
3 ms |
1524 KB |
Output is correct |
47 |
Correct |
3 ms |
1624 KB |
Output is correct |
48 |
Correct |
3 ms |
1628 KB |
Output is correct |
49 |
Correct |
3 ms |
1624 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
1628 KB |
Output is correct |
16 |
Correct |
3 ms |
1628 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
4 ms |
1628 KB |
Output is correct |
19 |
Correct |
3 ms |
1628 KB |
Output is correct |
20 |
Correct |
23 ms |
4212 KB |
Output is correct |
21 |
Correct |
152 ms |
63768 KB |
Output is correct |
22 |
Correct |
159 ms |
63752 KB |
Output is correct |
23 |
Correct |
139 ms |
63820 KB |
Output is correct |
24 |
Correct |
141 ms |
63816 KB |
Output is correct |
25 |
Correct |
117 ms |
38000 KB |
Output is correct |
26 |
Correct |
153 ms |
63908 KB |
Output is correct |
27 |
Correct |
150 ms |
63816 KB |
Output is correct |
28 |
Correct |
144 ms |
63820 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
3 ms |
1628 KB |
Output is correct |
13 |
Correct |
3 ms |
1628 KB |
Output is correct |
14 |
Correct |
3 ms |
1628 KB |
Output is correct |
15 |
Correct |
21 ms |
4212 KB |
Output is correct |
16 |
Correct |
143 ms |
63816 KB |
Output is correct |
17 |
Correct |
165 ms |
63816 KB |
Output is correct |
18 |
Correct |
129 ms |
63764 KB |
Output is correct |
19 |
Correct |
148 ms |
63816 KB |
Output is correct |
20 |
Correct |
133 ms |
63820 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
1628 KB |
Output is correct |
40 |
Correct |
3 ms |
1628 KB |
Output is correct |
41 |
Correct |
3 ms |
1628 KB |
Output is correct |
42 |
Correct |
3 ms |
1628 KB |
Output is correct |
43 |
Correct |
3 ms |
1116 KB |
Output is correct |
44 |
Correct |
3 ms |
1624 KB |
Output is correct |
45 |
Correct |
3 ms |
1628 KB |
Output is correct |
46 |
Correct |
3 ms |
1524 KB |
Output is correct |
47 |
Correct |
3 ms |
1624 KB |
Output is correct |
48 |
Correct |
3 ms |
1628 KB |
Output is correct |
49 |
Correct |
3 ms |
1624 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 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 |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
1628 KB |
Output is correct |
66 |
Correct |
3 ms |
1628 KB |
Output is correct |
67 |
Correct |
3 ms |
1116 KB |
Output is correct |
68 |
Correct |
4 ms |
1628 KB |
Output is correct |
69 |
Correct |
3 ms |
1628 KB |
Output is correct |
70 |
Correct |
23 ms |
4212 KB |
Output is correct |
71 |
Correct |
152 ms |
63768 KB |
Output is correct |
72 |
Correct |
159 ms |
63752 KB |
Output is correct |
73 |
Correct |
139 ms |
63820 KB |
Output is correct |
74 |
Correct |
141 ms |
63816 KB |
Output is correct |
75 |
Correct |
117 ms |
38000 KB |
Output is correct |
76 |
Correct |
153 ms |
63908 KB |
Output is correct |
77 |
Correct |
150 ms |
63816 KB |
Output is correct |
78 |
Correct |
144 ms |
63820 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 |
0 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
0 ms |
348 KB |
Output is correct |
90 |
Correct |
3 ms |
1628 KB |
Output is correct |
91 |
Correct |
3 ms |
1628 KB |
Output is correct |
92 |
Correct |
3 ms |
1628 KB |
Output is correct |
93 |
Correct |
21 ms |
4212 KB |
Output is correct |
94 |
Correct |
143 ms |
63816 KB |
Output is correct |
95 |
Correct |
165 ms |
63816 KB |
Output is correct |
96 |
Correct |
129 ms |
63764 KB |
Output is correct |
97 |
Correct |
148 ms |
63816 KB |
Output is correct |
98 |
Correct |
133 ms |
63820 KB |
Output is correct |
99 |
Correct |
147 ms |
63816 KB |
Output is correct |
100 |
Correct |
150 ms |
63760 KB |
Output is correct |
101 |
Correct |
140 ms |
64076 KB |
Output is correct |
102 |
Correct |
151 ms |
63820 KB |
Output is correct |
103 |
Correct |
151 ms |
63820 KB |
Output is correct |
104 |
Correct |
144 ms |
63724 KB |
Output is correct |
105 |
Correct |
155 ms |
63816 KB |
Output is correct |