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

pair <long long,int> pos[1005];

int main()
{
    long long n,s;
    int m;

    long long l,p;

    cin >> n >> m >> s;
    l=s-1;
    p=s+1;

    for(int i=0; i<m; i++)
    {
        long long x,y;
        cin >> x >> y;
        pos[i].first=x;
        pos[i].second=y;
    }
    sort(pos,pos+m);
    for(int i=0; i<m; i++)
    {
        long long x = pos[i].first;
        long long y = pos[i].second;
        if((p<=y || p==x)&&p>=x) p=y+1;
        //cout << l << ' ' << p << "\n";

    }
    for(int i=m; i>0; i--)
    {
        long long x = pos[i].first;
        long long y = pos[i].second;
        if((l>=x || l==y)&&l<=y) l=x-1;
        //cout << l << ' ' << p << "\n";

    }

    long long w=s-l;
    if(p-s<w)
    {
        cout << p;
    }
    else
    {
        cout << l;
    }
}