#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;
else 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;
}
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) continue;
if (all[v].ss < rr){
j = v;
out += (1 << k);
}
}
if (all[j].ss >= rr) ans = min(ans, out);
else {
j = g[j];
if (j != -1 && all[j].ss >= rr){
ans = min(ans, out + 1);
}
}
}
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 |
Incorrect |
1 ms |
400 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
1 ms |
400 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
1 ms |
400 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
352 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
1624 KB |
Output is correct |
13 |
Correct |
3 ms |
1628 KB |
Output is correct |
14 |
Correct |
3 ms |
1628 KB |
Output is correct |
15 |
Incorrect |
23 ms |
4212 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
1 ms |
400 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |