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
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;

int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    long long n, m, s;
    cin >> n >> m >> s;
    vector<pair<long long, long long>> a;
    long long temp, temp2;
    for (long long i = 0;i < m;i++){
        cin >> temp >> temp2;
        a.push_back({temp,temp2});
    }
    sort(a.begin(),a.end());
    vector<pair<long long, long long>> b;
    pair<long long, long long> caly;
    caly.first = a[0].first;
    for (long long i = 0;i < m;i++){
        if (i == m){
            caly.second = a[i].second;
            b.push_back(caly);
            break;
        }
        if (a[i].second != a[i+1].first-1){
            caly.second = a[i].second;
            b.push_back(caly);
            caly.first = a[i+1].first;
        }
    }

    // for (long long i = 0;i < b.size();i++){
    //     cout << b[i].first << " co " << b[i].second << '\n';
    // }


    long long lewy = -3;
    long long prway = -2;
    for (long long i = 0;i < b.size();i++){
        if (s >= b[i].first && s <= b[i].second){
            //cout << b[i].first << " " << b[i].second << " " << s << endl;
            lewy = b[i].first - 1;
            prway = b[i].second + 1;
            //cout << lewy << " " << prway << " " << s << endl;
            break;
        }
    }
    if (lewy == 0){
        cout << prway << '\n';
    }
    else if (prway > n){
        cout << lewy << '\n';
    }
    else if (s - lewy <= prway - s){
        cout << lewy << '\n';
    }
    else{
        cout << prway << '\n';
    }
    
return 0;
}