#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define MP make_pair
#define FT first
#define SD second
#define INT(x) int x; scanf("%d", &x)
#define LLG(x) LL x; scanf("%lld", &x)
typedef long long LL;
int main() {
LLG(n);
INT(m);
LLG(s);
vector<pair<LL, LL> > v;
REP(i,m) {
LLG(l);
LLG(r);
v.PB(MP(l, r));
}
sort(ALL(v));
int x = lower_bound(ALL(v), MP(s, n + 1)) - v.begin() - 1;
int a = x, b = x;
while (a && v[a - 1].SD == v[a].FT - 1) --a;
while (b < m - 1 && v[b + 1].FT == v[b].SD + 1) ++b;
LL aa = v[a].FT - 1, bb = v[b].SD + 1;
if (aa < 1) printf("%lld\n", bb);
else if (bb > n) printf("%lld\n", aa);
else if (bb - s < s - aa) printf("%lld\n", bb);
else printf("%lld\n", aa);
}
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 | #include <algorithm> #include <cstdio> #include <utility> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) #define LLG(x) LL x; scanf("%lld", &x) typedef long long LL; int main() { LLG(n); INT(m); LLG(s); vector<pair<LL, LL> > v; REP(i,m) { LLG(l); LLG(r); v.PB(MP(l, r)); } sort(ALL(v)); int x = lower_bound(ALL(v), MP(s, n + 1)) - v.begin() - 1; int a = x, b = x; while (a && v[a - 1].SD == v[a].FT - 1) --a; while (b < m - 1 && v[b + 1].FT == v[b].SD + 1) ++b; LL aa = v[a].FT - 1, bb = v[b].SD + 1; if (aa < 1) printf("%lld\n", bb); else if (bb > n) printf("%lld\n", aa); else if (bb - s < s - aa) printf("%lld\n", bb); else printf("%lld\n", aa); } |
English