#include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "debug.hpp" #else #define debug(...) (void)0 #endif namespace R = ranges; namespace V = views; auto ra(auto x, auto y) { return R::iota_view(x, y); } auto rae(auto x, auto y) { return ra(x, y + 1); } using i64 = int64_t; void solve() { i64 n, s; int m; cin >> n >> m >> s; vector<pair<i64, i64>> in(m); for (auto &[a, b] : in) { cin >> a >> b; } R::sort(in); vector<pair<i64, i64>> new_in{in[0]}; for (auto i : ra(1, m)) { auto [a, b] = in[i]; if (a - 1 == new_in.back().second) { new_in.back().second = b; } else { new_in.emplace_back(a, b); } } debug(new_in); pair<i64, i64> ans = {numeric_limits<i64>::max(), 0}; for (auto [a, b] : new_in) { if (a <= s and s <= b) { if (a != 1) { ans = min(ans, {s - a + 1, a - 1}); } if (b != n) { ans = min(ans, {b - s + 1, b + 1}); } } } cout << ans.second << '\n'; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int t = 1; // cin >> t; for (auto tc_n : ra(0, t)) { debug(tc_n); solve(); cout.flush(); } }
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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "debug.hpp" #else #define debug(...) (void)0 #endif namespace R = ranges; namespace V = views; auto ra(auto x, auto y) { return R::iota_view(x, y); } auto rae(auto x, auto y) { return ra(x, y + 1); } using i64 = int64_t; void solve() { i64 n, s; int m; cin >> n >> m >> s; vector<pair<i64, i64>> in(m); for (auto &[a, b] : in) { cin >> a >> b; } R::sort(in); vector<pair<i64, i64>> new_in{in[0]}; for (auto i : ra(1, m)) { auto [a, b] = in[i]; if (a - 1 == new_in.back().second) { new_in.back().second = b; } else { new_in.emplace_back(a, b); } } debug(new_in); pair<i64, i64> ans = {numeric_limits<i64>::max(), 0}; for (auto [a, b] : new_in) { if (a <= s and s <= b) { if (a != 1) { ans = min(ans, {s - a + 1, a - 1}); } if (b != n) { ans = min(ans, {b - s + 1, b + 1}); } } } cout << ans.second << '\n'; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int t = 1; // cin >> t; for (auto tc_n : ra(0, t)) { debug(tc_n); solve(); cout.flush(); } } |