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
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, s;
    int m;
    cin >> n >> m >> s;
    vector<pair<ll, ll>> a(m);
    for (int i = 0; i < m; i++)
        cin >> a[i].first >> a[i].second;
    sort(a.begin(), a.end());
    ll b = 1;
    ll mnd = n + 1;
    ll res = 0;
    for (int i = 0; i < m; i++) {
        if (b < a[i].first) {
            ll d1 = abs(b - s);
            if (d1 < mnd){
                mnd = d1;
                res = b;
            }
            ll d2 = abs(a[i].first - 1 - s);
            if (d2 < mnd) {
                mnd = d2;
                res = a[i].first - 1;
            }
        }
        b = a[i].second + 1;
    }
    if (b <= n && abs(b - s) < mnd) {
        res = b;
    }
    cout << res << "\n";
    return 0;
}