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
#include <bits/stdc++.h>
#define int long long 
using namespace std;
int32_t main(){
    cin.tie(0)->sync_with_stdio(0);
    int n, m, pos; cin >> n >> m >> pos;
    vector<pair<int, int>>vect, vect2;
    for(auto i = 0; i < m; ++i){
        int a, b; cin >> a >> b;
        vect.emplace_back(a, b);
    }
    sort(vect.begin(), vect.end());
    for(auto i = 0; i < m; ++i){
        if(i == m-1) vect2.push_back(vect[i]);
        else if(vect[i].second+1 != vect[i+1].first) vect2.push_back(vect[i]);
        else vect[i+1].first = vect[i].first;
    }
    // for(auto [a, b] : vect2) cout << a << " " << b << endl;
    m = vect2.size();
    for(auto i = 0; i < m; ++i){
        int a, b; a = vect2[i].first, b = vect2[i].second;
        if(a <= pos && pos <= b){
            if(a == 1){
                cout << b+1;
                return 0;
            } else if(b == n){
                cout << a-1;
                return 0;
            } else if(pos-a <= b-pos){
                cout << a-1;
                return 0;
            } else{
                cout << b+1;
                return 0;
            }
        }
    }
}