#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
ll L, S;
int N;
cin >> L >> N >> S;
S--;
vector<pair<ll, ll> > ivals;
for(int i = 0; i < N; i++){
ll l, r;
cin >> l >> r;
l--; r--;
ivals.push_back({l, r});
}
ivals.push_back({-1, -1});
ivals.push_back({L, L});
sort(ivals.begin(), ivals.end());
ll best = -1;
for(int a = 0; a + 1 < (int)ivals.size(); a++){
ll l = ivals[a].second + 1;
ll r = ivals[a+1].first - 1;
if(l <= r){
if(l <= S && S <= r){
best = S;
} else {
for(ll x : {l, r}){
if(best == -1 || (pair<ll, ll>{abs(x - S), x} < pair<ll, ll> {abs(best - S), best})){
best = x;
}
}
}
}
}
cout << (best+1) << '\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 | #include <bits/stdc++.h> using namespace std; using ll = int64_t; int main(){ ios_base::sync_with_stdio(false), cin.tie(nullptr); ll L, S; int N; cin >> L >> N >> S; S--; vector<pair<ll, ll> > ivals; for(int i = 0; i < N; i++){ ll l, r; cin >> l >> r; l--; r--; ivals.push_back({l, r}); } ivals.push_back({-1, -1}); ivals.push_back({L, L}); sort(ivals.begin(), ivals.end()); ll best = -1; for(int a = 0; a + 1 < (int)ivals.size(); a++){ ll l = ivals[a].second + 1; ll r = ivals[a+1].first - 1; if(l <= r){ if(l <= S && S <= r){ best = S; } else { for(ll x : {l, r}){ if(best == -1 || (pair<ll, ll>{abs(x - S), x} < pair<ll, ll> {abs(best - S), best})){ best = x; } } } } } cout << (best+1) << '\n'; } |
English