#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> adj[100'005], intersect[100'005];
map<int, ll> dist[100'005];
int sparse[17][200'005];
const 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(), m = l.size();
for (int i = 0; i < n; i++) {
sparse[0][i] = h[i];
}
for (int j = 1; j < 17; j++) {
for (int i = 0; i < n; i++) {
sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
}
}
for (int i = 0; i < m; i++) {
int j = l[i];
while (j <= r[i]) {
if (y[i] <= h[j]) {
adj[j].push_back(i);
intersect[i].push_back(j);
j++;
continue;
}
int mx = 0;
for (int k = 16; k >= 0; k--) {
if (y[i] > max(mx, sparse[k][j])) {
mx = max(mx, sparse[k][j]);
j += (1 << k);
}
}
if (j <= r[i]) {
adj[j].push_back(i);
intersect[i].push_back(j);
j++;
}
}
assert(intersect[i].size() <= 11);
}
y.push_back(0);
priority_queue<pair<ll, pair<int, int>>> pq;
pq.push({0, {s, m}});
int updates = 0;
while (pq.size()) {
ll d = -pq.top().first;
int i = pq.top().second.first;
int j = y[pq.top().second.second];
pq.pop();
for (auto nxtedge : adj[i]) {
for (auto nxt : intersect[nxtedge]) {
ll newd = d + abs(j - y[nxtedge]) + abs(x[i] - x[nxt]);
if (dist[nxt].find(nxtedge) == dist[nxt].end() || newd < dist[nxt][nxtedge]) {
if (dist[nxt].find(nxtedge) != dist[nxt].end()) updates++;
dist[nxt][nxtedge] = newd;
pq.push({-newd, {nxt, nxtedge}});
}
}
}
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += dist[i].size();
}
ll ans = INF;
for (int i = 0; i < m; i++) {
if (dist[g].find(i) != dist[g].end()) {
ans = min(ans, dist[g][i] + y[i]);
}
}
if (ans >= INF) return -1;
return ans;
}
// int main() {
// ios_base::sync_with_stdio(0), cin.tie(0);
// int N, M;
// cin >> N >> M;
// vector<int> X(N), H(N);
// for (int i = 0; i < N; i++) {
// cin >> X[i] >> H[i];
// }
// vector<int> L(M), R(M), Y(M);
// for (int i = 0; i < M; i++) {
// cin >> L[i] >> R[i] >> Y[i];
// }
// int S, G;
// cin >> S >> G;
// cout << min_distance(X, H, L, R, Y, S, G) << '\n';
// return 0;
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Runtime error |
14 ms |
19672 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Execution timed out |
4053 ms |
30360 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
20916 KB |
Output is correct |
2 |
Runtime error |
35 ms |
24572 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
20916 KB |
Output is correct |
2 |
Runtime error |
35 ms |
24572 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Runtime error |
14 ms |
19672 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |