Editorial for Apocalypse
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz(v) (v).size()
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
#define pr(a,b) make_pair(a,b)
#define f first
#define s second
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, s; cin >> n >> s;
map<int,ll> mp; mp[s] = 0;
for (int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
ll mnl = 1e18, mnr = 1e18;
auto it = mp.lower_bound(l);
while (it != mp.end() && it->f <= r) {
mnl = min(mnl, it->s + it->f - l);
mnr = min(mnr, it->s + r - it->f);
++it;
mp.erase(prev(it));
}
mp[l] = mnl;
mp[r] = mnr;
}
ll ans = 1e18;
for (auto &[a, b]: mp) ans = min(ans, b);
cout << ans << "\n";
}
Comments