#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
typedef pair <LL,LL> pll;
#define pb push_back
const int INF = 2147483647;
const int N = 500005;
LL n, s, a, b, res;
int m, i;
vector<pll> v;
void check(LL x) {
LL a = labs(res - s);
LL b = labs(x - s);
if (b < a) res = x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> s;
while (m--) {
cin >> a >> b;
v.pb(pll(a, b));
}
v.pb(pll(0, 0));
v.pb(pll(n + 1, n + 1));
sort(v.begin(), v.end());
res = INF * 1LL * INF;
for (i=1;i<(int)v.size() - 1;i++) {
if (v[i].first - 1 != v[i - 1].second) check(v[i].first - 1);
if (v[i].second + 1 != v[i + 1].first) check(v[i].second + 1);
}
cout << res << endl;
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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; typedef pair <LL,LL> pll; #define pb push_back const int INF = 2147483647; const int N = 500005; LL n, s, a, b, res; int m, i; vector<pll> v; void check(LL x) { LL a = labs(res - s); LL b = labs(x - s); if (b < a) res = x; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> s; while (m--) { cin >> a >> b; v.pb(pll(a, b)); } v.pb(pll(0, 0)); v.pb(pll(n + 1, n + 1)); sort(v.begin(), v.end()); res = INF * 1LL * INF; for (i=1;i<(int)v.size() - 1;i++) { if (v[i].first - 1 != v[i - 1].second) check(v[i].first - 1); if (v[i].second + 1 != v[i + 1].first) check(v[i].second + 1); } cout << res << endl; return 0; } |
English