#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
cin.tie(0)->sync_with_stdio(0);
LL n,m,s;
cin>>n>>m>>s;
vector<pair<LL,LL>> tab(m);
for(LL i=0;i<m;i++)cin>>tab[i].first>>tab[i].second;
sort(tab.begin(),tab.end());
LL start = -1;
for(LL i=0;i<m;i++){
if(s>=tab[i].first && s<=tab[i].second)start=i;
}
if(start==-1){
cout<<s<<endl;
return 0;
}
LL ans1=tab[0].first-1;
for(LL i=start-1;i>=0;i--){
if(tab[i+1].first-1>tab[i].second){
ans1=tab[i+1].first-1;
break;
}
}
LL ans2=tab[m-1].second+1;
for(LL i=start+1;i<m;i++){
if(tab[i-1].second+1<tab[i].first){
ans2=tab[i-1].second+1;
break;
}
}
if(ans1<1){
cout<<ans2<<endl;
}else if(ans2>n){
cout<<ans1<<endl;
}else{
if(abs(s-ans1)<=abs(s-ans2))cout<<ans1<<endl;
else cout<<ans2<<endl;
}
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 47 48 49 50 | #include <bits/stdc++.h> using namespace std; typedef long long LL; int main(){ cin.tie(0)->sync_with_stdio(0); LL n,m,s; cin>>n>>m>>s; vector<pair<LL,LL>> tab(m); for(LL i=0;i<m;i++)cin>>tab[i].first>>tab[i].second; sort(tab.begin(),tab.end()); LL start = -1; for(LL i=0;i<m;i++){ if(s>=tab[i].first && s<=tab[i].second)start=i; } if(start==-1){ cout<<s<<endl; return 0; } LL ans1=tab[0].first-1; for(LL i=start-1;i>=0;i--){ if(tab[i+1].first-1>tab[i].second){ ans1=tab[i+1].first-1; break; } } LL ans2=tab[m-1].second+1; for(LL i=start+1;i<m;i++){ if(tab[i-1].second+1<tab[i].first){ ans2=tab[i-1].second+1; break; } } if(ans1<1){ cout<<ans2<<endl; }else if(ans2>n){ cout<<ans1<<endl; }else{ if(abs(s-ans1)<=abs(s-ans2))cout<<ans1<<endl; else cout<<ans2<<endl; } return 0; } |
English