#include <bits/stdc++.h>
/*
Checklist:
- Check statement:
- Check filename:
- Check test limit:
- Stresstest:
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 5e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int n, m;
pii a[N];
struct ST {
vector <pii> st;
ST (int _n) {
st.assign(4 * (_n + 5), make_pair(0, 0));
}
void build(int i, int l, int r) {
if (l == r) {
st[i] = make_pair(a[l].se, l);
return;
}
int mid = (l + r) / 2;
build(2 * i, l, mid);
build(2 * i + 1, mid + 1, r);
st[i] = st[2 * i];
if (st[i].fi < st[2 * i + 1].fi) {
st[i] = st[2 * i + 1];
}
else if (st[i].fi == st[2 * i + 1].fi) {
st[i].se = max(st[i].se, st[2 * i + 1].se);
}
}
pii get(int i, int l, int r, int u, int v) {
if (u > r || v < l) return make_pair(0, 0);
if (u <= l && r <= v) return st[i];
int mid = (l + r) / 2;
pii L = get(2 * i, l, mid, u, v);
pii R = get(2 * i + 1, mid + 1, r, u, v);
pii res = L;
if (res.fi < R.fi) {
res = R;
}
else if (res.fi == R.fi) {
res.se = max(res.se, R.se);
}
return res;
}
} St(N);
int up[N][lim + 1];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> m;
int sz = n;
for (int i = 1; i <= n; i++) {
cin >> a[i].fi >> a[i].se;
if (a[i].fi < a[i].se) {
a[++sz] = make_pair(a[i].fi + m, a[i].se + m);
}
else {
a[i].se += m;
}
}
sort(a + 1, a + sz + 1);
St.build(1, 1, sz);
for (int i = 1; i <= sz; i++) {
int L = lower_bound(a + 1, a + sz + 1, make_pair(a[i].fi, -inf)) - a;
int R = upper_bound(a + 1, a + sz + 1, make_pair(a[i].se, inf)) - a - 1;
pii val = St.get(1, 1, sz, L, R);
if (val.fi > a[i].se) up[i][0] = val.se;
// cout << i << ' ' << val.se << ' ' << val.fi << '\n';
}
for (int i = 1; i <= lim; i++) {
for (int j = 1; j <= sz; j++) {
up[j][i] = up[up[j][i - 1]][i - 1];
}
}
int ans = inf;
for (int i = 1; i <= sz; i++) {
if (a[i].fi >= m) break;
int tg = a[i].fi + m;
if (a[i].se >= tg) {
ans = min(ans, 1ll);
continue;
}
int cur = i, cnt = 1;
for (int k = lim; k >= 0; k--) {
int nxt = up[cur][k];
if (nxt != 0 && a[nxt].se < tg) {
cur = nxt;
cnt += (1ll << k);
}
}
int nxt = up[cur][0];
if (nxt != 0 && a[nxt].se >= tg) {
cnt++;
ans = min(ans, cnt);
}
}
cout << (ans < inf ? ans : -1);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~| # | 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... |