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
#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long n, m, s;
	cin>>n>>m>>s;
	vector<pair<int, int>>p(m);
	for (int i=0; i<m; i++)
	{
		int a, b; cin>>a>>b;
		p[i] = {a, b};
	}
	sort(p.begin(), p.end());
	vector<pair<int, int>>pa;
	for (int i=0; i<m; i++)
	{
		int poc = p[i].first;
		int k = p[i].second;
		while (i+1 < n && p[i+1].first == k + 1)
		{
			k = p[i+1].second;
			i++;
		}
		//~ cout<<i<<" "<<poc<<" "<<k<<endl;
		if (poc <= s && k >= s)
		{
			int d1 = s-poc+1;
			int d2 = k-s+1;
			if (poc == 1 || (d2 < d1 && k+1 <= n)) cout<<k+1<<"\n";
			else cout<<poc-1<<"\n";
			return 0;
		}
	}
}