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
#include<bits/stdc++.h>
#define ll long long
using namespace std;

void solve(){
    long long n, m, c;
    cin >> n >> m >> c;
    //vector<int> arr(n+7);
    vector<pair<ll, ll>> interference;
    for(ll i = 0; i < m; i++){
        ll l, r;
        cin >> l >> r;
        interference.push_back({l, r});
    }
    sort(interference.begin(), interference.end());
    ll last_id = 0;
    ll ans = n+2;
    ll ans_loc = -1;
    for(auto [l, r]: interference){
        if(last_id + 1 != l){
            ll tmp1 = c - (last_id + 1);
            ll tmp2 = c - (l - 1);
            if(tmp1 < 0)tmp1*=-1;
            if(tmp2 < 0)tmp2*=-1;
            if(ans > tmp1){
                ans = tmp1;
                ans_loc = (last_id + 1);
            }
            if(ans > tmp2){
                ans = tmp2;
                ans_loc = (l - 1);   
            }
        }
        last_id = r;
    }
    if(last_id < n){
        ll tmp1 = c - (last_id + 1);
        if(tmp1 < 0)tmp1*=-1;
        if(ans > tmp1){
            ans_loc = last_id + 1; 
        }
        ans = min(ans, tmp1);
    }
    cout << ans_loc << '\n';


    



}

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