#define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; mt19937 mt_rand(time(0)); const int M = 1e3 + 5; long long n, s; int m; vector<pair<long long, long long>> in; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { return a.first <= b.first; } int main() { scanf("%lld %d %lld", &n, &m, &s); while(m--) { long long a, b; scanf("%lld %lld", &a, &b); in.pb(mp(a, b)); } sort(in.begin(), in.end(), cmp); int num = 0; while(s > in[num].second) num++; int prev = num, next = num; while(prev != 0 && in[prev-1].second == in[prev].first - 1) prev--; while(next != in.size() - 1 && in[next+1].first == in[next].second + 1) next++; long long ans = 1e12 + 5; if(in[prev].first != 1) ans = s - in[prev].first + 1; if(in[next].second != n) ans = min(ans, in[next].second + 1 - s); if(in[prev].first != 1 && ans == s - in[prev].first + 1) printf("%lld", in[prev].first - 1); else printf("%lld", in[next].second + 1); 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 | #define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; mt19937 mt_rand(time(0)); const int M = 1e3 + 5; long long n, s; int m; vector<pair<long long, long long>> in; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { return a.first <= b.first; } int main() { scanf("%lld %d %lld", &n, &m, &s); while(m--) { long long a, b; scanf("%lld %lld", &a, &b); in.pb(mp(a, b)); } sort(in.begin(), in.end(), cmp); int num = 0; while(s > in[num].second) num++; int prev = num, next = num; while(prev != 0 && in[prev-1].second == in[prev].first - 1) prev--; while(next != in.size() - 1 && in[next+1].first == in[next].second + 1) next++; long long ans = 1e12 + 5; if(in[prev].first != 1) ans = s - in[prev].first + 1; if(in[next].second != n) ans = min(ans, in[next].second + 1 - s); if(in[prev].first != 1 && ans == s - in[prev].first + 1) printf("%lld", in[prev].first - 1); else printf("%lld", in[next].second + 1); return 0; } |