#include <bits/stdc++.h>
using namespace std;
#define int long long
struct xd{
int l;
int r;
};
vector <xd> tab;
bool cmp(xd a, xd b){
return a.r<b.l;
}
int32_t main()
{
int n, m, i, j, a, b, s, k=-1, l=-1, r=-1;
cin >> n >> m >> s;
for(i=0; i<m; i++){
cin >> a >> b;
tab.push_back({a, b});
}
tab.push_back({-1, -1});
tab.push_back({n+2, n+2});
m+=2;
sort(tab.begin(), tab.end(), cmp);
for(i=0; i<m; i++){
if(tab[i].l<=s && s<=tab[i].r){
k=i;
break;
}
}
if(k==-1){
cout << s << '\n';
return 0;
}
for(i=k-1; i>=0; i--){
if(tab[i].r!=tab[i+1].l-1){
l=tab[i+1].l-1;
break;
}
}
for(i=k+1; i<m; i++){
if(tab[i].l!=tab[i-1].r+1){
r=tab[i-1].r+1;
break;
}
}
if(l==0){
cout << r << '\n';
return 0;
}
if(r==n+1){
cout << l << '\n';
return 0;
}
if(s-l<=r-s){
cout << l << '\n';
return 0;
}
cout << r << '\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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <bits/stdc++.h> using namespace std; #define int long long struct xd{ int l; int r; }; vector <xd> tab; bool cmp(xd a, xd b){ return a.r<b.l; } int32_t main() { int n, m, i, j, a, b, s, k=-1, l=-1, r=-1; cin >> n >> m >> s; for(i=0; i<m; i++){ cin >> a >> b; tab.push_back({a, b}); } tab.push_back({-1, -1}); tab.push_back({n+2, n+2}); m+=2; sort(tab.begin(), tab.end(), cmp); for(i=0; i<m; i++){ if(tab[i].l<=s && s<=tab[i].r){ k=i; break; } } if(k==-1){ cout << s << '\n'; return 0; } for(i=k-1; i>=0; i--){ if(tab[i].r!=tab[i+1].l-1){ l=tab[i+1].l-1; break; } } for(i=k+1; i<m; i++){ if(tab[i].l!=tab[i-1].r+1){ r=tab[i-1].r+1; break; } } if(l==0){ cout << r << '\n'; return 0; } if(r==n+1){ cout << l << '\n'; return 0; } if(s-l<=r-s){ cout << l << '\n'; return 0; } cout << r << '\n'; return 0; } |
English