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>
using namespace std;
#define mp make_pair
#define pb push_back
using ll = long long;
struct ds {
set<pair<ll, ll> > S;
void insert(ll x, ll y) {
vector<set<pair<ll, ll> >::iterator> to_erase;
// insert (x, y)
auto it = S.lower_bound(mp(x, 0ll));
if (it != S.end() && it->second >= y) {
return;
}
if (it == S.end() || it->first != x) {
if (it == S.begin()) {
S.emplace(x, y);
return;
}
--it;
}
while (1) {
if (it->second <= y) {
to_erase.pb(it);
} else {
break;
}
if (it == S.begin()) {
break;
}
--it;
}
for (auto it2 : to_erase) {
S.erase(it2);
}
S.emplace(x, y);
}
ll query(ll k) {
// max y over all x > k
auto it = S.upper_bound(mp(k, (ll)1e18));
return it == S.end() ? -(ll)1e18 : it->second;
}
};
ll find_shortcut(int n, vector<int> l, vector<int> d, int c) {
vector<ll> x(n, 0);
for (int i = 0; i + 1 < n; i++) {
x[i + 1] = x[i] + (ll)l[i];
}
auto good = [&](ll k) {
ll min_x = -(ll)1e18, max_x = (ll)1e18, min_y = -(ll)1e18, max_y = (ll)1e18;
{
ds S;
for (int j = 1; j < n; j++) {
S.insert(d[j - 1] - x[j - 1], x[j - 1] + d[j - 1]);
min_x = max(min_x, S.query(k - d[j] - x[j]) - k + c + d[j] - x[j]);
}
}
{
ds S;
for (int j = 1; j < n; j++) {
S.insert(d[j - 1] - x[j - 1], -(x[j - 1] - d[j - 1]));
max_x = min(max_x, -S.query(k - d[j] - x[j]) + k - c - d[j] - x[j]);
}
}
{
ds S;
for (int j = 1; j < n; j++) {
S.insert(d[j - 1] - x[j - 1], x[j - 1] + d[j - 1]);
min_y = max(min_y, S.query(k - d[j] - x[j]) - k + c + d[j] + x[j]);
}
}
{
ds S;
for (int j = 1; j < n; j++) {
S.insert(d[j - 1] - x[j - 1], -(x[j - 1] - d[j - 1]));
max_y = min(max_y, -S.query(k - d[j] - x[j]) + k - c - d[j] + x[j]);
}
}
if (min_x <= max_x && min_y <= max_y) {
set<ll> pos;
for (int j = 1; j < n; j++) {
pos.insert(x[j - 1]);
ll lb = max(min_x + x[j], min_y - x[j]), rb = min(max_x + x[j], max_y - x[j]);
auto it = pos.lower_bound(lb);
if (it != pos.end() && *it <= rb) {
return true;
}
}
return false;
} else {
return false;
}
};
ll lo = 0, hi = (ll)1e16, ans = -1;
while (lo <= hi) {
ll mid = (lo + hi) / 2ll;
if (good(mid)) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
# | 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... |