This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "shortcut.h"
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << '\n';
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)3e5 + 5;
const ll INF = (ll)1e16;
struct Rect {
ll lx, rx, ly, ry;
Rect() {
lx = ly = -INF;
rx = ry = INF;
}
Rect(ll lx, ll rx, ll ly, ll ry)
: lx(lx), rx(rx), ly(ly), ry(ry) {}
void operator = (Rect b) {
lx = b.lx;
ly = b.ly;
rx = b.rx;
ry = b.ry;
}
};
Rect combine(Rect a, Rect b) {
return Rect(max(a.lx, b.lx), min(a.rx, b.rx), max(a.ly, b.ly), min(a.ry, b.ry));
}
ll w[MAXN], d[MAXN], p[MAXN];
int comx[MAXN];
Rect T[MAXN];
vector<ll> V;
int n;
ll k;
void compress() {
for (int i = 1; i <= n; ++i) {
V.pb(d[i] + p[i - 1]);
}
sort(all(V));
V.resize(unique(all(V)) - V.begin());
for (int i = 1; i <= n; ++i) {
comx[i] = lower_bound(all(V), d[i] + p[i - 1]) - V.begin();
}
}
void update(int p, Rect x) {
for (; p < n; p |= (p + 1)) {
T[p] = combine(T[p], x);
}
}
Rect query(int p) {
Rect ret;
for (; p >= 0; --p) {
ret = combine(ret, T[p]);
p &= (p + 1);
}
return ret;
}
bool check(ll lim) {
ll tax = -INF, tbx = INF;
ll tay = -INF, tby = INF;
for (int i = 0; i < n; ++i) {
T[i] = Rect();
}
for (int i = n; i > 0; --i) {
int tmp = V.end() - upper_bound(all(V), lim + p[i - 1] - d[i]);
Rect cur = query(tmp - 1);
tax = max(tax, p[i - 1] + d[i] - lim + k + cur.lx);
tbx = min(tbx, p[i - 1] - d[i] + lim - k + cur.rx);
tay = max(tay, p[i - 1] + d[i] - lim + k + cur.ly);
tby = min(tby, p[i - 1] - d[i] + lim - k + cur.ry);
cur.lx = p[i - 1] + d[i];
cur.rx = p[i - 1] - d[i];
cur.ly = -p[i - 1] + d[i];
cur.ry = -p[i - 1] - d[i];
update((int)V.size() - 1 - comx[i], cur);
}
for (int b = n; b > 1; --b) {
ll l = tax - p[b - 1];
ll r = tbx - p[b - 1];
l = max(l, tay + p[b - 1]);
r = min(r, tby + p[b - 1]);
int pos = lower_bound(p, p + n, l) - p + 1;
if (pos < b && p[pos - 1] <= r) {
return 1;
}
}
return 0;
}
ll solve() {
for (int i = 1; i < n; ++i) {
p[i] = p[i - 1] + w[i];
}
compress();
ll l = 0, r = INF, ans = -1;
while (l <= r) {
ll mid = (l + r) >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
return ans;
}
ll find_shortcut(int _n, vector<int> l, vector<int> _d, int c) {
n = _n;
k = c;
for (int i = 1; i < n; ++i) {
w[i] = l[i - 1];
}
for (int i = 1; i <= n; ++i) {
d[i] = _d[i - 1];
}
return solve();
}
# | 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... |