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
#include <bits/stdc++.h>
using namespace std;
const int SIZE=1e4+10;
pair<long long,long long> tab[SIZE];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	long long N,M,s,a,b;
	cin>>N>>M>>s;

	for(int i=0;i<M;i++){
		cin>>a>>b;
		tab[i]={a,b};
	}

	sort(tab,tab+M);

	for(int i=0;i<M;i++){
		if(tab[i].first>s||tab[i].second<s){continue;}
		a=i;
		b=i;
		while(a&&tab[a-1].second+1ll==tab[a].first){a=a-1;}
		while(b+1<M&&tab[b+1].first-1ll==tab[b].second){b=b+1;}

		if(tab[a].first<=1ll){
            cout<<tab[b].second+1ll;
        }else if(tab[b].second>=N){
            cout<<tab[a].first-1ll;
        }else if(s-tab[a].first<=tab[b].second-s){
            cout<<tab[a].first-1ll;
        }else{
            cout<<tab[b].second+1ll;
        }
        break;
	}

return 0;}