#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll inf = 1e18;
ll min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int s, int g) {
int N = X.size();
int M = Y.size();
if (s == 0 && g == N - 1 && (*min_element(H.begin(), H.end())) == (*max_element(H.begin(), H.end()))) {
vector<set<pair<int, int>>> SL(N), SR(N);
for (int i = 0; i < M; i++) {
SL[L[i]].emplace(Y[i], i);
SR[R[i]].emplace(Y[i], i);
}
vector<int> ord(M);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) {
return R[i] < R[j];
});
vector<ll> dp(M, inf);
for (auto [y, z] : SL[0]) {
dp[z] = y + X[R[z]] - X[L[z]];
}
for (int t : ord) {
auto it = SR[L[t]].lower_bound({Y[t], -1});
if (it != SR[L[t]].end()) {
auto [y, z] = *it;
dp[t] = min(dp[t], dp[z] + y - Y[t] + X[R[t]] - X[L[t]]);
}
if (it != SR[L[t]].begin()) {
auto [y, z] = *prev(it);
dp[t] = min(dp[t], dp[z] + Y[t] - y + X[R[t]] - X[L[t]]);
}
}
ll ans = inf;
for (auto [y, z] : SR[N - 1]) {
ans = min(ans, dp[z] + y);
}
if (ans == inf) {
ans = -1;
}
return ans;
}
map<pair<int, int>, int> mp;
vector<pair<int, int>> p;
for (int i = 0; i < N; i++) {
mp[{X[i], 0}] = i;
p.emplace_back(X[i], 0);
}
vector<vector<pair<int, int>>> adj(N);
vector<tuple<int, int, int>> T(N + M);
for (int i = 0; i < N; i++) {
T[i] = {H[i], 1, i};
}
for (int i = 0; i < M; i++) {
T[N + i] = {Y[i], 0, i};
}
sort(T.rbegin(), T.rend());
set<int> S;
int q;
for (auto [y, t, k] : T) {
if (t == 1) {
S.insert(k);
} else {
q = -1;
auto it = S.lower_bound(L[k]);
while (it != S.end() && (*it) <= R[k]) {
int z = *it, w; it++;
if (!mp.count({X[z], y})) {
mp[{X[z], y}] = w = p.size();
p.emplace_back(X[z], y);
adj.push_back({});
} else {
w = mp[{X[z], y}];
}
if (q != -1) {
adj[q].emplace_back(w, X[z] - p[q].first);
adj[w].emplace_back(q, X[z] - p[q].first);
}
q = w;
}
}
}
q = -1;
for (auto [P, k] : mp) {
auto [x, y] = P;
if (q != -1 && p[q].first == x) {
adj[k].emplace_back(q, y - p[q].second);
adj[q].emplace_back(k, y - p[q].second);
}
q = k;
}
int K = adj.size();
vector<ll> dis(K, -1);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.emplace(0, s);
while (!pq.empty()) {
auto [d, x] = pq.top();
pq.pop();
if (dis[x] != -1) {
continue;
}
dis[x] = d;
for (auto [y, z] : adj[x]) {
pq.emplace(d + z, y);
}
}
return dis[g];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
7704 KB |
Output is correct |
2 |
Incorrect |
88 ms |
13196 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
7704 KB |
Output is correct |
2 |
Incorrect |
88 ms |
13196 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |