This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shortcut.h"
#include <bits/stdc++.h>
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
inline void upmin(ll &a, ll b) { if(b < a) a = b; }
inline void upmax(ll &a, ll b) { if(a < b) a = b; }
const int MAXN = 1000005;
ll Mn[MAXN][2], Mx[MAXN][2];
int Mni[MAXN][2], Mxi[MAXN][2];
ll X[MAXN], L[MAXN];
int O[MAXN], OJ[MAXN];
int N; ll K, Ans;
bool isp(ll Y) {
ll xymx = -INFLL, xymn = INFLL, yxmx = -INFLL, yxmn = INFLL;
for(int oi = 0, i, oj = 0, j; oj < N; oj++) {
j = OJ[oj];
for(ll t = Y-X[j]-L[j], c; oi < N; oi++) {
i = O[oi]; c = X[i]-L[i];
if(-c <= t) break;
}
if(!oi) continue;
ll rmx = j == Mxi[oi-1][0] ? Mx[oi-1][1] : Mx[oi-1][0];
ll rmn = j == Mni[oi-1][0] ? Mn[oi-1][1] : Mn[oi-1][0];
ll a = Y-K + X[j]-L[j], b = K-Y + X[j]+L[j];
upmin(xymn, a+rmn); upmax(xymx, b+rmx);
upmin(yxmn, a-rmx); upmax(yxmx, b-rmn);
if(xymn < xymx || yxmn < yxmx) return false;
}
for(int s = 0, e = 0; s < N; s++) {
ll rmn = max(xymx-X[s], yxmx+X[s]);
ll rmx = min(xymn-X[s], yxmn+X[s]);
if(rmx < rmn) continue;
for(; e+1 < N && X[e] < rmn; e++);
for(; e && rmx < X[e]; e--);
if(rmn <= X[e]) return true;
}
return false;
}
ll getAns() {
ll mx = -INFLL;
for(int i = 0; i < N; i++) {
upmax(Ans, X[i]+L[i] + mx);
upmax(mx, L[i]-X[i]);
}
iota(O, O+N, 0); sort(O, O+N, [&](int a, int b) {
return L[a]-X[a] > L[b]-X[b];
});
iota(OJ, OJ+N, 0); sort(OJ, OJ+N, [&](int a, int b) {
return X[a]+L[a] < X[b]+L[b];
});
Mn[0][0] = Mn[0][1] = INFLL;
Mx[0][0] = Mx[0][1] = -INFLL;
Mni[0][0] = Mni[0][1] = Mxi[0][0] = Mxi[0][1] = -1;
for(int oi = 0, i; oi < N; oi++) {
if(oi) {
memcpy(Mn[oi], Mn[oi-1], 16);
memcpy(Mx[oi], Mx[oi-1], 16);
memcpy(Mni[oi], Mni[oi-1], 8);
memcpy(Mxi[oi], Mxi[oi-1], 8);
}
i = O[oi];
ll c = X[i]-L[i];
if(c < Mn[oi][0]) { swap(Mn[oi][0], c); swap(Mni[oi][0], i); }
if(c < Mn[oi][1]) { swap(Mn[oi][1], c); swap(Mni[oi][1], i); }
i = O[oi]; c = X[i]+L[i];
if(Mx[oi][0] < c) { swap(Mx[oi][0], c); swap(Mxi[oi][0], i); }
if(Mx[oi][1] < c) { swap(Mx[oi][1], c); swap(Mxi[oi][1], i); }
}
ll a = 0, b = 0;
for(int i = 0; i < N; i++) {
ll c = L[i];
if(a < c) swap(a, c);
if(b < c) swap(b, c);
}
ll s = a+b, e = Ans;
for(ll m; s < e;) {
m = (s+e) >> 1;
if(isp(m)) e = m;
else s = m+1;
}
return s;
}
long long find_shortcut(int n, std::vector<int> l, std::vector<int> d, int c) {
::N = n; ::K = c;
for(int i = 1; i < N; i++) ::X[i] = ::X[i-1] + l[i-1];
for(int i = 0; i < N; i++) ::L[i] = d[i];
return getAns();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |