#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<pair<ll, ll>>
#define vpp vector<pair<ll, pair<ll, ll>>>
#define pp pair<ll, ll>
#define qi queue<ll>
#define qp queue<pp>
#define pqi priority_queue<ll>
#define pqp priority_queue<pp>
#define mi map<ll, ll>
#define mpi map<pp, ll>
#define mip map<ll, pp>
#define mp map<pp, pp>
#define mb map<ll, bool>
#define si set<ll>
#define sp set<pp>
#define sc set<char>
#define mod 1000000007
#define inf INT_MAX
#define all(x) (x).begin(), (x).end()
pp get_max(ll l, ll r, ll k, ll a, ll b, vp& tree) {
if(r < a || b < l) return {-1, -1};
if(a <= l && r <= b) return tree[k];
ll d = (l + r) / 2;
pp ans1 = get_max(l, d, 2 * k, a, b, tree);
pp ans2 = get_max(d + 1, r, 2 * k + 1, a, b, tree);
if(ans1.first >= ans2.first) return ans1;
return ans2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, m;
cin >> n >> m;
vp a(n);
si times;
for(int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
if(a[i].first > a[i].second) {
a[i].second += m;
}
times.insert(a[i].first);
times.insert(a[i].second);
}
vi coor2;
mi coor;
for(auto time : times) {
coor[time] = coor2.size();
coor2.push_back(time);
}
ll size = pow(2, ceil(log2(int(times.size()))));
vp tree(size * 2, {-1, -1});
for(int i = 0; i < n; i++) {
ll start = coor[a[i].first];
ll end = coor[a[i].second];
tree[start + size] = {end, i};
}
for(int i = size - 1; i > 0; i--) {
if(tree[i * 2].first >= tree[i * 2 + 1].first) {
tree[i] = tree[i * 2];
}
else {
tree[i] = tree[i * 2 + 1];
}
}
ll lg = 1;
while((1 << lg) <= n) lg++;
vvi up(n, vi(lg, -1));
for(int i = 0; i < n; i++) {
auto[start, end] = a[i];
ll l = coor[start], r = coor[end];
auto[mx, ind] = get_max(0, size - 1, 1, l, r, tree);
if(mx <= r) ind = -1;
up[i][0] = ind;
}
for(int k = 1; k < lg; k++) {
for(int i = 0; i < n; i++) {
ll par = up[i][k - 1];
if(par == -1) up[i][k] = -1;
else up[i][k] = up[par][k - 1];
}
}
ll ans = inf;
for(int i = 0; i < n; i++) {
ll cur = inf;
ll l = 1, r = n;
while(l <= r) {
ll v = i;
ll d = (l + r) / 2;
for(int k = lg - 1; k >= 0; k--) {
if(d & (1 << k)) {
v = up[v][k];
}
if(v == -1) break;
}
if(v == -1) {
r = d - 1;
continue;
}
ll start = a[i].first;
ll end = a[v].second;
if(start + m <= end) {
cur = min(cur, d + 1);
r = d - 1;
}
else {
l = d + 1;
}
}
ans = min(ans, cur);
}
if(ans == inf) ans = -1;
cout << ans << '\n';
return 0;
}
/*
4 100
11 30
0 9
9 0
4 5
2
*/
# | 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... |