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 int long long
using namespace std;

set <pair<int,int>> s;
int n,m,k;

int dis(int a,int b){
    return max(a,b) - min(a,b);
}

void solve(){
    int ans = 1e18;
    cin >> n >> m >> k;
    for(int i = 0;i < m;i++){
        int a,b;
        cin >> a >> b;
        s.insert({a,b});
    }
    s.insert({0,0});
    s.insert({n+1,n+1});
    auto t = s.begin();
    auto u = t;
    t++;
    while(t != s.end()){
        if(t->first - u->second > 1){
            if(dis(t->first-1,k) < dis(ans,k)){
                ans = t->first-1;
            }
            if(dis(u->second+1,k) < dis(ans,k)){
                ans = u->second+1;
            }
        }
        u++;
        t++;
    }
    cout << ans << "\n";
    return;
}

signed main(){
    iostream::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }

    return 0;
}