#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10;
const ll INF = 1e18+10;
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
int n, m;
vector<pair<int, int>> adj[N];
ll dist[N];
void add_edge(int a, int b, int w) {
adj[a].emplace_back(b, w);
adj[b].emplace_back(a, w);
}
void dijkstra(int S) {
fill(dist, dist + N, INF);
min_pq<pair<ll, int>> pq;
pq.push({dist[S] = 0, S});
while (!pq.empty()) {
auto [d_c, c] = pq.top(); pq.pop();
if (dist[c] != d_c) continue;
for (auto [nxt, w] : adj[c]) {
if (dist[nxt] > dist[c] + w) {
dist[nxt] = dist[c] + w;
pq.push({dist[nxt], nxt});
}
}
}
}
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
n = sz(x), m = sz(l);
assert(s == 0 && g == n-1);
auto build = [&]() {
vector<vector<int>> ev(n);
for (int i = 0; i < m; i++) {
ev[l[i]].push_back(i);
ev[r[i]].push_back(i);
}
vector<bool> on(m);
set<pair<int, int>> s;
for (int i = 0; i < n; i++) {
for (int x : ev[i]) if (!on[x]) {
auto me = s.insert({y[x], x}).first;
{
auto it = next(me);
if (it != s.end() && it->first <= h[i]) {
add_edge(x, it->second, it->first - y[x]);
}
}
{
if (me != s.begin()) {
auto it = prev(me);
add_edge(x, it->second, y[x] - it->first);
}
}
}
for (int x : ev[i]) if (on[x]) {
s.erase({y[x], x});
}
for (int x : ev[i]) on[x] = !on[x];
}
};
build();
int S = m+1;
for (int i = 0; i < m; i++) if (l[i] == 0) {
add_edge(S, i, y[i]);
}
dijkstra(S);
ll ans = INF;
for (int i = 0; i < m; i++) if (r[i] == n-1) {
ans = min(ans, dist[i] + y[i]);
}
// cerr << x[n-1] - x[0] << ' ' << ans << endl;
if (ans == INF) return -1;
return ans + x[n-1] - x[0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
8116 KB |
Output is correct |
2 |
Correct |
84 ms |
12548 KB |
Output is correct |
3 |
Correct |
94 ms |
12812 KB |
Output is correct |
4 |
Correct |
122 ms |
17308 KB |
Output is correct |
5 |
Correct |
146 ms |
21396 KB |
Output is correct |
6 |
Correct |
151 ms |
18788 KB |
Output is correct |
7 |
Correct |
58 ms |
12692 KB |
Output is correct |
8 |
Correct |
68 ms |
15120 KB |
Output is correct |
9 |
Correct |
141 ms |
24100 KB |
Output is correct |
10 |
Correct |
96 ms |
20472 KB |
Output is correct |
11 |
Correct |
10 ms |
5332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
8116 KB |
Output is correct |
2 |
Correct |
84 ms |
12548 KB |
Output is correct |
3 |
Correct |
94 ms |
12812 KB |
Output is correct |
4 |
Correct |
122 ms |
17308 KB |
Output is correct |
5 |
Correct |
146 ms |
21396 KB |
Output is correct |
6 |
Correct |
151 ms |
18788 KB |
Output is correct |
7 |
Correct |
58 ms |
12692 KB |
Output is correct |
8 |
Correct |
68 ms |
15120 KB |
Output is correct |
9 |
Correct |
141 ms |
24100 KB |
Output is correct |
10 |
Correct |
96 ms |
20472 KB |
Output is correct |
11 |
Correct |
10 ms |
5332 KB |
Output is correct |
12 |
Correct |
100 ms |
15124 KB |
Output is correct |
13 |
Correct |
112 ms |
21244 KB |
Output is correct |
14 |
Correct |
147 ms |
25440 KB |
Output is correct |
15 |
Correct |
101 ms |
20180 KB |
Output is correct |
16 |
Correct |
119 ms |
21032 KB |
Output is correct |
17 |
Correct |
103 ms |
20860 KB |
Output is correct |
18 |
Correct |
100 ms |
20212 KB |
Output is correct |
19 |
Correct |
114 ms |
20980 KB |
Output is correct |
20 |
Correct |
67 ms |
15696 KB |
Output is correct |
21 |
Correct |
20 ms |
8676 KB |
Output is correct |
22 |
Correct |
80 ms |
19156 KB |
Output is correct |
23 |
Correct |
82 ms |
19320 KB |
Output is correct |
24 |
Correct |
74 ms |
18448 KB |
Output is correct |
25 |
Correct |
74 ms |
18760 KB |
Output is correct |
26 |
Correct |
69 ms |
19328 KB |
Output is correct |
27 |
Correct |
151 ms |
25084 KB |
Output is correct |
28 |
Correct |
110 ms |
21324 KB |
Output is correct |
29 |
Correct |
137 ms |
22764 KB |
Output is correct |
30 |
Correct |
60 ms |
15660 KB |
Output is correct |
31 |
Correct |
142 ms |
24040 KB |
Output is correct |
32 |
Correct |
79 ms |
17928 KB |
Output is correct |
33 |
Incorrect |
86 ms |
19056 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |