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
#include <bits/stdc++.h>

using namespace std;

int main(){
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	long long n, s;
	int m;
	cin >> n >> m >> s;
	
	vector<pair<long long, long long> > v(m);
	for(int i = 0; i < m; ++i)
		cin >> v[i].first >> v[i].second;
		
	sort(v.begin(), v.end());
	vector<pair<long long, long long> > t;
	long long a = v[0].first;
	long long b = v[0].second;
	
	for(int i = 1; i < m; ++i){
		if(v[i].first - 1 == b) b = v[i].second;
		else{
			t.push_back({a,b});
			a = v[i].first;
			b = v[i].second;
		}
	}
	t.push_back({a,b});
	
	int ind = 0;
	for(int i = 0; i < (int)t.size(); ++i){
		if(s <= t[i].second){
			ind = i;
			break;
		}
	}
	
	if(ind == 0 && t[ind].first == 1)
		cout << t[ind].second + 1 << "\n";
	else if(ind + 1 == (int)t.size() && t[ind].second == n)
		cout << t[ind].first - 1 << "\n";
	else{
		a = s - (t[ind].first - 1);
		b = t[ind].second + 1 - s;
		if(a <= b)
			cout << t[ind].first - 1 << "\n";
		else
			cout << t[ind].second + 1 << "\n";
	}
}