This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
/*
int out = n + 1;
for (int i = 0; i < n; i++){
int j = i, cnt = 1, rr = all[i].ff + m - 1;
while (j != -1 && all[j].ss < rr){
j = g[j];
cnt++;
}
if (j != -1) out = min(out, cnt);
}
cout<<((out > n) ? -1 : out)<<"\n";
*/
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);
}
}
while (j != -1 && all[j].ss < rr){
j++; out++;
}
ans = min(ans, out);
}
cout<<((ans > n) ? -1 : ans)<<"\n";
}
Compilation message (stderr)
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){
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |