#include <bits/stdc++.h>
#include "walk.h"
using ll = long long;
template <class T> using Vec = std::vector<T>;
constexpr ll INF = std::numeric_limits<ll>::max() / 2;
struct RMQ {
int size;
Vec<ll> min;
RMQ(const int n): size(n), min(2 * n, INF) {}
void assign(int i, const ll x) {
i += size;
min[i] = x;
while (i > 1) {
i >>= 1;
min[i] = std::min(min[2 * i], min[2 * i + 1]);
}
}
ll fold(int l, int r) const {
l += size;
r += size;
ll x = INF;
while (l < r) {
if (l & 1) x = std::min(x, min[l++]);
if (r & 1) x = std::min(x, min[--r]);
l >>= 1;
r >>= 1;
}
return x;
}
};
ll min_distance(Vec<int> x, Vec<int> h, Vec<int> l, Vec<int> r, Vec<int> y, int s, int g) {
const int n = (int) x.size();
const int m = (int) y.size();
if (s == 0 and g == n - 1) {
Vec<Vec<int>> start(n), end(n);
for (int i = 0; i < m; ++i) {
start[l[i]].push_back(i);
end[r[i]].push_back(i);
}
Vec<int> cmp = y;
std::sort(cmp.begin(), cmp.end());
cmp.erase(std::unique(cmp.begin(), cmp.end()), cmp.end());
Vec<int> idx(m);
for (int i = 0; i < m; ++i) {
idx[i] = std::lower_bound(cmp.begin(), cmp.end(), y[i]) - cmp.begin();
}
const int sz = (int) cmp.size();
RMQ down(sz), up(sz);
Vec<int> alive(sz);
for (const int j: start[0]) {
down.assign(idx[j], y[j] - y[j]);
up.assign(idx[j], y[j] + y[j]);
alive[idx[j]] += 1;
}
for (int i = 1; i < n; ++i) {
const int height = std::upper_bound(cmp.begin(), cmp.end(), h[i]) - cmp.begin();
for (const auto j: start[i]) {
const ll a = down.fold(0, idx[j] + 1);
const ll b = up.fold(idx[j], height);
ll d = INF;
if (a != INF) {
d = std::min(d, a + y[j]);
}
if (b != INF) {
d = std::min(d, b - y[j]);
}
if (d != INF) {
down.assign(idx[j], d - y[j]);
up.assign(idx[j], d + y[j]);
} else {
down.assign(idx[j], INF);
down.assign(idx[j], INF);
}
alive[idx[j]] += 1;
}
if (i != n - 1) {
for (const auto j: end[i]) {
alive[idx[j]] -= 1;
if (alive[idx[j]] == 0) {
down.assign(idx[j], INF);
up.assign(idx[j], INF);
}
}
}
}
ll ret = up.fold(0, sz);
return ret == INF ? -1 : (ret + x[n - 1] - x[0]);
}
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
3376 KB |
Output is correct |
2 |
Correct |
133 ms |
8268 KB |
Output is correct |
3 |
Correct |
162 ms |
9248 KB |
Output is correct |
4 |
Correct |
218 ms |
17084 KB |
Output is correct |
5 |
Correct |
184 ms |
15704 KB |
Output is correct |
6 |
Correct |
182 ms |
16952 KB |
Output is correct |
7 |
Correct |
126 ms |
12728 KB |
Output is correct |
8 |
Correct |
191 ms |
19396 KB |
Output is correct |
9 |
Correct |
226 ms |
17984 KB |
Output is correct |
10 |
Correct |
124 ms |
14912 KB |
Output is correct |
11 |
Correct |
14 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
3376 KB |
Output is correct |
2 |
Correct |
133 ms |
8268 KB |
Output is correct |
3 |
Correct |
162 ms |
9248 KB |
Output is correct |
4 |
Correct |
218 ms |
17084 KB |
Output is correct |
5 |
Correct |
184 ms |
15704 KB |
Output is correct |
6 |
Correct |
182 ms |
16952 KB |
Output is correct |
7 |
Correct |
126 ms |
12728 KB |
Output is correct |
8 |
Correct |
191 ms |
19396 KB |
Output is correct |
9 |
Correct |
226 ms |
17984 KB |
Output is correct |
10 |
Correct |
124 ms |
14912 KB |
Output is correct |
11 |
Correct |
14 ms |
3404 KB |
Output is correct |
12 |
Correct |
143 ms |
9212 KB |
Output is correct |
13 |
Correct |
250 ms |
17192 KB |
Output is correct |
14 |
Correct |
228 ms |
15556 KB |
Output is correct |
15 |
Correct |
194 ms |
14052 KB |
Output is correct |
16 |
Correct |
216 ms |
14092 KB |
Output is correct |
17 |
Correct |
179 ms |
14020 KB |
Output is correct |
18 |
Correct |
159 ms |
14032 KB |
Output is correct |
19 |
Correct |
182 ms |
14088 KB |
Output is correct |
20 |
Correct |
159 ms |
12564 KB |
Output is correct |
21 |
Correct |
36 ms |
6580 KB |
Output is correct |
22 |
Correct |
146 ms |
14212 KB |
Output is correct |
23 |
Correct |
207 ms |
14512 KB |
Output is correct |
24 |
Correct |
153 ms |
15140 KB |
Output is correct |
25 |
Correct |
159 ms |
15044 KB |
Output is correct |
26 |
Correct |
178 ms |
16196 KB |
Output is correct |
27 |
Correct |
228 ms |
16184 KB |
Output is correct |
28 |
Correct |
227 ms |
16964 KB |
Output is correct |
29 |
Correct |
288 ms |
16964 KB |
Output is correct |
30 |
Correct |
122 ms |
12788 KB |
Output is correct |
31 |
Correct |
236 ms |
18000 KB |
Output is correct |
32 |
Correct |
118 ms |
13992 KB |
Output is correct |
33 |
Incorrect |
141 ms |
15480 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |