#include<bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<pair<ll, ll> > V, W; ll n, s, a, b; int m; cin>>n>>m>>s; for (int i = 0; i < m; ++i) { cin>>a>>b; V.push_back({a, b}); } sort(V.begin(), V.end()); W.push_back(V[0]); pair<ll, ll> p; for (int i = 1; i < m; ++i) { if (W.back().second + 1 == V[i].first) { W.back().second = V[i].second; } else { W.push_back(V[i]); } if (W.back().first <= s && W.back().second >= s) { p = W.back(); } } ll l = -1e13, r = 1e14; if (p.first - 1 > 0) l = p.first - 1; if (p.second + 1 <= n) r = p.second + 1; if (abs(s - l) <= abs(s - r)) { 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 | #include<bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<pair<ll, ll> > V, W; ll n, s, a, b; int m; cin>>n>>m>>s; for (int i = 0; i < m; ++i) { cin>>a>>b; V.push_back({a, b}); } sort(V.begin(), V.end()); W.push_back(V[0]); pair<ll, ll> p; for (int i = 1; i < m; ++i) { if (W.back().second + 1 == V[i].first) { W.back().second = V[i].second; } else { W.push_back(V[i]); } if (W.back().first <= s && W.back().second >= s) { p = W.back(); } } ll l = -1e13, r = 1e14; if (p.first - 1 > 0) l = p.first - 1; if (p.second + 1 <= n) r = p.second + 1; if (abs(s - l) <= abs(s - r)) { cout << l <<'\n'; } else cout << r << '\n'; } |