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>

#define ndl '\n'
#define ll long long
#define st first
#define nd second
#define debug(x) cout << #x << ": " << x << ndl
#define all(x) (x).begin(), (x).end()

using namespace std;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll n, m, s; cin>>n>>m>>s;
    set<pair<ll, ll>> segments;
    for(int i=0;i<m;i++) {
        ll a, b; cin>>a>>b;
        auto x = segments.upper_bound({b, 0});
        if(x != segments.end() && x->first == b+1) {
            b = x->second;
            x = segments.erase(x);
        }
        if(x != segments.begin()) {
            x--;
            if(x->second == a-1) {
                a = x->first;
                segments.erase(x);
            }
        }
        segments.insert({a, b});
    }

    auto x = segments.upper_bound({s, LLONG_MAX});
    x--;
    auto [a, b] = *x;
    
    if(b-s < s-a) {
        if(b+1 <= n) {
            cout<<b+1<<'\n';
        } else {
            cout<<a-1<<'\n';
        }
    } else {
        if(a-1 > 0) {
            cout<<a-1<<'\n';
        } else {
            cout<<b+1<<'\n';
        }
    }
    return 0;
}