#include <bits/stdc++.h> #define ll long long #define range(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll n, s; int m; cin >> n >> m >> s; vector<pair<ll, ll>> in(m); range(i, m) cin >> in[i].first >> in[i].second; sort(in.begin(), in.end()); vector<pair<ll, ll>> v; for (int i = 1; i < m; i++) { if (in[i-1].second + 1 == in[i].first) in[i].first = in[i-1].first; else v.push_back(in[i-1]); } v.push_back(in[m-1]); for (int index = 0; index < v.size(); index++) { if (s > v[index].second) continue; if (s < v[index].first) { cout << s << "\n"; return 0; } ll top = v[index].second + 1; ll bot = v[index].first - 1; if (bot == 0) { cout << top << "\n"; return 0; } if (top == n+1) { cout << bot << "\n"; return 0; } if (s - bot <= top - s) { cout << bot << "\n"; } else { cout << top << "\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 | #include <bits/stdc++.h> #define ll long long #define range(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll n, s; int m; cin >> n >> m >> s; vector<pair<ll, ll>> in(m); range(i, m) cin >> in[i].first >> in[i].second; sort(in.begin(), in.end()); vector<pair<ll, ll>> v; for (int i = 1; i < m; i++) { if (in[i-1].second + 1 == in[i].first) in[i].first = in[i-1].first; else v.push_back(in[i-1]); } v.push_back(in[m-1]); for (int index = 0; index < v.size(); index++) { if (s > v[index].second) continue; if (s < v[index].first) { cout << s << "\n"; return 0; } ll top = v[index].second + 1; ll bot = v[index].first - 1; if (bot == 0) { cout << top << "\n"; return 0; } if (top == n+1) { cout << bot << "\n"; return 0; } if (s - bot <= top - s) { cout << bot << "\n"; } else { cout << top << "\n"; } return 0; } } |