#include <iostream>
#include <vector>
#include <set>
#include <utility>
#include <algorithm>
#include <cstdint>
using namespace std;
#define int int64_t
int32_t main() {
int n, m, s;
cin >> n >> m >> s;
vector<pair<int, int>> in(m);
for(int i = 0; i < m; i++) {
cin >> in[i].first >> in[i].second;
}
sort(in.begin(), in.end());
vector<pair<int, int>> a;
for(auto [l, r] : in) {
if(!a.empty() && a.back().second + 1 == l) {
int L = a.back().first;
a.pop_back();
a.push_back({L, r});
} else {
a.push_back({l, r});
}
}
int left = -1, right = -1;
for(auto [l, r] : a) {
if(s >= l && s <= r) {
left = l - 1;
right = r + 1;
}
}
int op1 = abs(s - right) + 1, op2 = abs(s - left) + 1;
if(left < 1 && right <= n) {
cout << right;
} else if(left >= 1 && right > n){
cout << left;
} else {
if(op1 < op2) {
cout << right;
} else {
cout << left;
}
}
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 | #include <iostream> #include <vector> #include <set> #include <utility> #include <algorithm> #include <cstdint> using namespace std; #define int int64_t int32_t main() { int n, m, s; cin >> n >> m >> s; vector<pair<int, int>> in(m); for(int i = 0; i < m; i++) { cin >> in[i].first >> in[i].second; } sort(in.begin(), in.end()); vector<pair<int, int>> a; for(auto [l, r] : in) { if(!a.empty() && a.back().second + 1 == l) { int L = a.back().first; a.pop_back(); a.push_back({L, r}); } else { a.push_back({l, r}); } } int left = -1, right = -1; for(auto [l, r] : a) { if(s >= l && s <= r) { left = l - 1; right = r + 1; } } int op1 = abs(s - right) + 1, op2 = abs(s - left) + 1; if(left < 1 && right <= n) { cout << right; } else if(left >= 1 && right > n){ cout << left; } else { if(op1 < op2) { cout << right; } else { cout << left; } } return 0; } |
English