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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
	LL L;
	int cnt;
	LL s;
	cin >> L >> cnt >> s;
	vector<pair<LL,LL>> v;
	v.emplace_back(0LL, 0LL);
	for (int i = 0; i < cnt; i++) {
		LL x, y;
		cin >> x >> y;
		v.emplace_back(x, y);
	}
	v.emplace_back(L + 1, L + 1);
	sort(v.begin(), v.end());
	pair<LL,LL> answer = {L+1, L+1};
	auto consider = [&](LL x) {
		answer = min(answer, make_pair(abs(x - s), x));
	};
	for (int i = 1; i < (int) v.size() - 1; i++) {
		if (v[i-1].second + 1 != v[i].first) {
			consider(v[i].first - 1);
		}
		if (v[i].second + 1 != v[i+1].first) {
			consider(v[i].second + 1);
		}
	}
	cout << answer.second << "\n";
}