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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;

int  main(){
    ll n, m, s;
    cin >> n >> m >> s;
    vector<pair<int, int>> occupied = vector<pair<int, int>>();

    ll a, b;
    for(ll i = 0; i < m; i++){
        cin >> a >> b;
        occupied.push_back(make_pair(a, b));
    }

    sort(occupied.begin(), occupied.end());

    ll it = 0;
    for(auto x : occupied){
        if(x.first <= s && s <= x.second){
            break;
        }
        it++;
    }

    ll i, j;
    if(it - 1 < 0){
        i = -1;
    }
    else{
        i = it-1;
    }

    if(it + 1 >= m){
        j = m+1;
    }
    else{
        j = it+1;
    }

    ll p1, p2, x = -1, y = -1;
    bool flag1 = false, flag2 = false;
    while(!flag1 || !flag2){
        if(i >= 0){
            if(occupied[it].first - 1 == occupied[i].second){
                occupied[it].first = occupied[i].first;
                i--;
            }
            else{
                x = occupied[it].first - 1;
                p1 = s - x;
                flag1 = true;
            }
        }
        else{
            flag1 = true;
        }

        if(j < m){
            if(occupied[it].second + 1 == occupied[j].first){
                occupied[it].second = occupied[j].second;
                j++;
            }
            else{
                y = occupied[it].second + 1;
                p2 = y - s;
                flag2 = true;
            }
        }
        else{
            flag2 = true;
        }
    }

    if(x == -1 && y == -1){
        if(occupied[it].first - 1 < 1){
            cout << occupied[it].second + 1 << endl;
        }
        else if(occupied[it].second + 1 > n){
            cout << occupied[it].first - 1 << endl;
        }
        else{
            p1 = s - (occupied[it].first - 1);
            p2 = (occupied[it].second + 1) - s;
            if(p1 <= p2){
                cout << occupied[it].first - 1 << endl;
            }
            else{
                cout << occupied[it].second + 1 << endl;
            }
        }
    }
    else if(x == -1){
        if(occupied[it].first - 1 < 1){
            cout << y << endl;
        }
        else{
            p1 = s - (occupied[it].first - 1);
            if(p1 <= p2){
                cout << occupied[it].first - 1 << endl;
            }
            else{
                cout << y << endl;
            }
        }
    }
    else if(y == -1){
        if(occupied[it].second + 1 > n){
            cout << x << endl;
        }
        else{
            p2 = (occupied[it].second + 1) - s;
            if(p1 <= p2){
                cout << x << endl;
            }
            else{
                cout << occupied[it].second + 1 << endl;
            }
        }
    }
    else{
        if(p1 <= p2){
            cout << x << endl;
        }
        else{
            cout << y << endl;
        }
    }



    return 0;
}