#include <bits/stdc++.h> using namespace std; typedef long long ll; set<pair<ll,ll>> przedz; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, m, s; cin >> n >> m >> s; ll a, b; ll preva = -1; ll prevb = -1; vector<pair<ll,ll>> wej; for(int i = 1; i <= m; ++i){ cin >> a >> b; wej.push_back({a,b}); } sort(wej.begin(),wej.end()); for(int i = 1; i <= m; ++i){ a = wej[i-1].first; b = wej[i-1].second; if(prevb == a-1){ przedz.erase({preva, prevb}); prevb = b; przedz.insert({preva, prevb}); } else{ przedz.insert({a,b}); preva = a; prevb = b; } } auto it = przedz.upper_bound({s, 1e13}); --it; if(it->first == 1){ cout << it->second + 1 << "\n"; } else if(it->second == n){ cout << it->first - 1 << "\n"; } else{ ll i1 = it->first - 1; if(abs(s - i1) > abs(s - (it->second + 1))){ i1 = it->second + 1; } cout << i1 << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; set<pair<ll,ll>> przedz; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, m, s; cin >> n >> m >> s; ll a, b; ll preva = -1; ll prevb = -1; vector<pair<ll,ll>> wej; for(int i = 1; i <= m; ++i){ cin >> a >> b; wej.push_back({a,b}); } sort(wej.begin(),wej.end()); for(int i = 1; i <= m; ++i){ a = wej[i-1].first; b = wej[i-1].second; if(prevb == a-1){ przedz.erase({preva, prevb}); prevb = b; przedz.insert({preva, prevb}); } else{ przedz.insert({a,b}); preva = a; prevb = b; } } auto it = przedz.upper_bound({s, 1e13}); --it; if(it->first == 1){ cout << it->second + 1 << "\n"; } else if(it->second == n){ cout << it->first - 1 << "\n"; } else{ ll i1 = it->first - 1; if(abs(s - i1) > abs(s - (it->second + 1))){ i1 = it->second + 1; } cout << i1 << "\n"; } return 0; } |