//fast
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(x) x.begin(),x.end()
#define rep(n) for (int i = 0 ; i<n ; i++)
#define pb push_back
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,m,s;
cin >> n >> m >> s;
pair<ll,ll> zaj[m+2];
rep(m){
cin >> zaj[i].first >> zaj[i].second;
}
zaj[m] = {0,0};
zaj[m+1] = {n+1,n+1};
m+=2;
sort(zaj,zaj+m);
int x;
rep(m){
if (zaj[i].first<=s && s<=zaj[i].second) x = i;
}
ll l = -1e16;
ll r = 1e16;
for (int i = x-1 ; i>=0 ; i--){
if (zaj[i].second+1!=zaj[i+1].first){
l = zaj[i+1].first-1;
break;
}
}
for (int i = x ; i<m-1 ; i++){
if (zaj[i].second+1!=zaj[i+1].first){
r = zaj[i].second+1;
break;
}
}
if (s-l<=r-s) cout << l << '\n';
else cout << r << '\n';
}
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 | //fast #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define all(x) x.begin(),x.end() #define rep(n) for (int i = 0 ; i<n ; i++) #define pb push_back int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll n,m,s; cin >> n >> m >> s; pair<ll,ll> zaj[m+2]; rep(m){ cin >> zaj[i].first >> zaj[i].second; } zaj[m] = {0,0}; zaj[m+1] = {n+1,n+1}; m+=2; sort(zaj,zaj+m); int x; rep(m){ if (zaj[i].first<=s && s<=zaj[i].second) x = i; } ll l = -1e16; ll r = 1e16; for (int i = x-1 ; i>=0 ; i--){ if (zaj[i].second+1!=zaj[i+1].first){ l = zaj[i+1].first-1; break; } } for (int i = x ; i<m-1 ; i++){ if (zaj[i].second+1!=zaj[i+1].first){ r = zaj[i].second+1; break; } } if (s-l<=r-s) cout << l << '\n'; else cout << r << '\n'; } |
English