// Author: Olaf Surgut (surgutti)
// Created on 11-03-2025 15:31:45
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(x) begin(x),end(x)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
auto& operator<<(auto &o, pair<auto, auto> p) {
return o << "(" << p.st << ", " << p.nd << ")";}
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
o << "{"; int i=0; for (auto e : x) o << ","+!i++ << e;
return o << "}";}
#ifdef LOCAL
#define debug(x...) cerr << "[" #x "]: ", [](auto...$) { \
((cerr << $ << "; "),...) << endl; }(x)
#else
#define debug(...)
#endif
#define rep(i,a,b) for(int i = a; i < (b); i++)
using pii = pair<int, int>;
using vi = vector<int>;
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, m, s;
cin >> n >> m >> s;
vector<pii> seg(m);
for (auto& [l, r] : seg)
cin >> l >> r;
sort(all(seg));
vector<pii> stk;
for (auto [l, r] : seg) {
debug(stk, l, r);
while (sz(stk) > 0 && l <= stk.back().nd + 1) {
l = min(l, stk.back().st);
r = max(r, stk.back().nd);
stk.pop_back();
}
stk.eb(l, r);
}
debug(stk);
auto [l, r] = *prev(upper_bound(all(stk), make_pair(s + 1, s)));
debug(l, r);
l--, r++;
if (l < 1) l = r;
if (r > n) r = l;
debug(l, r);
if (abs(s - l) <= abs(s - r))
cout << l << '\n';
else
cout << r << '\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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | // Author: Olaf Surgut (surgutti) // Created on 11-03-2025 15:31:45 #include "bits/stdc++.h" using namespace std; #define int long long #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define all(x) begin(x),end(x) #define FOR(i,l,r) for(int i=(l);i<=(r);i++) #define ROF(i,r,l) for(int i=(r);i>=(l);i--) auto& operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")";} auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for (auto e : x) o << ","+!i++ << e; return o << "}";} #ifdef LOCAL #define debug(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define debug(...) #endif #define rep(i,a,b) for(int i = a; i < (b); i++) using pii = pair<int, int>; using vi = vector<int>; signed main() { cin.tie(0)->sync_with_stdio(0); int n, m, s; cin >> n >> m >> s; vector<pii> seg(m); for (auto& [l, r] : seg) cin >> l >> r; sort(all(seg)); vector<pii> stk; for (auto [l, r] : seg) { debug(stk, l, r); while (sz(stk) > 0 && l <= stk.back().nd + 1) { l = min(l, stk.back().st); r = max(r, stk.back().nd); stk.pop_back(); } stk.eb(l, r); } debug(stk); auto [l, r] = *prev(upper_bound(all(stk), make_pair(s + 1, s))); debug(l, r); l--, r++; if (l < 1) l = r; if (r > n) r = l; debug(l, r); if (abs(s - l) <= abs(s - r)) cout << l << '\n'; else cout << r << '\n'; return 0; } |
English