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
#include <bits/stdc++.h>
using namespace std;
#define int long long

int n, m, s;
vector<int> tabL;
vector<int> tabR;

int solve() {

}

int brute() {
    vector<pair<int, int>> tab(m);
    for (int i = 0; i < m; ++i)
        cin >> tab[i].first >> tab[i].second;
    std::sort(tab.begin(), tab.end());

    int k = -1;
    for (int i = 0; i < m; ++i) {
        if (i>0 && tabR[k]+1 == tab[i].first)
            tabR[k] = tab[i].second;
        else {
            k++;
            tabL.push_back(tab[i].first);
            tabR.push_back(tab[i].second);
        }
    }

    /*for (int i = 0; i < tabL.size(); ++i) {
        cout << tabL[i] << ' ' << tabR[i] << '\n';
    }*/

    int i = 0;
    while (s > tabR[i]) i++;
    //cout << i << ' ' << s << ' ' << s-tabL[i] << ' ' << tabR[i]-s << '\n';

    if ((s-tabL[i] <= tabR[i]-s && tabL[i]>1) || tabR[i]==n) {
        cout << tabL[i]-1;
    } else {
        cout << tabR[i]+1;
    }

    return 0;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //srand(time(NULL));

    cin >> n >> m >> s;
    brute();

}