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
// Homura Akemi a.k.a. Starrykiller (/user/235125)
// I love Madoka Kaname forever! 
#include <bits/stdc++.h>

using namespace std;

auto range(auto l, auto r) { return views::iota(l,r); }
auto rev=views::reverse;

_GLIBCXX_ALWAYS_INLINE void chmax(auto &a, auto b) { a=max(a,b); }
_GLIBCXX_ALWAYS_INLINE void chmin(auto &a, auto b) { a=min(a,b); }
#define int long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
// int T; cin>>T;
int T=1;
while (T--) []{
    int n, m, s;
    cin>>n>>m>>s;
    set<pair<int,int>> odt={{1,n}};
    auto split=[&](int x) {
        // [l,x-1], [x,r]
        // returns [x,r]
        auto it=odt.lower_bound({x,0});
        if (it!=end(odt) && it->first==x) return it;
        auto [L,R]=*--it; odt.erase(it);
        cerr<<L<<' '<<R<<' '<<x<<'\n';
        // assert(L<=x&&x<=R);
        it=odt.emplace(L,x-1).first;
        if (x>R)
            return ++it;
        return odt.emplace(x,R).first;
    };
    while (m--) {
        int l, r; cin>>l>>r;
        auto itr=split(r+1);
        
        // for (auto [l,r]: odt) cerr<<"("<<l<<","<<r<<")";
        // cerr<<'\n';
        auto itl=split(l);
        
        // for (auto [l,r]: odt) cerr<<"("<<l<<","<<r<<")";
        // cerr<<'\n';
        odt.erase(itl,itr);
        // for (auto [l,r]: odt) cerr<<"("<<l<<","<<r<<")";
        // cerr<<'\n';
        // cerr<<"------------\n";
    }
    int ans=1e18, u=0;
    for (auto [l,r]: odt) {
        // cerr<<l<<' '<<r<<'\n';
        if (abs(s-l)<ans) ans=abs(s-l), u=l;
        if (abs(s-r)<ans) ans=abs(s-r), u=r;
    }
    cout<<u<<'\n';
}();
}