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;
vector<int> vals;
vals.pb(0);
for(auto itr: st){
pos[itr] = ++here;
vals.pb(itr);
}
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] = pos[val];
}
for(int i = 1; i < K; i++){
for(int j = 1; j <= here; j++){
lift[j][i] = lift[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 = pos[f];
for(int bt = K-1; bt >= 0; bt--){
if(vals[lift[r][bt]] < m + f){
cnt += (1 << bt);
r = lift[r][bt];
}
}
if(vals[lift[r][0]] >= m + f) 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:61:14: warning: unused variable 'ok' [-Wunused-variable]
61 | 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... |