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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

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

	LL n, s;
	int m;
	cin >> n >> m >> s;
	vector<pair<LL, LL>> v(m);
	for (auto &[l, r] : v)
		cin >> l >> r;
	sort(v.begin(), v.end());
	vector<pair<LL, LL>> w;
	w.push_back(v[0]);
	for (int i = 1; i < m; i++)
	{
		if (w.back().second + 1 == v[i].first)
			w.back().second = v[i].second;
		else
			w.push_back(v[i]);
	}
	LL L, R;
	for (auto &[l, r] : w)
	{
		if (l <= s && s <= r)
			L = l, R = r;
	}
	L--; R++;
	if (L < 1)
		cout << R << "\n";
	else if (R > n)
		cout << L << "\n";
	else
	{
		LL x = abs(s-L);
		LL y = abs(s-R);
		cout << (x <= y ? L : R) << "\n";
	}
}