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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll MIN(ll a,ll b){
    if(a<b)
        return a;
    return b;
}
int main(){
    ll n,s;
    int m;
    cin >> n >> m >> s;
    vector<pair<ll,ll>>V(m);
    for(int i = 0;i<m;i++){
        ll a,b;
        cin >> a >> b;
        V[i] = {a,b};
    }
    if(m==0){
        if(s-1>0)
            cout << s-1 << '\n';
        else
            cout << s+1 <<'\n';
        return 0;
    }
    sort(V.begin(),V.end());
    vector<pair<ll,ll>>tab;
    tab.push_back(V[0]);
    for(int i = 1;i<m;i++){
        if(tab[tab.size()-1].second == V[i].first-1)
            tab[tab.size()-1].second = V[i].second;
        else{
            tab.push_back(V[i]);
        }
    }
    for(auto x:tab){
        if(x.first<=s&&s<=x.second){
            if(x.first-1>0&&x.second+1<=n)
                if(abs(x.first-1-s)<=abs(x.second+1-s))
                    cout << x.first-1 << '\n';
                else
                    cout << x.second+1 << '\n';
            else if(x.first-1>0)
                cout <<x.first-1 << '\n';
            else
                cout <<x.second+1 << '\n';
        }
    }
    /*
    for(auto x:tab){
        cout << x.first << ' ' << x.second << '\n';
    }
    */

   return 0; 
}