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
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

int main(){
long long n, s, left, right, sl, sr;
int m;
set<long long> houses {};

cin >> n >> m >> s;
houses.insert(s);
for(int i = 0; i < m; i++){
    cin >> left >> right;
    for(long long j = left; j <= right; j++){
        houses.insert(j);
        //cout << "z "<< j << "\n";
    }
}


sl = s;
sr = s;

while(true){
if(sl >= 1){
    if(houses.find(sl) == houses.end()){
    //cout << "sl in " << sl << "\n" ;
    cout << sl;
    break;
    }
    sl--;
    //cout << "sl " << sl << "\n" ;

}
if(sr <= n){
    if(houses.find(sr) == houses.end()){
    //cout << "sr in " << sr << "\n" ;
    cout << sr;
    break;
    }
    sr++;
    //cout << "sr " << sr << "\n" ;
}
}
//cout << "end!";
exit(0);
}