Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- using namespace std;
- void compare_and_change(set<pair<int, long long int>> & s, pair<int, long long int> & p);
- int main() {
- int n, start, k, l, r;
- cin >> n >> start >> k;
- set<pair<int, long long int>> s;
- s.insert(pair<int, long long int>(start, 0));
- pair<int, long long int> left, right;
- long long int dl, dr;
- for (int i = 0; i < k; ++i) {
- cin >> l >> r;
- auto first = s.lower_bound(pair<int,long long int>(l, 0));
- if (first == s.end() || first->first > r)
- continue;
- left.first = l-1;
- left.second = 0;
- right.first = r+1;
- right.second = 0;
- auto it = first;
- while (it != s.end() && it->first <= r) {
- dl = it->second+it->first-l+1;
- dr = it->second+r-it->first+1;
- if (left.second == 0 || left.second > dl)
- left.second = dl;
- if (right.second == 0 || right.second > dr)
- right.second = dr;
- it++;
- }
- s.erase(first, it);
- if (l > 1) compare_and_change(s, left);
- if (r < n) compare_and_change(s, right);
- }
- long long int d = -1;
- for (auto & it : s)
- if (d == -1 || d > it.second)
- d = it.second;
- cout << d;
- return 0;
- }
- void compare_and_change(set<pair<int, long long int>> & s, pair<int, long long int> & p) {
- auto it = s.lower_bound(pair<int, long long int>(p.first, 0));
- if (it != s.end() && it->first == p.first) {
- if (it->second > p.second) {
- s.erase(it);
- s.insert(p);
- }
- } else s.insert(p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement