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

#define TYPE unsigned long long

void print(std::vector<std::pair<TYPE, TYPE>> data)
{
    for (std::pair<TYPE, TYPE> d : data)
    {
        std::cout << d.first << ' ' << d.second << '\n';
    }
    std::cout << "---\n";
}

int main()
{
    std::vector<std::pair<TYPE, TYPE>> data, free_places;
    TYPE res, n, s, p = 1ULL;
    int m;
    std::cin >> n >> m >> s;
    data.resize(m);
    for (int i = 0; i < m; i++)
    {
        std::cin >> data[i].first >> data[i].second;
    }
    std::sort(data.begin(), data.end());
    for (int i = 0; i < m; i++)
    {
        if (p < data[i].first)
        {
            free_places.push_back(std::pair(p, data[i].first - 1));
        }
        p = data[i].second + 1;
    }
    if (p <= n)
    {
        free_places.push_back(std::pair(p, n));
    }
    auto found = std::upper_bound(free_places.begin(), free_places.end(), std::pair(s, s));
    if (found == free_places.end())
    {
        res = (found - 1)->second;
    }
    else if (found == free_places.begin())
    {
        res = found->first;
    }
    else
    {
        if (s - (found - 1)->second > found->first - s)
        {
            res = found->first;
        }
        else
        {
            res = (found - 1)->second;
        }
    }
    // print(data);
    // print(free_places);
    std::cout << res << '\n';
    return 0;
}