#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> intersect[100'005];
vector<pair<int, int>> adj[100'005];
vector<ll> dist[100'005];
vector<int> idx[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) {
auto st = chrono::steady_clock::now().time_since_epoch().count();
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))]);
}
}
y.push_back(0);
idx[s].push_back(m);
dist[s].push_back(INF);
for (int i = 0; i < m; i++) {
int j = l[i];
while (j <= r[i]) {
if (y[i] <= h[j]) {
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]) {
intersect[i].push_back(j);
j++;
}
}
for (int k = 0; k < intersect[i].size(); k++) {
dist[intersect[i][k]].push_back(INF);
idx[intersect[i][k]].push_back(i);
}
for (int k = 0; k < intersect[i].size(); k++) {
if (k + 1 < intersect[i].size()) {
adj[intersect[i][k]].push_back({intersect[i][k + 1], idx[intersect[i][k + 1]].size() - 1});
}
if (k - 1 >= 0) {
adj[intersect[i][k]].push_back({intersect[i][k - 1], idx[intersect[i][k - 1]].size() - 1});
}
}
}
priority_queue<pair<ll, pair<int, int>>> pq;
pq.push({0, {s, 0}});
while (pq.size()) {
ll d = -pq.top().first;
int i = pq.top().second.first;
int j = y[idx[i][pq.top().second.second]];
pq.pop();
for (auto [nxt, nxth] : adj[i]) {
ll newd = d + abs(x[nxt] - x[i]) + abs(j - y[idx[nxt][nxth]]);
if (newd < dist[nxt][nxth]) {
dist[nxt][nxth] = newd;
pq.push({-newd, {nxt, nxth}});
}
}
}
ll ans = INF;
for (int i = 0; i < dist[g].size(); i++) {
ans = min(ans, dist[g][i] + y[idx[g][i]]);
}
// for (auto i : dist[g]) {
// ans = min(ans, i.first + i.second);
// }
auto en = chrono::steady_clock::now().time_since_epoch().count();
cout << (en - st) / 1e9 << '\n';
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;
// }
Compilation message
walk.cpp: In function 'll min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:45:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int k = 0; k < intersect[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int k = 0; k < intersect[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | if (k + 1 < intersect[i].size()) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
walk.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < dist[g].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
9684 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
9684 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
17704 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
17704 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
9684 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |