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 <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long n, m, s, in1, in2, st, bl, br;
    cin>>n>>m>>s;
    pair <long long, long long> tab[m];
    vector <pair<long long, long long>> vec;
    for(int i = 0; i < m; i++) {
        cin>>tab[i].first>>tab[i].second;
    }
    sort(tab, tab + m);
    st = tab[0].first;
    for(int i = 1; i < m; i++) {
        if(tab[i].first - tab[i - 1].second != 1) {
            vec.push_back({st, tab[i - 1].second});
            st = tab[i].first;
        }
    }
    vec.push_back({st, tab[m - 1].second});
    for(auto i : vec) {
        if(i.second >= s && i.first <= s) {
            bl = i.first - 1;
            br = i.second + 1;
            break;
        }
    }
    if(bl == 0) cout<<br;
    else if(br == n + 1) cout<<bl;
    else {
        if(s - bl <= br - s) cout<<bl;
        else cout<<br;
    }
}