#include <bits/stdc++.h> using namespace std; using ll=long long; using pr=pair<ll, ll>; #define nl '\n' #define st first #define nd second #define sz(x) (int)(x).size() #define each(a, b) for(const auto& a:b) #define rep(a, b) for(int a=0; a<(b); a++) #define coz(x) cerr<<"("<<__LINE__<<") "<<(#x)<<": "<<(x)<<'\n' #define cot(x, l, n) cerr<<"("<<__LINE__<<") "<<(#x)<<": "; \ for(int i=l; i<l+n; i++) { cerr<<x[i]<<' '; } cerr<<'\n' constexpr int M=1e3+6; pr lr[M]; set<ll> s; ll n,a; int m; ll dw=1e13; ll bw=0; void check(ll b) { bool ok=((bool)(s.find(b)==s.end())&(bool)(b>=1)&(bool)(b<=n)); if(!ok) { return; } ll d=abs(a-b); // cerr<<b<<' '<<d<<nl; if(d<dw) { dw=d; bw=b; } } int main() { cin.tie(nullptr)->sync_with_stdio(false); cin>>n>>m>>a; rep(i, m) { cin>>lr[i].st>>lr[i].nd; } sort(lr, lr+m); rep(i, m) { s.insert(lr[i].st); s.insert(lr[i].nd); } rep(i, m) { check(lr[i].st-1); check(lr[i].nd+1); } cout<<bw<<nl; }
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 | #include <bits/stdc++.h> using namespace std; using ll=long long; using pr=pair<ll, ll>; #define nl '\n' #define st first #define nd second #define sz(x) (int)(x).size() #define each(a, b) for(const auto& a:b) #define rep(a, b) for(int a=0; a<(b); a++) #define coz(x) cerr<<"("<<__LINE__<<") "<<(#x)<<": "<<(x)<<'\n' #define cot(x, l, n) cerr<<"("<<__LINE__<<") "<<(#x)<<": "; \ for(int i=l; i<l+n; i++) { cerr<<x[i]<<' '; } cerr<<'\n' constexpr int M=1e3+6; pr lr[M]; set<ll> s; ll n,a; int m; ll dw=1e13; ll bw=0; void check(ll b) { bool ok=((bool)(s.find(b)==s.end())&(bool)(b>=1)&(bool)(b<=n)); if(!ok) { return; } ll d=abs(a-b); // cerr<<b<<' '<<d<<nl; if(d<dw) { dw=d; bw=b; } } int main() { cin.tie(nullptr)->sync_with_stdio(false); cin>>n>>m>>a; rep(i, m) { cin>>lr[i].st>>lr[i].nd; } sort(lr, lr+m); rep(i, m) { s.insert(lr[i].st); s.insert(lr[i].nd); } rep(i, m) { check(lr[i].st-1); check(lr[i].nd+1); } cout<<bw<<nl; } |