#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;
ll maxx;
void compress() {
vector <ll> c;
for (int i = 1; i <= n; i++) {
c.push_back(a[i].first);
c.push_back(a[i].second);
}
for (auto x : checkLines) {
c.push_back(x.first);
c.push_back(x.second);
}
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
for (int i = 1; i <= n; i++) {
a[i].first = lower_bound(c.begin(), c.end(), a[i].first) - c.begin() + 1;
a[i].second = lower_bound(c.begin(), c.end(), a[i].second) - c.begin() + 1;
maxx = max(maxx, a[i].first);
maxx = max(maxx, a[i].second);
}
for (auto &x : checkLines) {
x.first = lower_bound(c.begin(), c.end(), x.first) - c.begin() + 1;
x.second = lower_bound(c.begin(), c.end(), x.second) - c.begin() + 1;
maxx = max(maxx, x.first);
maxx = max(maxx, x.second);
}
}
ll maxR[N], up[N][26];
void solve() {
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};
}
compress();
for (int i = 1; i <= n; i++) {
maxR[a[i].first] = a[i].second;
}
for (int i = 1; i <= maxx; i++) {
maxR[i] = max(maxR[i - 1], maxR[i]);
}
for (int i = maxx; i >= 1; i--) {
if (maxR[i] != maxR[i + 1]) {
up[i][0] = i;
}
else {
up[i][0] = up[i + 1][0];
}
}
for (auto x : checkLines) {
bool flag = true;
for (int i = x.first; i <= x.second; i++) {
if (maxR[i] == 0) {
flag = false;
break;
}
}
if (!flag) {
continue;
}
int u = x.first, v = x.second, ans = 0;
while (u <= v) {
int k = -1;
for (int i = 20; i >= 0; i--) {
if (up[u][i] == 0) {
continue;
}
if (up[u][i] < v) {
k = i;
break;
}
}
if (k == -1) {
break;
}
u = up[u][k] + 1;
ans += (1 << k);
}
ans++;
result = min(result, 1LL * ans);
}
if (result > n) {
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:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | 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... |