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>
#define int long long
using namespace std;
const int inf = 1e12 + 9;
int32_t main(){
    int n, m, s;
    cin >> n >> m >> s;
    vector<pair<int,int>>v;
    v.push_back({0,0});
    v.push_back({n+1,n+1});
    for(int i = 0; i < m; ++i){
        int a, b; cin >> a >> b;
        v.push_back({a, b});
    }
    sort(v.begin(), v.end());
    pair<int,int>score = {-inf, inf};
    for(int i = 1; i <= m; ++i){
        if(v[i-1].second+1 != v[i].first and v[i].first-1 >= 1){
            if(score.second > abs(v[i].first-1-s)){
                score = {v[i].first-1, abs(v[i].first-1-s)};
            }
        }
        if(v[i].second+1 != v[i+1].first and v[i].second+1 <= n){
            if(score.second > abs(v[i].second+1-s)){
                score = {v[i].second+1, abs(v[i].second+1-s)};
            }
        }
    }
    cout << score.first;
}