#include <bits/stdc++.h>
#ifndef LOCAL
#include "walk.h"
#endif
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
return y > x ? (x = y, true) : false;
}
void debug_out() {
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
cerr << ' ' << A;
debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
for (int i = 0; i < n; ++i)
cerr << a[i] << ' ';
cerr << endl;
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
for (auto& x : v)
stream << x << ' ';
return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
return stream << p.first << ' ' << p.second;
}
const ll INF64 = 1e18;
const int maxN = 100005;
int n, m;
const int maxV = 4e6 + 6;
int V;
vector<pii> G[maxV];
ll dist[maxV];
priority_queue<pli> pq;
map<int, int> ptIdx[maxN];
int getIdx(int x, int y) {
if (ptIdx[x].count(y))
return ptIdx[x][y];
// debug(x, y, V);
return ptIdx[x][y] = V++;
}
void addEdge(int u, int v, int w) {
// debug(u, v, w);
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
ll min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int SI, int TI) {
n = X.size(), m = L.size();
int S, T;
for (int i = 0; i < n; ++i) {
int v = getIdx(i, 0);
if (i == SI)
S = v;
if (i == TI)
T = v;
}
// debug(S, T);
vector<int> ordy(m);
iota(all(ordy), 0);
sort(all(ordy), [&](int j1, int j2) -> bool {
return Y[j1] > Y[j2];
});
vector<int> ordh(n);
iota(all(ordh), 0);
sort(all(ordh), [&](int i1, int i2) -> bool {
return H[i1] > H[i2];
});
set<int> active;
int hptr = 0;
for (int j : ordy) {
int l = L[j], r = R[j], y = Y[j];
for (; hptr < ordh.size(); ++hptr) {
int i = ordh[hptr];
if (H[i] >= y)
active.insert(i);
else
break;
}
int last_i = l, last_v = getIdx(l, y);
for (auto it = active.upper_bound(l); it != active.end() && (*it) <= r; it = next(it)) {
int i = *it;
int v = getIdx(i, y);
addEdge(last_v, v, X[i] - X[last_i]);
last_v = v;
last_i = i;
}
// for (int i = l + 1; i <= r; ++i) {
// if (H[i] >= y) {
// int v = getIdx(i, y);
// addEdge(last_v, v, X[i] - X[last_i]);
// last_v = v;
// last_i = i;
// }
// }
}
for (int i = 0; i < n; ++i) {
int last_y = -1, last_v = -1;
for (auto& [y, v] : ptIdx[i]) {
if (last_y != -1) {
addEdge(last_v, v, y - last_y);
}
last_y = y;
last_v = v;
}
}
fill(dist, dist + V, INF64);
dist[S] = 0;
pq.push({0, S});
while (!pq.empty()) {
ll _d = -pq.top().fi;
int v = pq.top().se;
pq.pop();
if (_d != dist[v]) continue;
// debug(v, dist[v]);
if (v == T) return dist[v];
for (auto& [u, w] : G[v])
if (chkmin(dist[u], dist[v] + w))
pq.push({-dist[u], u});
}
return -1;
}
#ifdef LOCAL
int32_t main() {
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
assert(2 == scanf("%d%d", &n, &m));
vector<int> x(n), h(n);
for (int i = 0; i < n; i++)
assert(2 == scanf("%d%d", &x[i], &h[i]));
vector<int> l(m), r(m), y(m);
for (int i = 0; i < m; i++)
assert(3 == scanf("%d%d%d", &l[i], &r[i], &y[i]));
int s, g;
assert(2 == scanf("%d%d", &s, &g));
fclose(stdin);
long long result = min_distance(x, h, l, r, y, s, g);
printf("%lld\n", result);
fclose(stdout);
return 0;
}
#endif
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:106:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (; hptr < ordh.size(); ++hptr) {
| ~~~~~^~~~~~~~~~~~~
walk.cpp:151:9: warning: 'T' may be used uninitialized in this function [-Wmaybe-uninitialized]
151 | if (v == T) return dist[v];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
98876 KB |
Output is correct |
2 |
Correct |
52 ms |
98912 KB |
Output is correct |
3 |
Correct |
52 ms |
98876 KB |
Output is correct |
4 |
Correct |
53 ms |
98880 KB |
Output is correct |
5 |
Correct |
53 ms |
98884 KB |
Output is correct |
6 |
Correct |
53 ms |
98928 KB |
Output is correct |
7 |
Correct |
56 ms |
99068 KB |
Output is correct |
8 |
Correct |
54 ms |
98884 KB |
Output is correct |
9 |
Correct |
53 ms |
98924 KB |
Output is correct |
10 |
Correct |
52 ms |
98964 KB |
Output is correct |
11 |
Correct |
52 ms |
98936 KB |
Output is correct |
12 |
Correct |
53 ms |
98884 KB |
Output is correct |
13 |
Correct |
55 ms |
98912 KB |
Output is correct |
14 |
Correct |
54 ms |
98864 KB |
Output is correct |
15 |
Correct |
54 ms |
98812 KB |
Output is correct |
16 |
Correct |
54 ms |
98884 KB |
Output is correct |
17 |
Correct |
53 ms |
99012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
98880 KB |
Output is correct |
2 |
Correct |
53 ms |
98916 KB |
Output is correct |
3 |
Correct |
895 ms |
191868 KB |
Output is correct |
4 |
Correct |
950 ms |
204484 KB |
Output is correct |
5 |
Correct |
623 ms |
190868 KB |
Output is correct |
6 |
Correct |
568 ms |
180352 KB |
Output is correct |
7 |
Correct |
662 ms |
190916 KB |
Output is correct |
8 |
Correct |
1235 ms |
218640 KB |
Output is correct |
9 |
Correct |
792 ms |
188612 KB |
Output is correct |
10 |
Correct |
1322 ms |
244648 KB |
Output is correct |
11 |
Correct |
524 ms |
149752 KB |
Output is correct |
12 |
Correct |
416 ms |
135884 KB |
Output is correct |
13 |
Correct |
1131 ms |
226864 KB |
Output is correct |
14 |
Correct |
302 ms |
134464 KB |
Output is correct |
15 |
Correct |
361 ms |
135904 KB |
Output is correct |
16 |
Correct |
326 ms |
136996 KB |
Output is correct |
17 |
Correct |
333 ms |
135220 KB |
Output is correct |
18 |
Correct |
274 ms |
133432 KB |
Output is correct |
19 |
Correct |
63 ms |
100724 KB |
Output is correct |
20 |
Correct |
151 ms |
118468 KB |
Output is correct |
21 |
Correct |
333 ms |
136768 KB |
Output is correct |
22 |
Correct |
304 ms |
138820 KB |
Output is correct |
23 |
Correct |
352 ms |
147408 KB |
Output is correct |
24 |
Correct |
320 ms |
138776 KB |
Output is correct |
25 |
Correct |
344 ms |
137340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
108824 KB |
Output is correct |
2 |
Runtime error |
3284 ms |
840888 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
108824 KB |
Output is correct |
2 |
Runtime error |
3284 ms |
840888 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
98876 KB |
Output is correct |
2 |
Correct |
52 ms |
98912 KB |
Output is correct |
3 |
Correct |
52 ms |
98876 KB |
Output is correct |
4 |
Correct |
53 ms |
98880 KB |
Output is correct |
5 |
Correct |
53 ms |
98884 KB |
Output is correct |
6 |
Correct |
53 ms |
98928 KB |
Output is correct |
7 |
Correct |
56 ms |
99068 KB |
Output is correct |
8 |
Correct |
54 ms |
98884 KB |
Output is correct |
9 |
Correct |
53 ms |
98924 KB |
Output is correct |
10 |
Correct |
52 ms |
98964 KB |
Output is correct |
11 |
Correct |
52 ms |
98936 KB |
Output is correct |
12 |
Correct |
53 ms |
98884 KB |
Output is correct |
13 |
Correct |
55 ms |
98912 KB |
Output is correct |
14 |
Correct |
54 ms |
98864 KB |
Output is correct |
15 |
Correct |
54 ms |
98812 KB |
Output is correct |
16 |
Correct |
54 ms |
98884 KB |
Output is correct |
17 |
Correct |
53 ms |
99012 KB |
Output is correct |
18 |
Correct |
52 ms |
98880 KB |
Output is correct |
19 |
Correct |
53 ms |
98916 KB |
Output is correct |
20 |
Correct |
895 ms |
191868 KB |
Output is correct |
21 |
Correct |
950 ms |
204484 KB |
Output is correct |
22 |
Correct |
623 ms |
190868 KB |
Output is correct |
23 |
Correct |
568 ms |
180352 KB |
Output is correct |
24 |
Correct |
662 ms |
190916 KB |
Output is correct |
25 |
Correct |
1235 ms |
218640 KB |
Output is correct |
26 |
Correct |
792 ms |
188612 KB |
Output is correct |
27 |
Correct |
1322 ms |
244648 KB |
Output is correct |
28 |
Correct |
524 ms |
149752 KB |
Output is correct |
29 |
Correct |
416 ms |
135884 KB |
Output is correct |
30 |
Correct |
1131 ms |
226864 KB |
Output is correct |
31 |
Correct |
302 ms |
134464 KB |
Output is correct |
32 |
Correct |
361 ms |
135904 KB |
Output is correct |
33 |
Correct |
326 ms |
136996 KB |
Output is correct |
34 |
Correct |
333 ms |
135220 KB |
Output is correct |
35 |
Correct |
274 ms |
133432 KB |
Output is correct |
36 |
Correct |
63 ms |
100724 KB |
Output is correct |
37 |
Correct |
151 ms |
118468 KB |
Output is correct |
38 |
Correct |
333 ms |
136768 KB |
Output is correct |
39 |
Correct |
304 ms |
138820 KB |
Output is correct |
40 |
Correct |
352 ms |
147408 KB |
Output is correct |
41 |
Correct |
320 ms |
138776 KB |
Output is correct |
42 |
Correct |
344 ms |
137340 KB |
Output is correct |
43 |
Correct |
124 ms |
108824 KB |
Output is correct |
44 |
Runtime error |
3284 ms |
840888 KB |
Execution killed with signal 11 |
45 |
Halted |
0 ms |
0 KB |
- |