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;
#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 (stderr)
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;
| ~~~~~~~~~~^~~~
# | 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... |