// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "gen"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int n, m;
vector<ii> range;
int nxt[N][20];
void init(void) {
cin >> n >> m;
FOR(i, 1, n) {
int L, R;
cin >> L >> R;
if (R < L) R += m;
range.pb(mp(L, R));
}
}
void process(void) {
sort(all(range));
// for(ii x : range) cerr << x.fi << ' ' << x.se << '\n';
int j = 0, mxR = 0;
REP(i, n) {
maximize(j, i);
while(j + 1 < n && range[i].se >= range[j + 1].fi) {
j++;
if (range[j].se >= range[mxR].se) mxR = j;
}
// cerr << i << ' ' << mxR << '\n';
if (range[i].se >= range[mxR].fi) nxt[i][0] = mxR;
}
FOR(j, 1, 19) REP(i, n)
nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
auto jump = [&](int i, int k) {
RED(j, 20) if (BIT(k, j))
i = nxt[i][j];
return i;
};
int low = 1, high = n, res = -1;
while(low <= high) {
int mid = (low + high) >> 1;
bool ok = false;
REP(i, n) {
int j = jump(i, mid - 1);
if (range[j].se - range[i].fi >= m) {
ok = true;
break;
}
}
if (ok) res = mid, high = mid - 1;
else low = mid + 1;
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | 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... |