#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int f) {
int n = (int) x.size();
int m = (int) y.size();
vector<pair<int, int>> ver;
for (int i = 0; i < n; i++) {
ver.emplace_back(i, 0);
ver.emplace_back(i, h[i]);
}
for (int i = 0; i < m; i++) {
ver.emplace_back(l[i], y[i]);
ver.emplace_back(r[i], y[i]);
}
vector<vector<int>> qs(n);
for (int i = 0; i < m; i++) {
qs[l[i]].push_back(y[i]);
qs[r[i]].push_back(~y[i]);
}
multiset<int> st;
for (int i = 0; i < n; i++) {
for (int j : qs[i]) {
if (j >= 0) {
auto it = st.lower_bound(j);
if (it != st.end()) {
ver.emplace_back(i, *it);
}
if (it != st.begin()) {
ver.emplace_back(i, *prev(it));
}
st.insert(j);
}
}
for (int j : qs[i]) {
if (j < 0) {
j = ~j;
auto it = st.lower_bound(j);
if (it != prev(st.end())) {
ver.emplace_back(i, *next(it));
}
if (it != st.begin()) {
ver.emplace_back(i, *prev(it));
}
st.erase(it);
}
}
}
vector<pair<int, int>> new_ver;
for (auto& p : ver) {
if (h[p.first] >= p.second) {
new_ver.push_back(p);
}
}
ver = new_ver;
sort(ver.begin(), ver.end());
ver.erase(unique(ver.begin(), ver.end()), ver.end());
auto Index = [&](int i, int h) {
return (int) (lower_bound(ver.begin(), ver.end(), make_pair(i, h)) - ver.begin());
};
int k = (int) ver.size();
vector<vector<pair<int, int>>> g(k);
auto Add = [&](int from, int to, int cost) {
g[from].emplace_back(to, cost);
g[to].emplace_back(from, cost);
};
for (int i = 0; i + 1 < k; i++) {
if (ver[i + 1].first == ver[i].first) {
Add(i, i + 1, ver[i + 1].second - ver[i].second);
}
}
map<int, vector<int>> mp;
for (int i = 0; i < k; i++) {
mp[ver[i].second].push_back(i);
}
for (auto& p : mp) {
sort(p.second.begin(), p.second.end(), [&](int i, int j) {
return ver[i].first < ver[j].first;
});
}
for (int i = 0; i < m; i++) {
int low = 0, high = (int) mp[y[i]].size() - 1, pos = -1;
while (low <= high) {
int mid = (low + high) >> 1;
if (ver[mp[y[i]][mid]].first >= l[i]) {
pos = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
while (ver[mp[y[i]][pos]].first != r[i]) {
Add(mp[y[i]][pos], mp[y[i]][pos + 1], x[ver[mp[y[i]][pos + 1]].first] - x[ver[mp[y[i]][pos]].first]);
pos += 1;
}
}
const long long inf = (long long) 1e18;
vector<long long> dist(k, inf);
dist[Index(s, 0)] = 0;
set<pair<long long, int>> que;
que.emplace(0, Index(s, 0));
while (!que.empty()) {
auto it = que.begin();
int i = it->second;
que.erase(it);
for (auto &e : g[i]) {
int to = e.first;
int w = e.second;
if (dist[to] > dist[i] + w) {
if (dist[to] != inf) {
que.erase({dist[to], to});
}
dist[to] = dist[i] + w;
que.emplace(dist[to], to);
}
}
}
long long res = dist[Index(f, 0)];
if (res == inf) {
return -1;
} else {
return res;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
15416 KB |
Output is correct |
2 |
Correct |
843 ms |
81628 KB |
Output is correct |
3 |
Correct |
888 ms |
84436 KB |
Output is correct |
4 |
Correct |
1098 ms |
111640 KB |
Output is correct |
5 |
Correct |
1166 ms |
113560 KB |
Output is correct |
6 |
Correct |
1052 ms |
107700 KB |
Output is correct |
7 |
Correct |
510 ms |
67484 KB |
Output is correct |
8 |
Correct |
369 ms |
66852 KB |
Output is correct |
9 |
Correct |
1025 ms |
102804 KB |
Output is correct |
10 |
Correct |
420 ms |
75936 KB |
Output is correct |
11 |
Correct |
19 ms |
11428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
15416 KB |
Output is correct |
2 |
Correct |
843 ms |
81628 KB |
Output is correct |
3 |
Correct |
888 ms |
84436 KB |
Output is correct |
4 |
Correct |
1098 ms |
111640 KB |
Output is correct |
5 |
Correct |
1166 ms |
113560 KB |
Output is correct |
6 |
Correct |
1052 ms |
107700 KB |
Output is correct |
7 |
Correct |
510 ms |
67484 KB |
Output is correct |
8 |
Correct |
369 ms |
66852 KB |
Output is correct |
9 |
Correct |
1025 ms |
102804 KB |
Output is correct |
10 |
Correct |
420 ms |
75936 KB |
Output is correct |
11 |
Correct |
19 ms |
11428 KB |
Output is correct |
12 |
Correct |
876 ms |
87280 KB |
Output is correct |
13 |
Correct |
885 ms |
115892 KB |
Output is correct |
14 |
Correct |
1227 ms |
122544 KB |
Output is correct |
15 |
Correct |
662 ms |
86524 KB |
Output is correct |
16 |
Correct |
757 ms |
103736 KB |
Output is correct |
17 |
Correct |
885 ms |
116668 KB |
Output is correct |
18 |
Correct |
668 ms |
88052 KB |
Output is correct |
19 |
Correct |
800 ms |
103184 KB |
Output is correct |
20 |
Correct |
530 ms |
76792 KB |
Output is correct |
21 |
Correct |
96 ms |
33920 KB |
Output is correct |
22 |
Correct |
632 ms |
94264 KB |
Output is correct |
23 |
Correct |
561 ms |
89308 KB |
Output is correct |
24 |
Correct |
390 ms |
72632 KB |
Output is correct |
25 |
Correct |
567 ms |
89896 KB |
Output is correct |
26 |
Correct |
347 ms |
63812 KB |
Output is correct |
27 |
Correct |
1169 ms |
126604 KB |
Output is correct |
28 |
Correct |
816 ms |
114224 KB |
Output is correct |
29 |
Correct |
1149 ms |
118704 KB |
Output is correct |
30 |
Correct |
586 ms |
77892 KB |
Output is correct |
31 |
Correct |
1090 ms |
115896 KB |
Output is correct |
32 |
Correct |
312 ms |
59612 KB |
Output is correct |
33 |
Correct |
275 ms |
61884 KB |
Output is correct |
34 |
Correct |
345 ms |
67440 KB |
Output is correct |
35 |
Correct |
378 ms |
68456 KB |
Output is correct |
36 |
Correct |
312 ms |
59360 KB |
Output is correct |
37 |
Correct |
181 ms |
44076 KB |
Output is correct |
38 |
Correct |
177 ms |
49404 KB |
Output is correct |
39 |
Correct |
400 ms |
67252 KB |
Output is correct |
40 |
Correct |
230 ms |
48912 KB |
Output is correct |
41 |
Correct |
172 ms |
45860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |