#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
int main () {
int64_t n, s, a, b, left_h, right_h;
int m, i, pzs, left, right;
scanf("%lld %d %lld", &n, &m, &s);
std::vector<std::pair<int64_t, int64_t> > zaj;
for (i = 0; i < m; ++i) {
scanf("%lld %lld", &a, &b);
zaj.push_back(std::make_pair(a, b));
}
std::sort(zaj.begin(), zaj.end());
pzs = -1;
for (i = 0; i < m; ++i) {
if ((zaj[i].first <= s) && (zaj[i].second >= s)) {
pzs = i;
break;
}
}
left = pzs;
while (true) {
if ((left == 0) || (zaj[left - 1].second < zaj[left].first - 1)) {
break;
}
--left;
}
left_h = (zaj[left].first == 1) ? 2000000000000LL : (s - zaj[left].first + 1);
right = pzs;
while (true) {
if ((right == m - 1) || (zaj[right + 1].first > zaj[right].second + 1)) {
break;
}
++right;
}
right_h = (zaj[right].second == n) ? 2000000000000LL : (zaj[right].second - s + 1);
printf("%lld\n", left_h > right_h ? s + right_h : s - left_h);
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 | #include <cstdio> #include <cstdint> #include <vector> #include <algorithm> int main () { int64_t n, s, a, b, left_h, right_h; int m, i, pzs, left, right; scanf("%lld %d %lld", &n, &m, &s); std::vector<std::pair<int64_t, int64_t> > zaj; for (i = 0; i < m; ++i) { scanf("%lld %lld", &a, &b); zaj.push_back(std::make_pair(a, b)); } std::sort(zaj.begin(), zaj.end()); pzs = -1; for (i = 0; i < m; ++i) { if ((zaj[i].first <= s) && (zaj[i].second >= s)) { pzs = i; break; } } left = pzs; while (true) { if ((left == 0) || (zaj[left - 1].second < zaj[left].first - 1)) { break; } --left; } left_h = (zaj[left].first == 1) ? 2000000000000LL : (s - zaj[left].first + 1); right = pzs; while (true) { if ((right == m - 1) || (zaj[right + 1].first > zaj[right].second + 1)) { break; } ++right; } right_h = (zaj[right].second == n) ? 2000000000000LL : (zaj[right].second - s + 1); printf("%lld\n", left_h > right_h ? s + right_h : s - left_h); return 0; } |
English