#include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const ll oo = 1e18; int main(){ cin.tie(NULL),cin.sync_with_stdio(false); ll n,m,s; cin >> n >> m >> s; vector<ll> pts(m*2), tries(m*2); rep(i,0,m){ cin >> pts[i*2] >> pts[i*2+1]; ++pts[i*2+1]; tries[i*2] = pts[i*2]-1; tries[i*2+1] = pts[i*2+1]; } ll ans = 1e18; ll at = 0; sort(all(pts)); for (auto x : tries){ if (x >= 1 and x <= n){ int cnt = upper_bound(all(pts),x)-begin(pts); if (!(cnt&1)){ if (abs(s-x) < ans or (abs(s-x) == ans and x < at)){ ans = abs(s-x); at = x; } } } } cout << at << '\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" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const ll oo = 1e18; int main(){ cin.tie(NULL),cin.sync_with_stdio(false); ll n,m,s; cin >> n >> m >> s; vector<ll> pts(m*2), tries(m*2); rep(i,0,m){ cin >> pts[i*2] >> pts[i*2+1]; ++pts[i*2+1]; tries[i*2] = pts[i*2]-1; tries[i*2+1] = pts[i*2+1]; } ll ans = 1e18; ll at = 0; sort(all(pts)); for (auto x : tries){ if (x >= 1 and x <= n){ int cnt = upper_bound(all(pts),x)-begin(pts); if (!(cnt&1)){ if (abs(s-x) < ans or (abs(s-x) == ans and x < at)){ ans = abs(s-x); at = x; } } } } cout << at << '\n'; } |