#include <cstdio> #include <map> using namespace std; int main(void) { long long n, s; int m; map<long long, long long> occ; scanf("%lld%d%lld", &n, &m, &s); for (int i = 0; i < m; i++) { long long a, b; scanf("%lld%lld", &a, &b); occ[a] = b; } for (map<long long, long long>::iterator i = occ.begin(); i != occ.end(); ++i) { while (1) { map<long long, long long>::iterator j = i; if (++j != occ.end()) { if (j->first == i->second + 1) { i->second = j->second; occ.erase(j); continue; } } break; } } map<long long, long long>::iterator f = occ.upper_bound(s); --f; long long l = f->first - 1; long long r = f->second + 1; long long p; if (s - l <= r - s) p = l; else p = r; if (p < 1) p = r; if (p > n) p = l; printf("%lld\n", p); 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 | #include <cstdio> #include <map> using namespace std; int main(void) { long long n, s; int m; map<long long, long long> occ; scanf("%lld%d%lld", &n, &m, &s); for (int i = 0; i < m; i++) { long long a, b; scanf("%lld%lld", &a, &b); occ[a] = b; } for (map<long long, long long>::iterator i = occ.begin(); i != occ.end(); ++i) { while (1) { map<long long, long long>::iterator j = i; if (++j != occ.end()) { if (j->first == i->second + 1) { i->second = j->second; occ.erase(j); continue; } } break; } } map<long long, long long>::iterator f = occ.upper_bound(s); --f; long long l = f->first - 1; long long r = f->second + 1; long long p; if (s - l <= r - s) p = l; else p = r; if (p < 1) p = r; if (p > n) p = l; printf("%lld\n", p); return 0; } |