#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back const ll INF = 2e18; void program() { ll n, m, s; cin >> n >> m >> s; vc<pll> a(m); for (pll &ai : a) cin >> ai.st >> ai.nd; sort(all(a)); vc<pll> tmp = {a[0]}; for (ll i = 1; i < m; i++) { if (tmp.back().nd + 1 == a[i].st) tmp.back().nd = a[i].nd; else tmp.pub(a[i]); } swap(tmp, a); ll l, r; for (pll &ai : a) if (ai.st <= s and s <= ai.nd) { l = ai.st; r = ai.nd; } if ((s - l <= r - s and l - 1 >= 1) or r + 1 > n) cout << l - 1 << "\n"; else cout << r + 1 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); program(); 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 <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back const ll INF = 2e18; void program() { ll n, m, s; cin >> n >> m >> s; vc<pll> a(m); for (pll &ai : a) cin >> ai.st >> ai.nd; sort(all(a)); vc<pll> tmp = {a[0]}; for (ll i = 1; i < m; i++) { if (tmp.back().nd + 1 == a[i].st) tmp.back().nd = a[i].nd; else tmp.pub(a[i]); } swap(tmp, a); ll l, r; for (pll &ai : a) if (ai.st <= s and s <= ai.nd) { l = ai.st; r = ai.nd; } if ((s - l <= r - s and l - 1 >= 1) or r + 1 > n) cout << l - 1 << "\n"; else cout << r + 1 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); program(); return 0; } |