#include <bits/stdc++.h>
#define ll long long
#define task "FIRE"
using namespace std;
const int N = 1e6 + 16;
const ll INF = 1e18;
int n, m;
ll s[N], e[N];
namespace sub6 {
ll cnt, result = INF;
pair <ll, ll> a[N];
vector < pair <ll, ll> > checkLines;
bool cmp(pair <ll, ll> A, pair <ll, ll> B) {
if (A.first == B.first) return A.second > B.second;
return A.first < B.first;
}
const int LG = 21; // 2^21 > 1e6
static ll prefMax[N];
static ll starts[N];
static int up[LG][N];
// tìm last index với starts[idx] <= x (trả về 0..n)
inline int lastIndexLE(ll x, int n) {
int l = 1, r = n, ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (starts[mid] <= x) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
return ans;
}
void build(int n) {
prefMax[0] = -INF;
for (int i = 1; i <= n; ++i) {
starts[i] = a[i].first;
prefMax[i] = max(prefMax[i - 1], a[i].second);
}
// up[0][i] = last index with start <= prefMax[i] + 1
for (int i = 1; i <= n; ++i) {
int j = lastIndexLE(prefMax[i] + 1, n);
if (j <= i) up[0][i] = 0; // không tiến được
else up[0][i] = j;
}
for (int k = 1; k < LG; ++k) {
for (int i = 1; i <= n; ++i) {
int mid = up[k - 1][i];
up[k][i] = (mid == 0) ? 0 : up[k - 1][mid];
}
}
}
// trả về số bước để cover [p, R] (tọa độ) hoặc -1
ll query(pair<ll,ll> target, int n) {
ll p = target.first, R = target.second;
int idx = lastIndexLE(p, n); // index các interval có start <= p
if (idx == 0) return -1;
if (prefMax[idx] < p) return -1; // không thể bắt đầu phủ p
if (prefMax[idx] >= R) return 1; // 1 bước là đủ
int cur = idx;
int added = 0;
for (int k = LG - 1; k >= 0; --k) {
int to = up[k][cur];
if (to != 0 && prefMax[to] < R) {
cur = to;
added += (1 << k);
}
}
int nxt = up[0][cur];
if (nxt == 0) return -1;
added += 1;
return 1 + added;
}
void solve() {
checkLines.clear();
checkLines.push_back({0, m - 1});
for (int i = 1; i <= n; i++) {
if (s[i] > e[i]) {
checkLines.push_back({e[i], e[i] + m - 1});
e[i] += m;
}
a[i] = {s[i], e[i] - 1};
}
sort(a + 1, a + 1 + n, cmp);
build(n);
for (auto x : checkLines) {
ll ans = query(x, n);
if (ans == -1) continue;
result = min(result, ans);
}
if (result == INF) result = -1;
cout << result;
}
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> s[i] >> e[i];
}
sub6 :: solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | 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... |