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 <bits/stdc++.h>
#include "shortcut.h"
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define y1 qwertyuiop
#define y2 asdfghjkl
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 1000013
#define INF 1000000007
#define LLINF 2696969696969696969
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N;
ll D;
pll arr[MAXN];
vl pts;
ll fen[2][MAXN];
set<ll> vals;
void init()
{
FOR(i, 0, SZ(pts) + 1)
{
fen[0][i] = LLINF;
fen[1][i] = -LLINF;
}
}
void update(int w, int idx, ll v)
{
for (int e = idx + 1; e <= SZ(pts); e += e & (-e))
{
if (w == 0) ckmin(fen[w][e], v);
else ckmax(fen[w][e], v);
}
return;
}
ll query(int w, int idx)
{
ll res = (w ? -LLINF : LLINF);
for (int e = idx + 1; e; e -= e & (-e))
{
if (w == 0) ckmin(res, fen[w][e]);
else ckmax(res, fen[w][e]);
}
return res;
}
bool check(ll s)
{
pll x = {-LLINF, LLINF}, y = {-LLINF, LLINF};
init();
FOR(i, 0, N)
{
ll c = arr[i].fi, d = arr[i].se;
int idx = LB(ALL(pts), c + d - s) - pts.begin() - 1;
ll mx = query(1, idx), mn = query(0, idx);
ckmax(x.fi, mx + c + d);
ckmin(x.se, mn + c - d);
ckmax(y.fi, mx - c + d);
ckmin(y.se, mn - c - d);
idx = LB(ALL(pts), c - d) - pts.begin();
update(1, idx, c + d);
update(0, idx, c - d);
}
// FOR(i, 0, N)
// {
// ll a = arr[i].fi, b = arr[i].se;
// FOR(j, i + 1, N)
// {
// ll c = arr[j].fi, d = arr[j].se;
// if (d + c + b - a <= s) continue;
// // you need d+c+b-a > s <=> a-b < d+c-s.
// // store min value of a-b and max value of a+b
// ckmax(x.fi, a + b + c + d);
// ckmin(x.se, a - b + c - d);
// ckmax(y.fi, a + b - c + d);
// ckmin(y.se, a - b - c - d);
// //update(a + c - rem, a + c + rem, a - c - rem, a - c + rem)
// }
// }
x.fi += (D - s);
y.fi += (D - s);
x.se += (s - D);
y.se += (s - D);
vals.clear();
FORD(i, N, 0)
{
ll m = arr[i].fi;
ll lt = max(x.fi - m, m - y.se), rt = min(x.se - m, m - y.fi);
auto it = vals.LB(lt);
if (it != vals.end() && *it <= rt) return true;
vals.insert(m);
}
// cerr << s << ' ' << x.fi << ' ' << x.se << ' ' << y.fi << ' ' << y.se << endl;
// FOR(i, 0, N)
// {
// ll m = arr[i].fi;
// FOR(j, i + 1, N)
// {
// ll n = arr[j].fi;
// // cerr << m + n << ',' << m - n << endl;
// if (x.fi <= m + n && m + n <= x.se && y.fi <= m - n && m - n <= y.se) return true;
// }
// }
return false;
}
ll find_shortcut(int n, vi l, vi d, int vs)
{
N = n; D = vs;
l.insert(l.begin(), 0);
FOR(i, 0, N)
{
arr[i].fi = l[i];
arr[i].se = d[i];
}
FOR(i, 1, N)
{
arr[i].fi += arr[i - 1].fi;
}
// FOR(i, 0, N)
// {
// cerr << arr[i].fi << ' ' << arr[i].se << endl;
// }
FOR(i, 0, N)
{
pts.PB(arr[i].fi - arr[i].se);
}
sort(ALL(pts));
pts.erase(unique(ALL(pts)), pts.end());
ll lo = 0, hi = LLINF;
while(hi > lo)
{
ll mid = (hi + lo) >> 1;
if (check(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
# | 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... |