#include <bits/stdc++.h>
using namespace std;
int main(){
long long n,m,s,x,y;
cin>>n>>m>>s;
pair<long long,long long> tab[m];
for(int i=0;i<m;i++){
cin>>tab[i].first>>tab[i].second;
}
sort(tab,tab+m);
/*
for(int i=0;i<m;i++){
cout<<"<"<<tab[i].first<<","<<tab[i].second<<"> ";
}
*/
vector<pair<long long,long long> > kol;
kol.push_back(tab[0]);
for(int i=1;i<m;i++){
if(tab[i].first==kol[kol.size()-1].second+1){
kol[kol.size()-1].second = tab[i].second;
}else{
kol.push_back(tab[i]);
}
}
/*
for(int i=0;i<kol.size();i++){
cout<<"<"<<kol[i].first<<","<<kol[i].second<<"> ";
}
*/
for(int i=0;i<kol.size();i++){
if(kol[i].first<=s && kol[i].second>=s){
x=kol[i].first-1;
y=kol[i].second+1;
}
}
if(s-x <= y-s){
if(x>0){
cout<<x;
}else{
cout<<y;
}
}else{
if(y<=n){
cout<<y;
}else{
cout<<x;
}
}
}
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 | #include <bits/stdc++.h> using namespace std; int main(){ long long n,m,s,x,y; cin>>n>>m>>s; pair<long long,long long> tab[m]; for(int i=0;i<m;i++){ cin>>tab[i].first>>tab[i].second; } sort(tab,tab+m); /* for(int i=0;i<m;i++){ cout<<"<"<<tab[i].first<<","<<tab[i].second<<"> "; } */ vector<pair<long long,long long> > kol; kol.push_back(tab[0]); for(int i=1;i<m;i++){ if(tab[i].first==kol[kol.size()-1].second+1){ kol[kol.size()-1].second = tab[i].second; }else{ kol.push_back(tab[i]); } } /* for(int i=0;i<kol.size();i++){ cout<<"<"<<kol[i].first<<","<<kol[i].second<<"> "; } */ for(int i=0;i<kol.size();i++){ if(kol[i].first<=s && kol[i].second>=s){ x=kol[i].first-1; y=kol[i].second+1; } } if(s-x <= y-s){ if(x>0){ cout<<x; }else{ cout<<y; } }else{ if(y<=n){ cout<<y; }else{ cout<<x; } } } |
English