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>
using namespace std;
const int LOG = 20;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, M; cin >> N >> M;
vector<vector<pair<int, int>>> binlift(LOG, vector<pair<int, int>>(N));
vector<pair<int, int>> inv(N);
vector<int> s(N), e(N);
for(int i = 0; i < N; i++){
cin >> s[i] >> e[i];
if(s[i] > e[i]) e[i] += M;
inv[i] = {s[i], i};
}
sort(inv.begin(), inv.end());
set<pair<int, int>> ending;
for(auto [l, i] : inv){
while(!ending.empty() && (*ending.begin()).first < l){
binlift[0][(*ending.begin()).second] = *ending.rbegin();
ending.erase(ending.begin());
}
ending.insert({e[i], i});
}
while(!ending.empty()){
binlift[0][(*ending.begin()).second] = *ending.rbegin();
ending.erase(ending.begin());
}
for(int k = 1; k < LOG; k++){
for(int i = 0; i < N; i++){
binlift[k][i] = binlift[k-1][binlift[k-1][i].second];
}
}
int ans = 1e9;
for(int i = 0; i < N; i++){
int curr = i;
int currAns = 0;
for(int k = LOG - 1; k >= 0; k--){
if(binlift[k][curr].first >= M && binlift[k][curr].first - M >= s[i]){
ans = min(ans, currAns + (1 << k));
}else{
currAns += (1 << k);
curr = binlift[k][curr].second;
}
}
}
cout << (ans == 1e9 ? -1 : ans + 1) << "\n";
}
# | 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... |