#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";}
auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";}
#define debug(X...) cerr<<"["#X"]: ", [](auto...$) {((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
#define int long long
const int INF = 1e18+7;
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define eb emplace_back
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n, m, s;
cin>>n>>m>>s;
vector<pair<int, int> > segs;
while(m--)
{
int l, r;
cin>>l>>r;
segs.eb(l, r);
}
sort(segs.begin(), segs.end());
vector<pair<int, int> > full;
for(auto it = segs.begin(); next(it) != segs.end();++it)
{
if(it->se + 1 == next(it)->fi)
next(it)->fi = it->fi;
else
full.eb(*it);
}
full.eb(segs.back());
debug(full);
auto it = upper_bound(full.begin(), full.end(), mp(s, INF));
it--;
int a = (it->fi-1 != 0 ? it->fi-1 : INF),
b = (it->se+1 != n+1 ? it->se+1 : INF);
if(abs(a-s) <= abs(b-s))
cout<<a;
else
cout<<b;
}
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> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X...) cerr<<"["#X"]: ", [](auto...$) {((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif #define int long long const int INF = 1e18+7; #define mp(x, y) make_pair(x, y) #define fi first #define se second #define eb emplace_back int32_t main() { cin.tie(0)->sync_with_stdio(0); int n, m, s; cin>>n>>m>>s; vector<pair<int, int> > segs; while(m--) { int l, r; cin>>l>>r; segs.eb(l, r); } sort(segs.begin(), segs.end()); vector<pair<int, int> > full; for(auto it = segs.begin(); next(it) != segs.end();++it) { if(it->se + 1 == next(it)->fi) next(it)->fi = it->fi; else full.eb(*it); } full.eb(segs.back()); debug(full); auto it = upper_bound(full.begin(), full.end(), mp(s, INF)); it--; int a = (it->fi-1 != 0 ? it->fi-1 : INF), b = (it->se+1 != n+1 ? it->se+1 : INF); if(abs(a-s) <= abs(b-s)) cout<<a; else cout<<b; } |
English