/// UH Top
#include <bits/stdc++.h>
#define db(x) cerr << #x << ':' << (x) << '\n';
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define int ll
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// typedef __int128_t int128;
typedef pair<ll, ll> pii;
typedef pair<ld, ll> pdi;
typedef pair<ld, ld> pdd;
typedef pair<ld, pdd> pdp;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<string, string> pss;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
// typedef complex<ld> point;
// typedef vector<point> polygon;
typedef vector<ll> vi;
// typedef pair<point, int> ppi;
#define prec(n) \
cout.precision(n); \
cout << fixed
const ll mod = (1e9 + 7);
const ld eps = (1e-9);
const ll oo = (ll)(1e9 + 5);
#define pi acos(-1)
#define MAXN (ll)(1e6 + 5)
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, s;
cin >> n >> m >> s;
vector<pii> a(m);
bool isIn = 0;
vector<int> toTry;
for (int i = 0; i < m; i++) {
cin >> a[i].first >> a[i].second;
if (a[i].first <= s && s <= a[i].second) {
isIn = 1;
}
if (a[i].first > 1)
toTry.push_back(a[i].first - 1);
if (a[i].second < n)
toTry.push_back(a[i].second + 1);
}
if (!isIn) {
cout << s << "\n";
return 0;
}
sort(all(toTry));
int best = 2 * n;
for (auto x : toTry) {
bool ok = 1;
for (auto p : a) {
if (p.first <= x && x <= p.second) {
ok = 0;
break;
}
}
if (!ok)
continue;
if (abs(best - s) > abs(x - s)) {
best = x;
}
}
cout << best << "\n";
return 0;
}
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /// UH Top #include <bits/stdc++.h> #define db(x) cerr << #x << ':' << (x) << '\n'; #define all(v) (v).begin(), (v).end() #define allr(v) (v).rbegin(), (v).rend() #define int ll using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; // typedef __int128_t int128; typedef pair<ll, ll> pii; typedef pair<ld, ll> pdi; typedef pair<ld, ld> pdd; typedef pair<ld, pdd> pdp; typedef pair<string, ll> psi; typedef pair<ll, string> pls; typedef pair<string, string> pss; typedef pair<ll, pii> pip; typedef pair<pii, pii> ppp; // typedef complex<ld> point; // typedef vector<point> polygon; typedef vector<ll> vi; // typedef pair<point, int> ppi; #define prec(n) \ cout.precision(n); \ cout << fixed const ll mod = (1e9 + 7); const ld eps = (1e-9); const ll oo = (ll)(1e9 + 5); #define pi acos(-1) #define MAXN (ll)(1e6 + 5) int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, s; cin >> n >> m >> s; vector<pii> a(m); bool isIn = 0; vector<int> toTry; for (int i = 0; i < m; i++) { cin >> a[i].first >> a[i].second; if (a[i].first <= s && s <= a[i].second) { isIn = 1; } if (a[i].first > 1) toTry.push_back(a[i].first - 1); if (a[i].second < n) toTry.push_back(a[i].second + 1); } if (!isIn) { cout << s << "\n"; return 0; } sort(all(toTry)); int best = 2 * n; for (auto x : toTry) { bool ok = 1; for (auto p : a) { if (p.first <= x && x <= p.second) { ok = 0; break; } } if (!ok) continue; if (abs(best - s) > abs(x - s)) { best = x; } } cout << best << "\n"; return 0; } |
English