#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#else
#define debug(...) 228
#endif
#define pb push_back
typedef long long ll;
typedef long double ld;
ll n;
int m;
const int maxN = 2005;
ll l[maxN], r[maxN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
ll s;
cin >> n >> m >> s;
vector<ll> cands{s};
for (int i = 1; i <= m; i++) {
cin >> l[i] >> r[i];
if (l[i] > 1) cands.emplace_back(l[i] - 1);
if (r[i] + 1 <= n) cands.emplace_back(r[i] + 1);
}
pair<ll,ll> best = {1e18, 1e18};
for (auto& it : cands) {
ll who = it;
bool ok = true;
for (int j = 1; j <= m; j++) {
if (l[j] <= who && who <= r[j]) {
ok = false;
break;
}
}
if (ok) {
best = min(best, make_pair(abs(s - who), who));
}
}
cout << best.second;
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 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; ll n; int m; const int maxN = 2005; ll l[maxN], r[maxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif ll s; cin >> n >> m >> s; vector<ll> cands{s}; for (int i = 1; i <= m; i++) { cin >> l[i] >> r[i]; if (l[i] > 1) cands.emplace_back(l[i] - 1); if (r[i] + 1 <= n) cands.emplace_back(r[i] + 1); } pair<ll,ll> best = {1e18, 1e18}; for (auto& it : cands) { ll who = it; bool ok = true; for (int j = 1; j <= m; j++) { if (l[j] <= who && who <= r[j]) { ok = false; break; } } if (ok) { best = min(best, make_pair(abs(s - who), who)); } } cout << best.second; return 0; } |
English