#include "walk.h"
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)
struct info {
ll l, r;
ll height;
info(ll l, ll r, ll height): l(l), r(r), height(height) {}
} ;
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
ll n = x.size();
// the only important heights, anyways
vector<ll> yvalues(A(y)); yvalues.push_back(0);
sort(A(yvalues));
yvalues.erase(unique(A(yvalues)), yvalues.end());
ll m = yvalues.size();
vector<vl> dist(n, vl(m, 1e18));
priority_queue<pair<ll, pl>, vector<pair<ll, pl>>, greater<>> pq;
map<ll, vector<info>> skywalks;
F(i, 0, y.size()) skywalks[y[i]].emplace_back(l[i], r[i], y[i]);
dist[s][0] = 0;
pq.emplace(0, pair(s, 0));
while (pq.size()) {
auto [d, head] = pq.top(); pq.pop();
auto [i, j] = head;
if (d != dist[i][j]) continue;
assert(yvalues[j] <= h[i]);
vector<pair<ll, pl>> edges;
// going up and down
auto place = [&](ll nj) {
if (!(0 <= nj and nj < m)) return;
if (yvalues[nj] <= h[i]) {
edges.emplace_back(abs(yvalues[j] - yvalues[nj]), pair(i, nj));
}
};
place(j-1); place(j+1);
if (j) for (auto &[l, r, height]: skywalks[yvalues[j]]) {
auto canreach = [&](ll i) {
return height <= h[i] and l <= i and i <= r;
};
if (canreach(i)) {
F(ni, l, r+1) if (canreach(ni)) {
edges.emplace_back(abs(x[i] - x[ni]), pair(ni, j));
}
// // we can go between this to other nodes
// FD(ni, i-1, -1) if (canreach(ni, j)) {
// edges.emplace_back(abs(x[i] - x[ni]), pair(ni, j));
// break;
// }
// F(ni, i+1, n) if (canreach(ni, j)) {
// edges.emplace_back(abs(x[i] - x[ni]), pair(ni, j));
// break;
// }
}
}
for (const auto &[w, to]: edges) {
auto [nr, nc] = to;
if (d + w < dist[nr][nc]) {
dist[nr][nc] = d + w;
pq.emplace(d + w, to);
}
}
}
if (dist[g][0] > 7e17) return -1;
return dist[g][0];
}
Compilation message
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:23:37: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | #define F(i, l, r) for (ll i = l; i < (r); ++i)
| ^
walk.cpp:44:5: note: in expansion of macro 'F'
44 | F(i, 0, y.size()) skywalks[y[i]].emplace_back(l[i], r[i], y[i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
440 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Runtime error |
544 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1615 ms |
4316 KB |
Output is correct |
2 |
Execution timed out |
4088 ms |
800508 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1615 ms |
4316 KB |
Output is correct |
2 |
Execution timed out |
4088 ms |
800508 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
440 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Runtime error |
544 ms |
1048576 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |