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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long> PLL;



int main() {
    ios_base::sync_with_stdio(0);
    long long n, m, s;
    cin>>n>>m>>s;
    vector<PLL> occ;
    occ.push_back({0, 0});
    occ.push_back({n+1, n+1});
    for(int i=0;i<m;i++) {
        long long a, b;
        cin>>a>>b;
        occ.push_back({a, b});
    }
    sort(occ.begin(), occ.end());

    long long res = 1e18;
    long long pos = 0;
    for(int i=1;i<=m;i++) {
        auto [a, b] = occ[i];
        if(a-1 != occ[i-1].second) {
            if(abs(s-(a-1)) < res) {
                res = abs(s-(a-1));
                pos = a-1;
            }
        }
        if(b+1 != occ[i+1].first) {
            if(abs(s-(b+1)) < res) {
                res = abs(s-(b+1));
                pos = b+1;
            }
        }
    }
    cout<<pos<<endl;
}