#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
// range min
struct Seg {
int n;
vector<int64_t> seg;
Seg(int _n) : n(_n) { seg.assign(2 * n, INT64_MAX); }
void set(int i, int64_t v) {
i += n;
seg[i] = v;
while (i > 1) {
i /= 2;
seg[i] = std::min(seg[2 * i], seg[2 * i + 1]);
}
}
int64_t min(int l, int r) {
l += n, r += n;
int64_t minimum = INT64_MAX;
while (l < r) {
if (l & 1) {
minimum = std::min(minimum, seg[l]);
l += 1;
}
if (r & 1) {
r -= 1;
minimum = std::min(minimum, seg[r]);
}
l /= 2, r /= 2;
}
return minimum;
}
};
long long find_shortcut(int n, vector<int> l, vector<int> d, int c) {
vector<int64_t> x(n), xpdsfx_xmd_min(n + 1), xpdsfx_x_max(n + 1);
vector<pair<int64_t, int>> xpd(n);
vector<int> xpd_ord(n);
x[0] = 0;
for (int i = 0; i < n - 1; ++i) x[i + 1] = x[i] + l[i];
for (int i = 0; i < n; ++i) xpd[i] = {x[i] + d[i], i};
sort(xpd.begin(), xpd.end());
for (int i = 0; i < n; ++i) xpd_ord[xpd[i].second] = i;
xpdsfx_xmd_min[n] = INT64_MAX;
xpdsfx_x_max[n] = INT64_MIN;
for (int i = n - 1; i >= 0; --i)
xpdsfx_xmd_min[i] =
min(xpdsfx_xmd_min[i + 1], x[xpd[i].second] - d[xpd[i].second]),
xpdsfx_x_max[i] = max(xpdsfx_x_max[i + 1], x[xpd[i].second]);
vector<int64_t> last_xpd(n, INT64_MIN), first_xmd(n, INT64_MIN);
// rect of [a, b] transpose w. length <= l
auto find_rect = [&](int64_t l) -> array<int64_t, 4> {
int64_t gpl = INT64_MIN, gpu = INT64_MAX, gml = INT64_MIN, gmu = INT64_MAX;
// xmd only exist for x[v] > x[u], otherwise MAX in xpd order
Seg xmd(n);
for (int i = 0; i < n; ++i) {
xmd.set(i, x[xpd[i].second] - d[xpd[i].second]);
}
for (int u = 0; u < n; ++u) {
xmd.set(xpd_ord[u], INT64_MAX);
// |u-v| > l- d[u] - d[v]
// v < u: pass
// u < v: v-u > l-d[u] - d[v]
// v + d[v] > l + u - d[u]
// xpd[v] > l + u - d[u]
int v_lower = upper_bound(xpd.begin(), xpd.end(),
make_pair(l + x[u] - d[u], INT_MAX)) -
xpd.begin();
if (v_lower == xpd.size()) continue;
// for v in xpd[v_lower..] and x[v] > x[u]
// find last elem in xpd w. x[v] > x[u]
auto last_x_it =
lower_bound(xpdsfx_x_max.begin(), xpdsfx_x_max.end(), x[u],
[&](int64_t x1, int64_t x2) { return x1 > x2; });
if (last_x_it == xpdsfx_x_max.begin()) continue;
int last_x = *--last_x_it;
int last_v = lower_bound(x.begin(), x.end(), last_x) - x.begin();
if (last_v <= u) continue;
if (xpd_ord[last_v] < v_lower) continue;
// find elem in xpd[v_lower..] w. x[v] > x[u] and x[v]-d[v] min
int64_t minimum_xmd = xmd.min(v_lower, n);
if (minimum_xmd == INT64_MAX) {
for (int i = 0; i < n; ++i) cout << xmd.min(i, i + 1) << " ";
cout << "\n" << flush;
int x = 3;
}
assert(minimum_xmd != INT64_MAX);
if (minimum_xmd == INT64_MAX) continue;
int64_t last_xpd = x[last_v] + d[last_v];
// xpd[u] + xpd[v] - (l-c) max
int64_t pl = (x[u] + d[u]) + last_xpd - (l - c);
// xmd[u] + xmd[v] + (l-c) min
int64_t pu = (x[u] - d[u]) + minimum_xmd + (l - c);
// -xmd[u] + xpd[v] - (l-c) max
int64_t ml = -(x[u] - d[u]) + last_xpd - (l - c);
// -xpd[u] + xmd[v] + (l-c) min
int64_t mu = -(x[u] + d[u]) + minimum_xmd + (l - c);
gpl = max(gpl, pl), gpu = min(gpu, pu);
gml = max(gml, ml), gmu = min(gmu, mu);
}
return {gpl, gpu, gml, gmu};
};
auto conatins_lattice_point = [&](array<int64_t, 4> rect) -> bool {
auto [pl, pu, ml, mu] = rect;
if (pl > pu || ml > mu) return false;
if (pl == INT64_MIN) {
assert(pu == INT64_MAX && ml == INT64_MIN && mu == INT64_MAX);
return true;
}
for (int a = 0; a < n; ++a) {
// pl <= a+b <= pu
// ml <= b-a <= mu
// pl - a <= b <= pu - a
// ml + a <= b <= mu + a
int64_t bl = max(pl - x[a], ml + x[a]);
int64_t bu = min(pu - x[a], mu + x[a]);
if (bl > bu) continue;
if (lower_bound(x.begin(), x.end(), bl) !=
upper_bound(x.begin(), x.end(), bu)) {
return true;
}
}
return false;
};
int64_t ll = 0, lu = INT64_MAX / 2;
// diameter <= ll impos, <= lu pos
while (ll + 1 < lu) {
int64_t lm = ll + (lu - ll) / 2;
if (conatins_lattice_point(find_rect(lm))) {
// lm possible
lu = lm;
} else {
ll = lm;
}
}
return lu;
}
#ifndef EVAL
#include "grader.cpp"
#endif
Compilation message
shortcut.cpp: In lambda function:
shortcut.cpp:76:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | if (v_lower == xpd.size()) continue;
| ~~~~~~~~^~~~~~~~~~~~~
shortcut.cpp:94:13: warning: unused variable 'x' [-Wunused-variable]
94 | int x = 3;
| ^
shortcut.cpp: In lambda function:
shortcut.cpp:116:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | auto [pl, pu, ml, mu] = rect;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
344 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
360 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
348 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
344 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
0 ms |
348 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
344 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
0 ms |
348 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
0 ms |
348 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
0 ms |
348 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
0 ms |
348 KB |
n = 3, 3000000000 is a correct answer |
14 |
Incorrect |
0 ms |
348 KB |
n = 4, incorrect answer: jury 3000000001 vs contestant 1 |
15 |
Halted |
0 ms |
0 KB |
- |