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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp (pair<long long, long long> a, pair<long long, long long> b);
int main () {
    cin.tie(0) -> sync_with_stdio(0);
    int n, m, s;
    cin >> n >> m >> s;
    vector<pair<long long, long long>> P;
    for (int x = 0; x < m; x++) {
        long long l, r;
        cin >> l >> r;
        P.push_back({l, r});
    }
    sort(P.begin(), P.end(), cmp);
    long long bl = s - 1, br = s + 1;
    for (int x = 0; x < m; x++) {
        
        if (P[x].second >= bl && bl >= P[x].first) bl = P[x].first - 1;
        if (P[x].second >= br && br >= P[x].first) br = P[x].second + 1;
    }
    if (br > n || s - bl <= br - s && bl >= 1 && br <= n) cout << bl << "\n";
    else if (bl < 1 || s - bl > br - s && bl >= 1 && br <= n) cout << br << "\n";
}
bool cmp (pair<long long, long long> a, pair<long long, long long> b) {
    if (a.first < b.first) return false;
    return true;
}