This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 2e5 + 5;
const int K = 20;
const int mod = 1e9 + 7;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin>>n>>m;
vector<pair<int, int> > a(n);
set<int> st;
for(int i = 0; i < n; i++){
cin>>a[i].first>>a[i].second;
st.insert(a[i].first);
st.insert(a[i].second);
st.insert(a[i].first + m);
st.insert(a[i].second + m);
}
map<int, int> pos;
int here = 0;
for(auto itr: st){
pos[itr] = ++here;
}
vector<int> upd(here + 1);
for(int i = 0; i < n; i++){
int f = a[i].first, s = a[i].second;
if(f > s){
upd[pos[f]] = max(upd[pos[f]], s + m);
}
else{
upd[pos[f]] = max(upd[pos[f]], s);
upd[pos[f + m]] = max(upd[pos[f]], s + m);
}
}
vector<vector<int> > lift(here + 1, vector<int>(K));
int val = 0;
for(auto itr: st){
val = max(val, upd[pos[itr]]);
lift[pos[itr]][0] = val;
}
for(int i = 1; i < K; i++){
for(int j = 1; j <= here; j++){
lift[j][i] = lift[pos[lift[j][i-1]]][i-1];
}
}
int ans = n + 23;
for(int st = 0; st < n; st++){
int f = a[st].first;
int cnt = 0;
bool ok = 1;
int r = f;
for(int bt = K-1; bt >= 0; bt--){
if(lift[pos[r]][bt] - f < m){
cnt += (1 << bt);
r = lift[pos[r]][bt];
}
}
if(lift[pos[r]][0] - f >= m) ans = min(ans, cnt + 1);
}
if(ans == n + 23) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:58:14: warning: unused variable 'ok' [-Wunused-variable]
58 | bool ok = 1;
| ^~
# | 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... |