#include "walk.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define MP make_pair
#define EB emplace_back
#define PB push_back
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 1e5 + 7;
const ll oo = 1e18;
vi asr[N], asl[N];
map<int, ll> dist[N];
map<int, vi> js[N];
vi ys[N];
vi ks[N];
ll brute(vi& x, vi& h, vi& l, vi& r, vi& y, int s, int g) {
int n = SZ(x);
int m = SZ(l);
ys[s].PB(0);
ys[g].PB(0);
auto add_ev = [&] (int i, int j) {
js[i][y[j]].PB(j);
ys[i].PB(y[j]);
ks[j].PB(i);
};
vi sh(n); iota(ALL(sh), 0);
sort(ALL(sh), [&] (int i, int j) {
return h[i] < h[j];
});
vi sy(m); iota(ALL(sy), 0);
sort(ALL(sy), [&] (int i, int j) {
return y[i] < y[j];
});
set<int> st;
while(SZ(sy)) {
int j = sy.back(); sy.pop_back();
while(SZ(sh) && h[sh.back()] >= y[j]) {
st.insert(sh.back());
sh.pop_back();
}
auto it = st.lower_bound(l[j]);
while(it != st.end() && *it <= r[j]) {
add_ev(*it, j);
it++;
}
}
for(int i = 0; i < n; i++) {
sort(ALL(ys[i]));
ys[i].resize(unique(ALL(ys[i])) - ys[i].begin());
}
auto get_edges = [&] (int i, int yy) {
V<pair<pi, int>> res;
int pos = lower_bound(ALL(ys[i]), yy) - ys[i].begin();
for(int _ = max(pos - 1, 0); _ <= min(pos + 1, SZ(ys[i]) - 1); _++) {
int yyy = ys[i][_];
res.EB(pi(i, yyy), abs(yy - yyy));
}
for(int j:js[i][yy]) {
for(int k:ks[j]) {
res.EB(pi(k, yy), abs(x[i] - x[k]));
}
}
return res;
};
priority_queue<pair<ll, pi>, V<pair<ll, pi>>, greater<pair<ll, pi>>> pq;
pq.push(MP(0, pi(s, 0)));
dist[s][0] = 0;
while(pq.size()) {
auto[dd, state] = pq.top(); pq.pop();
auto[i, yy] = state;
if(dd > dist[i][yy]) continue;
V<pair<pi, int>> res = get_edges(i, yy);
for(auto[nstate, w]:res) {
auto[ni, nyy] = nstate;
if(dist[ni].count(nyy) == 0 || dd + w < dist[ni][nyy]) {
dist[ni][nyy] = dd + w;
pq.push(MP(dd + w, nstate));
}
}
}
if(dist[g].count(0))
return dist[g][0];
return -1;
}
ll min_distance(vi x, vi h, vi l, vi r, vi y, int s, int g) {
int n = SZ(x);
int m = SZ(l);
if(s != 0 || g != n - 1) {
return brute(x, h, l, r, y, s, g);
}
for(int i = 0; i < m; i++) {
asl[l[i]].PB(y[i]);
asr[r[i]].PB(y[i]);
}
map<int, ll> mp;
mp[0] = 0;
asr[0].PB(0);
for(int i = 0; i < n; i++) {
set<int> no;
for(int yy:asl[i]) {
no.insert(yy);
auto it = mp.lower_bound(yy);
ll d = oo;
if(it != mp.end())
d = min(d, it -> S + abs(yy - it -> F));
if(it != mp.begin()) {
it--;
d = min(d, it -> S + abs(yy - it -> F));
}
mp[yy] = d;
}
if(i < n - 1) for(int yy:asr[i]) {
if(no.count(yy)) continue;
mp.erase(yy);
}
if(mp.empty()) return -1;
}
ll ans = oo;
for(auto[yy, d]:mp)
ans = min(ans, d + yy);
return ans + x[n - 1] - x[0];
}
Compilation message
walk.cpp: In function 'll brute(vi&, vi&, vi&, vi&, vi&, int, int)':
walk.cpp:86:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | auto[dd, state] = pq.top(); pq.pop();
| ^
walk.cpp:87:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | auto[i, yy] = state;
| ^
walk.cpp:90:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | for(auto[nstate, w]:res) {
| ^
walk.cpp:91:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
91 | auto[ni, nyy] = nstate;
| ^
walk.cpp: In function 'll min_distance(vi, vi, vi, vi, vi, int, int)':
walk.cpp:141:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
141 | for(auto[yy, d]:mp)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
10 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19176 KB |
Output is correct |
6 |
Correct |
11 ms |
19156 KB |
Output is correct |
7 |
Correct |
12 ms |
19156 KB |
Output is correct |
8 |
Correct |
10 ms |
19156 KB |
Output is correct |
9 |
Correct |
10 ms |
19060 KB |
Output is correct |
10 |
Correct |
11 ms |
19156 KB |
Output is correct |
11 |
Correct |
10 ms |
19060 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
18996 KB |
Output is correct |
14 |
Correct |
10 ms |
19068 KB |
Output is correct |
15 |
Correct |
9 ms |
19028 KB |
Output is correct |
16 |
Correct |
10 ms |
19052 KB |
Output is correct |
17 |
Correct |
9 ms |
19028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19020 KB |
Output is correct |
3 |
Correct |
1980 ms |
152748 KB |
Output is correct |
4 |
Correct |
1105 ms |
159508 KB |
Output is correct |
5 |
Correct |
483 ms |
102780 KB |
Output is correct |
6 |
Correct |
418 ms |
93448 KB |
Output is correct |
7 |
Correct |
473 ms |
102864 KB |
Output is correct |
8 |
Correct |
2859 ms |
189984 KB |
Output is correct |
9 |
Correct |
659 ms |
108804 KB |
Output is correct |
10 |
Correct |
1661 ms |
215592 KB |
Output is correct |
11 |
Correct |
615 ms |
90736 KB |
Output is correct |
12 |
Correct |
97 ms |
29148 KB |
Output is correct |
13 |
Correct |
104 ms |
29268 KB |
Output is correct |
14 |
Correct |
238 ms |
54840 KB |
Output is correct |
15 |
Correct |
321 ms |
69236 KB |
Output is correct |
16 |
Correct |
313 ms |
70504 KB |
Output is correct |
17 |
Correct |
326 ms |
71360 KB |
Output is correct |
18 |
Correct |
152 ms |
37248 KB |
Output is correct |
19 |
Correct |
19 ms |
21532 KB |
Output is correct |
20 |
Correct |
112 ms |
44924 KB |
Output is correct |
21 |
Correct |
79 ms |
30028 KB |
Output is correct |
22 |
Correct |
92 ms |
32396 KB |
Output is correct |
23 |
Correct |
103 ms |
33812 KB |
Output is correct |
24 |
Correct |
90 ms |
32092 KB |
Output is correct |
25 |
Correct |
81 ms |
30412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
21332 KB |
Output is correct |
2 |
Correct |
95 ms |
22860 KB |
Output is correct |
3 |
Correct |
101 ms |
23440 KB |
Output is correct |
4 |
Correct |
133 ms |
27184 KB |
Output is correct |
5 |
Correct |
154 ms |
31656 KB |
Output is correct |
6 |
Correct |
148 ms |
29604 KB |
Output is correct |
7 |
Correct |
77 ms |
24672 KB |
Output is correct |
8 |
Correct |
98 ms |
29232 KB |
Output is correct |
9 |
Correct |
141 ms |
31032 KB |
Output is correct |
10 |
Correct |
114 ms |
30796 KB |
Output is correct |
11 |
Correct |
21 ms |
19796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
21332 KB |
Output is correct |
2 |
Correct |
95 ms |
22860 KB |
Output is correct |
3 |
Correct |
101 ms |
23440 KB |
Output is correct |
4 |
Correct |
133 ms |
27184 KB |
Output is correct |
5 |
Correct |
154 ms |
31656 KB |
Output is correct |
6 |
Correct |
148 ms |
29604 KB |
Output is correct |
7 |
Correct |
77 ms |
24672 KB |
Output is correct |
8 |
Correct |
98 ms |
29232 KB |
Output is correct |
9 |
Correct |
141 ms |
31032 KB |
Output is correct |
10 |
Correct |
114 ms |
30796 KB |
Output is correct |
11 |
Correct |
21 ms |
19796 KB |
Output is correct |
12 |
Correct |
97 ms |
23448 KB |
Output is correct |
13 |
Correct |
80 ms |
26804 KB |
Output is correct |
14 |
Correct |
154 ms |
31644 KB |
Output is correct |
15 |
Correct |
109 ms |
27180 KB |
Output is correct |
16 |
Correct |
117 ms |
27276 KB |
Output is correct |
17 |
Correct |
112 ms |
27272 KB |
Output is correct |
18 |
Correct |
110 ms |
27272 KB |
Output is correct |
19 |
Correct |
112 ms |
27276 KB |
Output is correct |
20 |
Correct |
76 ms |
24728 KB |
Output is correct |
21 |
Correct |
36 ms |
20684 KB |
Output is correct |
22 |
Correct |
99 ms |
25248 KB |
Output is correct |
23 |
Correct |
96 ms |
25504 KB |
Output is correct |
24 |
Correct |
99 ms |
27056 KB |
Output is correct |
25 |
Correct |
92 ms |
25468 KB |
Output is correct |
26 |
Correct |
101 ms |
29628 KB |
Output is correct |
27 |
Correct |
154 ms |
31240 KB |
Output is correct |
28 |
Correct |
81 ms |
26804 KB |
Output is correct |
29 |
Correct |
144 ms |
29472 KB |
Output is correct |
30 |
Correct |
71 ms |
24608 KB |
Output is correct |
31 |
Correct |
141 ms |
31036 KB |
Output is correct |
32 |
Correct |
99 ms |
29000 KB |
Output is correct |
33 |
Correct |
105 ms |
29980 KB |
Output is correct |
34 |
Correct |
101 ms |
29260 KB |
Output is correct |
35 |
Correct |
97 ms |
28040 KB |
Output is correct |
36 |
Correct |
88 ms |
27768 KB |
Output is correct |
37 |
Correct |
78 ms |
27248 KB |
Output is correct |
38 |
Correct |
92 ms |
29228 KB |
Output is correct |
39 |
Correct |
102 ms |
30908 KB |
Output is correct |
40 |
Correct |
85 ms |
29048 KB |
Output is correct |
41 |
Correct |
84 ms |
27704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
10 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19176 KB |
Output is correct |
6 |
Correct |
11 ms |
19156 KB |
Output is correct |
7 |
Correct |
12 ms |
19156 KB |
Output is correct |
8 |
Correct |
10 ms |
19156 KB |
Output is correct |
9 |
Correct |
10 ms |
19060 KB |
Output is correct |
10 |
Correct |
11 ms |
19156 KB |
Output is correct |
11 |
Correct |
10 ms |
19060 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
18996 KB |
Output is correct |
14 |
Correct |
10 ms |
19068 KB |
Output is correct |
15 |
Correct |
9 ms |
19028 KB |
Output is correct |
16 |
Correct |
10 ms |
19052 KB |
Output is correct |
17 |
Correct |
9 ms |
19028 KB |
Output is correct |
18 |
Correct |
9 ms |
19028 KB |
Output is correct |
19 |
Correct |
10 ms |
19020 KB |
Output is correct |
20 |
Correct |
1980 ms |
152748 KB |
Output is correct |
21 |
Correct |
1105 ms |
159508 KB |
Output is correct |
22 |
Correct |
483 ms |
102780 KB |
Output is correct |
23 |
Correct |
418 ms |
93448 KB |
Output is correct |
24 |
Correct |
473 ms |
102864 KB |
Output is correct |
25 |
Correct |
2859 ms |
189984 KB |
Output is correct |
26 |
Correct |
659 ms |
108804 KB |
Output is correct |
27 |
Correct |
1661 ms |
215592 KB |
Output is correct |
28 |
Correct |
615 ms |
90736 KB |
Output is correct |
29 |
Correct |
97 ms |
29148 KB |
Output is correct |
30 |
Correct |
104 ms |
29268 KB |
Output is correct |
31 |
Correct |
238 ms |
54840 KB |
Output is correct |
32 |
Correct |
321 ms |
69236 KB |
Output is correct |
33 |
Correct |
313 ms |
70504 KB |
Output is correct |
34 |
Correct |
326 ms |
71360 KB |
Output is correct |
35 |
Correct |
152 ms |
37248 KB |
Output is correct |
36 |
Correct |
19 ms |
21532 KB |
Output is correct |
37 |
Correct |
112 ms |
44924 KB |
Output is correct |
38 |
Correct |
79 ms |
30028 KB |
Output is correct |
39 |
Correct |
92 ms |
32396 KB |
Output is correct |
40 |
Correct |
103 ms |
33812 KB |
Output is correct |
41 |
Correct |
90 ms |
32092 KB |
Output is correct |
42 |
Correct |
81 ms |
30412 KB |
Output is correct |
43 |
Correct |
32 ms |
21332 KB |
Output is correct |
44 |
Correct |
95 ms |
22860 KB |
Output is correct |
45 |
Correct |
101 ms |
23440 KB |
Output is correct |
46 |
Correct |
133 ms |
27184 KB |
Output is correct |
47 |
Correct |
154 ms |
31656 KB |
Output is correct |
48 |
Correct |
148 ms |
29604 KB |
Output is correct |
49 |
Correct |
77 ms |
24672 KB |
Output is correct |
50 |
Correct |
98 ms |
29232 KB |
Output is correct |
51 |
Correct |
141 ms |
31032 KB |
Output is correct |
52 |
Correct |
114 ms |
30796 KB |
Output is correct |
53 |
Correct |
21 ms |
19796 KB |
Output is correct |
54 |
Correct |
97 ms |
23448 KB |
Output is correct |
55 |
Correct |
80 ms |
26804 KB |
Output is correct |
56 |
Correct |
154 ms |
31644 KB |
Output is correct |
57 |
Correct |
109 ms |
27180 KB |
Output is correct |
58 |
Correct |
117 ms |
27276 KB |
Output is correct |
59 |
Correct |
112 ms |
27272 KB |
Output is correct |
60 |
Correct |
110 ms |
27272 KB |
Output is correct |
61 |
Correct |
112 ms |
27276 KB |
Output is correct |
62 |
Correct |
76 ms |
24728 KB |
Output is correct |
63 |
Correct |
36 ms |
20684 KB |
Output is correct |
64 |
Correct |
99 ms |
25248 KB |
Output is correct |
65 |
Correct |
96 ms |
25504 KB |
Output is correct |
66 |
Correct |
99 ms |
27056 KB |
Output is correct |
67 |
Correct |
92 ms |
25468 KB |
Output is correct |
68 |
Correct |
101 ms |
29628 KB |
Output is correct |
69 |
Correct |
154 ms |
31240 KB |
Output is correct |
70 |
Correct |
81 ms |
26804 KB |
Output is correct |
71 |
Correct |
144 ms |
29472 KB |
Output is correct |
72 |
Correct |
71 ms |
24608 KB |
Output is correct |
73 |
Correct |
141 ms |
31036 KB |
Output is correct |
74 |
Correct |
99 ms |
29000 KB |
Output is correct |
75 |
Correct |
105 ms |
29980 KB |
Output is correct |
76 |
Correct |
101 ms |
29260 KB |
Output is correct |
77 |
Correct |
97 ms |
28040 KB |
Output is correct |
78 |
Correct |
88 ms |
27768 KB |
Output is correct |
79 |
Correct |
78 ms |
27248 KB |
Output is correct |
80 |
Correct |
92 ms |
29228 KB |
Output is correct |
81 |
Correct |
102 ms |
30908 KB |
Output is correct |
82 |
Correct |
85 ms |
29048 KB |
Output is correct |
83 |
Correct |
84 ms |
27704 KB |
Output is correct |
84 |
Correct |
110 ms |
32536 KB |
Output is correct |
85 |
Execution timed out |
4097 ms |
480752 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |