#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main()
{
using ull = unsigned long long;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ull n, m, s;
cin >> n >> m >> s;
vector<pair<ull, ull>> street(m, { 0, 0 });
for (int i = 0; i < m; i++) {
cin >> street[i].first >> street[i].second;
}
sort(street.begin(), street.end());
int si = 0;
for (int i = 0; i < m; i++) {
if (street[i].first <= s && street[i].second >= s) {
si = i;
break;
}
}
ull w, d = 1e13, lf = street[si].first, ls = street[si].second;
for (int i = si - 1; i >= 0; i--) {
if (lf - 1 > street[i].second) {
w = lf - 1;
d = s - lf + 1;
break;
} else {
lf = street[i].first;
}
}
if ((s - lf + 1) < d && lf - 1 > 0) {
w = lf - 1;
d = s - lf + 1;
}
for (int i = si + 1; i < m; i++) {
if (ls + 1 < street[i].first) {
if ((ls + 1 - s) < d) {
w = ls + 1;
}
break;
} else {
ls = street[i].second;
}
}
if ((ls + 1 - s) < d && (ls + 1) <= n) {
w = ls + 1;
}
cout << w << endl;
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 | #include <algorithm> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { using ull = unsigned long long; ios_base::sync_with_stdio(false); cin.tie(NULL); ull n, m, s; cin >> n >> m >> s; vector<pair<ull, ull>> street(m, { 0, 0 }); for (int i = 0; i < m; i++) { cin >> street[i].first >> street[i].second; } sort(street.begin(), street.end()); int si = 0; for (int i = 0; i < m; i++) { if (street[i].first <= s && street[i].second >= s) { si = i; break; } } ull w, d = 1e13, lf = street[si].first, ls = street[si].second; for (int i = si - 1; i >= 0; i--) { if (lf - 1 > street[i].second) { w = lf - 1; d = s - lf + 1; break; } else { lf = street[i].first; } } if ((s - lf + 1) < d && lf - 1 > 0) { w = lf - 1; d = s - lf + 1; } for (int i = si + 1; i < m; i++) { if (ls + 1 < street[i].first) { if ((ls + 1 - s) < d) { w = ls + 1; } break; } else { ls = street[i].second; } } if ((ls + 1 - s) < d && (ls + 1) <= n) { w = ls + 1; } cout << w << endl; return 0; } |
English