#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> adj[100'005], intersect[100'005];
struct chash {
uint32_t C = uint32_t(2e9 * acos(0)) + 7;
int R = chrono::steady_clock::now().time_since_epoch().count();
int operator()(int x) const {
return __builtin_bswap64((R ^ x) * C);
}
};
unordered_map<int, ll, chash> 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++;
}
}
// for (auto j : intersect[i]) {
// cout << j << ' ';
// }
// cout << '\n';
assert(intersect[i].size() <= 11);
}
y.push_back(0);
priority_queue<pair<ll, pair<int, int>>> pq;
pq.push({0, {s, m}});
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]) {
dist[nxt][nxtedge] = newd;
pq.push({-newd, {nxt, nxtedge}});
}
}
}
}
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;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11348 KB |
Output is correct |
2 |
Correct |
9 ms |
11340 KB |
Output is correct |
3 |
Correct |
9 ms |
11348 KB |
Output is correct |
4 |
Correct |
10 ms |
11264 KB |
Output is correct |
5 |
Runtime error |
19 ms |
22868 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
11256 KB |
Output is correct |
2 |
Correct |
8 ms |
11268 KB |
Output is correct |
3 |
Execution timed out |
4065 ms |
29948 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
22568 KB |
Output is correct |
2 |
Runtime error |
42 ms |
29508 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
22568 KB |
Output is correct |
2 |
Runtime error |
42 ms |
29508 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11348 KB |
Output is correct |
2 |
Correct |
9 ms |
11340 KB |
Output is correct |
3 |
Correct |
9 ms |
11348 KB |
Output is correct |
4 |
Correct |
10 ms |
11264 KB |
Output is correct |
5 |
Runtime error |
19 ms |
22868 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |