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

using namespace std;

typedef long long LL;
typedef pair<LL, LL> PL;

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

    LL n, m, s;
    cin >> n >> m >> s;

    vector<PL> raw(m);
    vector<PL> joined(m);
    for (int i = 0; i < m; ++i) cin >> raw[i].first >> raw[i].second;
    sort(raw.begin(), raw.end());

    for (int i = 0; i < m; ++i)
    {
        LL l = raw[i].first;
        while (i < m - 1 && raw[i + 1].first == raw[i].second + 1) ++i;
        joined.emplace_back(l, raw[i].second);
    }

    int k = 0;
    while (joined[k].second < s) ++k;

    LL left = joined[k].first - 1;
    LL right = joined[k].second + 1;
    if (left < 1) left = -n;
    if (right > n) right = 2 * n;

    if (s - left <= right - s) cout << left;
    else cout << right;
}