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
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, m, s;
    cin >> n >> m >> s;
    long long res = n;
    long long p = n + 1;
    long long l, r;
    set<long long> taken;
    vector<pair<int, int>> v;
    for (long long i = 0; i < m; i++)
    {
        cin >> l >> r;
        v.push_back({ l, r });
        taken.insert(l);
        taken.insert(r);
    }
    for (long long i = 0; i < m; i++)
    {
        l = v[i].first;
        r = v[i].second;
        if (l > 0 && !taken.count(l - 1))
        {
            if (res > abs(s - l + 1))
            {
                res = abs(s - l + 1);
                p = l - 1;
            }
            else if (res == abs(s - l + 1))
            {
                p = min(p, l - 1);
            }
        }
        if (r < n && !taken.count(r + 1))
        {
            if (res > abs(r + 1 - s))
            {
                res = abs(r + 1 - s);
                p = r + 1;
            }
            if (res == abs(r + 1 - s))
            {
                p = min(p, r + 1);
            }
        }
    }
    cout << p << endl;
}