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>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "shortcut.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e5 + 10;
long long x[N];
long long Min[N], Max[N];
void add(int i, long long ma, long long mi) {
++i;
for (; i; i -= i & - i) {
Max[i] = min(Max[i], ma);
Min[i] = max(Min[i], mi);
}
}
pair <long long, long long> get(int i) {
++i;
long long ma = 1e18, mi = - 1e18;
for (; i < N; i += i & - i) {
ma = min(ma, Max[i]);
mi = max(mi, Min[i]);
}
return {ma, mi};
}
long long find_shortcut(int n, vector<int> l, vector<int> d, int c) {
for (int i = 0; i < n - 1; ++i) x[i + 1] = x[i] + l[i];
vector <pair <long long, int>> ord1, ord2;
for (int i = 0; i < n; ++i) ord1.push_back({x[i] - d[i], i});
for (int i = 0; i < n; ++i) ord2.push_back({x[i] + d[i], i});
sort(ord1.begin(), ord1.end());
sort(ord2.begin(), ord2.end());
reverse(ord2.begin(), ord2.end());
reverse(ord1.begin(), ord1.end());
auto check = [&](long long len) {
for (int i = 0; i < N; ++i) Max[i] = 1e18, Min[i] = - 1e18;
long long max1 = 1e18, min1 = - 1e18;
long long max2 = 1e18, min2 = - 1e18;
int id = 0;
for (auto [delta, i]: ord1) {
while (id < ord2.size() && get<0>(ord2[id]) > len + delta) {
int j = get<1>(ord2[id]);
add(j, x[j] - d[j], x[j] + d[j]);
++id;
}
if (i + 1 == n) continue;
auto [Max, Min] = get(i + 1);
long long r = len - d[i] - c;
max1 = min(max1, x[i] + Max + r);
min1 = max(min1, x[i] + Min - r);
max2 = min(max2, - x[i] + Max + r);
min2 = max(min2, - x[i] + Min - r);
}
if (max1 < min1 || max2 < min2) return 0;
for (int i = 0; i < n; ++i) {
long long L = max(min1 - x[i], min2 + x[i]);
long long R = min(max1 - x[i], max2 + x[i]);
int j = lower_bound(x, x + n, L) - x;
if (j == n || x[j] > R) continue;
return 1;
}
return 0;
};
long long L = 0, R = 1e15;
while (L < R) {
long long mid = L + R >> 1;
if (check(mid)) R = mid;
else L = mid + 1;
}
return L;
}
//
//int main()
//{
// int n, c;
// assert(2 == scanf("%d%d", &n, &c));
//
// std::vector<int> l(n - 1);
// std::vector<int> d(n);
// for (int i = 0; i < n - 1; i++)
// assert(1 == scanf("%d", &l[i]));
// for (int i = 0; i < n; i++)
// assert(1 == scanf("%d", &d[i]));
//
// long long t = find_shortcut(n, l, d, c);
//
//
// printf("%lld\n", t);
//
// return 0;
//}
Compilation message (stderr)
shortcut.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
shortcut.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
shortcut.cpp: In lambda function:
shortcut.cpp:52:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for (auto [delta, i]: ord1) {
| ^
shortcut.cpp:53:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | while (id < ord2.size() && get<0>(ord2[id]) > len + delta) {
| ~~~^~~~~~~~~~~~~
shortcut.cpp:59:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | auto [Max, Min] = get(i + 1);
| ^
shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:78:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | long long mid = L + R >> 1;
| ~~^~~
# | 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... |