#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];
vi ys[N];
vi js[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].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++;
}
}
auto get_edges = [&] (int i, int yy) {
V<pair<pi, int>> res;
for(int yyy:ys[i]) if(yyy <= h[i]) {
res.EB(pi(i, yyy), abs(yy - yyy));
}
for(int j:js[i]) {
if(y[j] == 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:81:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | auto[dd, state] = pq.top(); pq.pop();
| ^
walk.cpp:82:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
82 | auto[i, yy] = state;
| ^
walk.cpp:85:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
85 | for(auto[nstate, w]:res) {
| ^
walk.cpp:86:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | auto[ni, nyy] = nstate;
| ^
walk.cpp: In function 'll min_distance(vi, vi, vi, vi, vi, int, int)':
walk.cpp:136:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
136 | for(auto[yy, d]:mp)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
12 ms |
16712 KB |
Output is correct |
6 |
Correct |
10 ms |
16792 KB |
Output is correct |
7 |
Correct |
11 ms |
16724 KB |
Output is correct |
8 |
Correct |
10 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
11 ms |
16724 KB |
Output is correct |
11 |
Correct |
10 ms |
16724 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
10 ms |
16724 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
11 ms |
16668 KB |
Output is correct |
17 |
Correct |
9 ms |
16692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Execution timed out |
4075 ms |
74332 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
18920 KB |
Output is correct |
2 |
Correct |
96 ms |
20612 KB |
Output is correct |
3 |
Correct |
105 ms |
21312 KB |
Output is correct |
4 |
Correct |
135 ms |
25164 KB |
Output is correct |
5 |
Correct |
158 ms |
29584 KB |
Output is correct |
6 |
Correct |
154 ms |
27488 KB |
Output is correct |
7 |
Correct |
73 ms |
22512 KB |
Output is correct |
8 |
Correct |
93 ms |
27116 KB |
Output is correct |
9 |
Correct |
141 ms |
28928 KB |
Output is correct |
10 |
Correct |
115 ms |
28780 KB |
Output is correct |
11 |
Correct |
19 ms |
17748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
18920 KB |
Output is correct |
2 |
Correct |
96 ms |
20612 KB |
Output is correct |
3 |
Correct |
105 ms |
21312 KB |
Output is correct |
4 |
Correct |
135 ms |
25164 KB |
Output is correct |
5 |
Correct |
158 ms |
29584 KB |
Output is correct |
6 |
Correct |
154 ms |
27488 KB |
Output is correct |
7 |
Correct |
73 ms |
22512 KB |
Output is correct |
8 |
Correct |
93 ms |
27116 KB |
Output is correct |
9 |
Correct |
141 ms |
28928 KB |
Output is correct |
10 |
Correct |
115 ms |
28780 KB |
Output is correct |
11 |
Correct |
19 ms |
17748 KB |
Output is correct |
12 |
Correct |
97 ms |
21288 KB |
Output is correct |
13 |
Correct |
79 ms |
24820 KB |
Output is correct |
14 |
Correct |
153 ms |
29552 KB |
Output is correct |
15 |
Correct |
110 ms |
25092 KB |
Output is correct |
16 |
Correct |
111 ms |
25064 KB |
Output is correct |
17 |
Correct |
114 ms |
25148 KB |
Output is correct |
18 |
Correct |
107 ms |
25064 KB |
Output is correct |
19 |
Correct |
124 ms |
25052 KB |
Output is correct |
20 |
Correct |
79 ms |
22652 KB |
Output is correct |
21 |
Correct |
32 ms |
18636 KB |
Output is correct |
22 |
Correct |
101 ms |
23200 KB |
Output is correct |
23 |
Correct |
97 ms |
23396 KB |
Output is correct |
24 |
Correct |
123 ms |
24868 KB |
Output is correct |
25 |
Correct |
114 ms |
23476 KB |
Output is correct |
26 |
Correct |
100 ms |
27532 KB |
Output is correct |
27 |
Correct |
187 ms |
29112 KB |
Output is correct |
28 |
Correct |
100 ms |
24680 KB |
Output is correct |
29 |
Correct |
141 ms |
27500 KB |
Output is correct |
30 |
Correct |
68 ms |
22628 KB |
Output is correct |
31 |
Correct |
155 ms |
28820 KB |
Output is correct |
32 |
Correct |
89 ms |
26976 KB |
Output is correct |
33 |
Correct |
95 ms |
27880 KB |
Output is correct |
34 |
Correct |
118 ms |
27284 KB |
Output is correct |
35 |
Correct |
128 ms |
25948 KB |
Output is correct |
36 |
Correct |
86 ms |
25564 KB |
Output is correct |
37 |
Correct |
76 ms |
25140 KB |
Output is correct |
38 |
Correct |
95 ms |
27172 KB |
Output is correct |
39 |
Correct |
118 ms |
28896 KB |
Output is correct |
40 |
Correct |
94 ms |
26928 KB |
Output is correct |
41 |
Correct |
95 ms |
25660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
12 ms |
16712 KB |
Output is correct |
6 |
Correct |
10 ms |
16792 KB |
Output is correct |
7 |
Correct |
11 ms |
16724 KB |
Output is correct |
8 |
Correct |
10 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
11 ms |
16724 KB |
Output is correct |
11 |
Correct |
10 ms |
16724 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
10 ms |
16724 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
11 ms |
16668 KB |
Output is correct |
17 |
Correct |
9 ms |
16692 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
8 ms |
16724 KB |
Output is correct |
20 |
Execution timed out |
4075 ms |
74332 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |