#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define sz(x)(int) x.size()
#define ins insert
#define speed ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define pf push_front
#define pb push_back
#define all(x) x.begin(), x.end()
#define pii pair < int, int >
#define pll pair < ll, ll >
#define vi vector < int >
#define vll vector < long long >
#define vpii vector < pair < int, int >>
#define vpll vector < pair < long long, long long >>
#define mem memset
#define debug cerr << "OK\n";
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count());
const int inf = 2e9;
const ll INF = (ll)1e18;
const ll INF1 = (ll) 1e14 + 1;
const int N = (int) 2e5 + 5;
const ll mod = (int)1e9 + 7;
const ll mod1 = (ll) 1e9 + 9;
const ll MAX = (ll) 1e7 + 5;
const int P = 331, B = 300;
ll binpow(ll a, ll n) {
if (n == 0) return 1;
if (n == 1) return a;
ll r = binpow(a, n / 2);
if (n & 1) return (((r * r) % mod) * a) % mod;
return (r * r) % mod;
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int dx[4] = {
0,
-1,
0,
1
};
int dy[4] = {
1,
0,
-1,
0
};
ll mul(ll a, ll b) {
return (a * b) % mod;
}
int n, m;
pii t[N << 2];
bool used[N];
vector<int> g[N];
struct sch {
int l, r, id;
bool operator<(const sch& other) const { // Pass by const reference and mark as const
return l < other.l;
}
} a[N];
void build(int v = 1, int tl = 1, int tr = n) {
if (tl == tr) {
t[v] = {a[tl].r, a[tl].id};
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
pii get(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (tr < l || tl > r) return {0, 0};
if (l <= tl && tr <= r) {
return t[v];
}
int tm = (tl + tr) / 2;
return max(get(l, r, v * 2, tl, tm), get(l, r, v * 2 + 1, tm + 1, tr));
}
int up[N][20], timer = 0;
void dfs(int v, int p = 0) {
up[v][0] = p;
for (int i = 1; i < 20; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (int u : g[v]) {
if (u == p) continue;
dfs(u, v);
}
}
pii b[N];
int ANS(int u) {
int l = 1, r = n, ans = inf;
while (l <= r) {
int mid = (l + r) / 2;
int cur = 1, x = mid - 1, v = u;
for (int i = 19; i >= 0; --i) {
if ((1 << i) <= x && up[v][i] != 0) {
x -= (1 << i);
v = up[v][i];
}
}
if(b[v].S >= b[u].F + m) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
return ans;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
if (r < l) {
r += m;
}
a[i] = {l, r, i};
b[i] = {l, r};
}
sort(a + 1, a + n + 1);
build();
for (int i = n - 1; i > 0; --i) {
auto [l, r, id] = a[i];
sch R = {r, 0, 0};
int j = (int)(upper_bound(a + 1, a + n + 1, R) - a);
int nx = get(i, j - 1).S;
if (nx != id && b[nx].S > r) {
g[nx].pb(id);
used[id] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
dfs(i);
}
}
int ans = inf;
for (int i = 1; i <= n; i++) {
ans = min(ans, ANS(i));
}
if (ans >= inf) {
cout << "-1\n";
return;
}
cout << ans;
}
signed main() {
speed;
int T = 1;
//cin >> T;
for (int i = 1; i <= T; i++) {
solve();
}
#ifdef ONPC
cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:50:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:51:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen((s + ".out").c_str(), "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... |