#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n,m,s;
cin>>n>>m>>s;
vector<pair<long long,long long>> V(m);
for(auto & [a,b] : V)
cin>>a>>b;
sort(V.begin(),V.end());
long long s1 = s-1;
if(s1<1)
s1=-1;
long long s2 = s+1;
if(s2>n)
s2=-1;
for(auto [l,r] : V)
{
if(l<=s1 && s1 <=r)
s1 = -1;
if(l<=s2 && s2 <=r)
s2 = -1;
}
if(s1!=-1)
{
cout<<s1<<'\n';
return 0;
}
if(s2!=-1)
{
cout<<s2<<'\n';
return 0;
}
long long p= -1;
long long o = n+1000;
for(long long i = 0 ;i < m ;i ++)
{
long long pot = V[i].first-1;
if(pot<1)
continue;
if(i > 0)
{
if(V[i-1].second >= pot)
continue;
}
if(abs(s - pot)<o)
{
o = abs(s-pot);
p=pot;
}
}
for(long long i = 0; i < m; i ++)
{
long long pot = V[i].second+1;
if(pot>n)
continue;
if(i<m-1)
{
if(V[i+1].first <= pot)
continue;
}
if(abs(s - pot)<o)
{
o = abs(s-pot);
p=pot;
}
}
cout<<p;
}
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n,m,s; cin>>n>>m>>s; vector<pair<long long,long long>> V(m); for(auto & [a,b] : V) cin>>a>>b; sort(V.begin(),V.end()); long long s1 = s-1; if(s1<1) s1=-1; long long s2 = s+1; if(s2>n) s2=-1; for(auto [l,r] : V) { if(l<=s1 && s1 <=r) s1 = -1; if(l<=s2 && s2 <=r) s2 = -1; } if(s1!=-1) { cout<<s1<<'\n'; return 0; } if(s2!=-1) { cout<<s2<<'\n'; return 0; } long long p= -1; long long o = n+1000; for(long long i = 0 ;i < m ;i ++) { long long pot = V[i].first-1; if(pot<1) continue; if(i > 0) { if(V[i-1].second >= pot) continue; } if(abs(s - pot)<o) { o = abs(s-pot); p=pot; } } for(long long i = 0; i < m; i ++) { long long pot = V[i].second+1; if(pot>n) continue; if(i<m-1) { if(V[i+1].first <= pot) continue; } if(abs(s - pot)<o) { o = abs(s-pot); p=pot; } } cout<<p; } |
English