#include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using lll = __int128_t; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif ll n,s; int m; cin >> n >> m >> s; ll lst = 0; ll bst = -1e18; auto check = [&](ll x) { if(abs(s-bst) > abs(s-x)) bst = x; }; vector<pair<ll,ll>> p(m); for(auto &[l,r] : p) cin >> l >> r; sort(all(p)); for(auto [l,r] : p) { if(l-1!=lst) { check(lst+1); check(l-1); } lst = r; } if(lst != n) check(lst+1); cout << bst << '\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 | #include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using lll = __int128_t; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif ll n,s; int m; cin >> n >> m >> s; ll lst = 0; ll bst = -1e18; auto check = [&](ll x) { if(abs(s-bst) > abs(s-x)) bst = x; }; vector<pair<ll,ll>> p(m); for(auto &[l,r] : p) cin >> l >> r; sort(all(p)); for(auto [l,r] : p) { if(l-1!=lst) { check(lst+1); check(l-1); } lst = r; } if(lst != n) check(lst+1); cout << bst << '\n'; return 0; } |