#include "bits/stdc++.h"
using namespace std;
#define task ""
#define ll long long
#define endl '\n'
#define x first
#define y second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ep emplace_back
#define pb push_back
#define pf push_front
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
const ll oo = 1e16;
const int LOG = 20;
pii cor[N];
vector<int> arr;
int n, m, t;
int up[N][LOG];
ll cost[N][LOG];
struct node {
ll l, x;
node(ll l_ = -oo, ll x_ = 0) : l(l_), x(x_) {}
node operator+(const node &other) const {
if (l > other.l || (l == other.l && x > other.x)) return *this;
return other;
}
};
struct ST {
node st[N << 2];
void update(int id, int l, int r, int i, ll val, ll pos) {
if (l > i || r < i) return;
if (l == r) {
st[id] = st[id] + node(val, pos);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, i, val, pos);
update(id << 1 | 1, mid + 1, r, i, val, pos);
st[id] = st[id << 1] + st[id << 1 | 1];
}
node get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return node();
if (l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
} st;
int sad[N];
namespace FN {
void solve() {
sort(vall(arr));
arr.erase(unique(vall(arr)), arr.end());
for (int i = 1; i <= 2 * n; ++i) {
cor[i].x = lower_bound(vall(arr), cor[i].x) - arr.begin() + 1;
cor[i].y = lower_bound(vall(arr), cor[i].y) - arr.begin() + 1;
}
t = sze(arr);
for (int i = 1; i <= 2 * n; ++i) st.update(1, 1, t, cor[i].x, cor[i].y, i);
for (int i = 1; i <= 2 * n; ++i) {
node cur = st.get(1, 1, t, cor[i].x, cor[i].y);
ll l = cur.l;
ll x = cur.x;
if (l == cor[i].y) {
up[i][0] = i;
cost[i][0] = 0;
} else {
up[i][0] = x;
cost[i][0] = 1;
}
}
for (int i = 1; i < LOG; ++i) {
for (int j = 1; j <= 2 * n; ++j) {
up[j][i] = up[up[j][i - 1]][i - 1];
cost[j][i] = cost[j][i - 1] + cost[up[j][i - 1]][i - 1];
}
}
ll res = oo;
for (int t = 1; t <= 2 * n; ++t) {
int cur = lower_bound(vall(arr), sad[t] + m) - arr.begin() + 1;
int p = t;
ll ans = 1;
for (int i = LOG - 1; i >= 0; --i) {
if (cor[up[p][i]].y <= cur) {
ans += cost[p][i];
p = up[p][i];
}
}
if (cor[p].y >= cur) {
res = min(res, ans);
} else {
p = up[p][0];
if (cor[p].y >= cur) res = min(res, ans + 1);
}
}
cout << (res == oo ? -1 : res) << endl;
}
}
void input() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int l, r;
cin >> l >> r;
if (r < l) r += m;
cor[i].x = l;
cor[i].y = r;
sad[i] = l;
arr.pb(cor[i].x);
arr.pb(cor[i].y);
cor[i + n].x = l + m;
cor[i + n].y = r + m;
sad[i + n] = l + m;
arr.pb(cor[i + n].x);
arr.pb(cor[i + n].y);
arr.pb(cor[i + n].x + m);
}
return FN::solve();
}
int main() {
if(fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
input();
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen(task ".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... |