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

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    llong n; int m; llong s;
    cin>>n>>m>>s;
    s--;
    vector<pair<llong,llong>>A(m);
    for(int i=0; i<m; i++)
    {
        cin>>A[i].first>>A[i].second;
        A[i].first--; A[i].second--;
    }
    sort(A.begin(),A.end());
    
    set<llong>P,Q;
    if(0<=A[0].first-1)
    {
        P.insert(0);
        Q.insert(A[0].first-1);
    }
    for(int i=0; i+1<m; i++)
    {
        if(A[i].second+1<=A[i+1].first-1)
        {
            P.insert(A[i].second+1);
            Q.insert(A[i+1].first-1);
        }
    }
    if(A[m-1].second+1<=n-1)
    {
        P.insert(A[m-1].second+1);
        Q.insert(n-1);
    }

    auto p=P.upper_bound(s);
    auto q=Q.upper_bound(s);
    if(q==Q.begin())
    {
        cout<<*p+1;
        return 0;
    }
    q=prev(q);
    if(p==P.end())
    {
        cout<<*q+1;
        return 0;
    }
    
    if(llabs(s-(*p))>=llabs(s-(*q)))
    {
        cout<<*q+1;
        return 0;
    }
    cout<<*p+1;
    return 0;
}