1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, m, s;
    cin >> n >> m >> s;
    vector<pair<ll, ll>> zakresy(m);
    for (auto& [a, b] : zakresy) cin >> a >> b;
    sort(zakresy.begin(), zakresy.end());
    vector<pair<ll, ll>> possible;
    ll poprz = 0;
    for (auto& [l, r] : zakresy) {
        if (poprz + 1 <= l - 1) possible.emplace_back(poprz + 1, l - 1);
        poprz = max(poprz, r);
    }
    if (poprz < n) possible.emplace_back(poprz + 1, n);
    ll res = -1, min_odl = LLONG_MAX;
    for (auto& [a, b] : possible) {
        ll kandydat = (a > s) ? a : b;
        ll odl = abs(kandydat - s);
        if (odl < min_odl || (odl == min_odl && kandydat < res)) {
            min_odl = odl;
            res = kandydat;
        }
    }
    cout << res << "\n";
}