This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
* Author: Nonoze
* Created: Monday 06/05/2024
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
#define int long long
#ifdef _IN_LOCAL
#include <bits/DebugTemplate.h>
#else
#define dbg(...) ;
#endif
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) {cout << x << endl; return;}
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;
void solve();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
// cin >> tt;
while(tt--) solve();
return 0;
}
int n, m, q;
vector<pair<int, int>> a;
vector<int> memo, nxt;
int first=0;
vector<vector<int>> sparce;
vector<int> logs;
void init() {
vector<int> vec;
for (int i=0; i<n; i++) vec.pb(a[i].fi);
logs.clear(), logs.resize(n+1); logs[0]=0, logs[1]=0;
for (int i=2; i<=n; i++) logs[i]=logs[i/2]+1;
int lg=logs[n]+1;
sparce.clear(), sparce.resize(lg, vector<int>(n, inf));
for (int i=0; i<n; i++) sparce[0][i]=vec[i];
for (int i=1; i<lg; i++) {
for (int j=0; j+(1<<i)<=n; j++) {
sparce[i][j]=min(sparce[i-1][j], sparce[i-1][j+(1<<(i-1))]);
}
}
}
int query(int l, int r) {
int lg=logs[r-l+1];
return min(sparce[lg][l], sparce[lg][r-(1<<lg)+1]);
}
vector<vector<int>> up;
vector<int> depth;
vector<bool> did;
void dfs(int s) {
if (s>=n || did[s]) return;
did[s]=1;
dfs(nxt[s]);
up[s][0]=nxt[s];
depth[s]=depth[nxt[s]]+1;
for (int i=1; i<LOG; i++) {
up[s][i]=up[up[s][i-1]][i-1];
}
}
int kth(int s, int k) {
for (int i=0; i<LOG; i++) {
if (k&(1<<i)) s=up[s][i];
}
return s;
}
void solve() {
cin >> n >> m;
a.clear(), a.resize(n), nxt.resize(n);
for (auto &u: a) {
cin >> u.fi >> u.se;
if (u.se<u.fi) u.se+=m;
}
sort(all(a), [&](pair<int, int> &x, pair<int, int> &y) {
return x.se < y.se;
});
init();
for (int i=n-1; i>=0; i--) {
int l=i+1, r=n-1, ans=n;
while (l<=r) {
int mid=(l+r)/2;
if (query(mid, r)<=a[i].se) {
ans=mid;
l=mid+1;
} else r=mid-1;
}
nxt[i]=ans;
}
up.resize(n+1, vector<int>(LOG, n)), did.resize(n, 0), depth.resize(n+1, 0);
for (int i=0; i<n; i++) dfs(i);
int ans=inf;
for (int i=0; i<n; i++) {
int objective=a[i].fi+m;
int l=1, r=depth[i]-1, res=inf;
while (l<=r) {
int mid=(l+r)/2;
if (a[kth(i, mid)].se>=objective) {
res=mid;
r=mid-1;
} else l=mid+1;
}
cmin(ans, res+1);
}
if (ans!=inf) cout << ans << endl;
else cout << -1 << endl;
}
# | 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... |