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
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto operator<<(auto&o, auto p)->decltype(p.first, o){return o<<'('<<p.first<<", "<<p.second<<')';}
auto operator<<(auto&o, auto x)->decltype(x.end(), o){o<<'{';int i=2;for (auto e:x)o<<(", ")+i<<e,i=0;return o<<'}';}
#define LOG(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X);
#else
#define LOG(X...)(void)0
#endif

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

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

    vector<pair<long long, long long>> values(m);
    for (int i = 0; i < m; i++)
        cin >> values[i].first >> values[i].second;

    sort(values.begin(), values.end());
    LOG(m);
    auto join = [&](int index) -> int {
        LOG(index);
        long long last = values[index].second;
        while (index < m - 1 && values[index + 1].first - 1 == last) {
            index++;
            LOG(index);
            last = values[index].second;
        }
        return index;
    };

    vector<pair<long long, long long>> nvalues;
    for (int i = 0; i < m; i++) {
        long long begin = values[i].first;
        int ret = join(i);
        long long end = values[ret].second;
        i = ret;
        nvalues.push_back({begin, end});
    }
    LOG(nvalues);

    int index = (int)distance(nvalues.begin(), lower_bound(nvalues.begin(), nvalues.end(), make_pair((long long)s + 1, 0LL))) - 1;
    long long first = nvalues[index].first - 1;
    long long second = nvalues[index].second + 1;
    if (first < 1) {
        cout << second << "\n";
    } else if (second > n) {
        cout << first << "\n";
    } else if (s - first <= second - s) {
        cout << first << "\n";
    } else {
        cout << second << "\n";
    }
}