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
64
65
66
67
#include <bits/stdc++.h>
using namespace std;

struct Przedzial{
    long long l, r;
    bool operator<(const Przedzial& other) const{
        return l < other.l;
    }
};

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    long long n, m, s; cin >> n >> m >> s;

    vector< Przedzial > v;

    for(int i = 0; i < m; ++i){
        long long l, r; cin >> l >> r;
        v.push_back({l, r});
    }

    sort(v.begin(), v.end());

    vector< Przedzial > merged;

    Przedzial temp = v[ 0 ];
    for(int i = 1; i < v.size(); ++i){
        if(temp.r + 1 == v[ i ].l){
            temp.r = v[ i ].r;
        }
        else{
            merged.push_back(temp);
            temp = v[ i ];
        }
    }

    merged.push_back(temp);

    for(auto[ l, r ] : merged){
        if(s >= l && s <= r){
            //cout << l << " " << r << '\n';
            long long low = l - 1;
            long long high = r + 1;
            if(low < 1){
                low = LLONG_MIN;
            }
            if(high > n){
                high = LLONG_MAX;
            }
            low = abs(low - s);
            high = abs(high - s);
            if(high == low){
                cout << l - 1 << '\n';
                return 0;
            }
            cout << (low < high ? l - 1 : r + 1) << '\n';
            return 0;
        }
    }


    return 0;

}