#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define ln '\n'
using i64 = long long;
template <class F, class S>
bool chmin(F &u, const S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
const i64 inf = 1e15;
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) {
{ // modification
auto modify = [&](int u){
int m = l.size();
set <int> st;
for ( int i = 0; i < m; i++ ){
if ( y[i] <= h[u] && l[i] <= u && r[i] >= u ){
l.pb(l[i]), r.pb(u); y.pb(y[i]);
l.pb(u), r.pb(r[i]); y.pb(y[i]);
st.insert(i);
}
}
vector <int> l_, r_, y_;
for ( int i = 0; i < (int)l.size(); i++ ){
if ( !st.count(i) ){
l_.pb(l[i]), r_.pb(r[i]), y_.pb(y[i]);
}
}
l = l_, r = r_, y = y_;
};
modify(s), modify(g);
}
int n = x.size(), m = l.size();
vector <vector<ar<int,3>>> H(n);
vector <vector<int>> qry(n);
for ( int i = 0; i < m; i++ ){
H[l[i]].pb({1, y[i], i});
H[r[i]].pb({0, y[i], i});
qry[r[i]].pb(i);
}
y.pb(0), l.pb(s), r.pb(s); // start
y.pb(0), l.pb(g), r.pb(g); // end
qry[s].pb(m), qry[g].pb(m + 1);
vector <int> D(m + 2, -1), U(m + 2, -1);
// m -> (0), m + 1 -> (n - 1)
set <pair<int,int>> st;
for ( int i = 0; i < n; i++ ){
for ( auto &[t, u, j]: H[i] ){
if ( t > 0 ){
st.insert({u, j});
} else if ( i + 1 < n ){ // for (n - 1, 0)
st.erase({u, j});
}
}
for ( auto &j: qry[i] ){
auto it = st.lower_bound({y[j], -1});
if ( it != st.end() ){
U[j] = it -> second;
}
if ( it != st.begin() ){
D[j] = prev(it) -> second;
}
}
}
//~ cout << "Up & Down : \n";
//~ for ( int i = 0; i < m + 2; i++ ){
//~ cout << U[i] << " " << D[i] << ln;
//~ } cout << ln;
vector <vector<int>> rng(m + 2);
for ( int i = 0; i < m + 2; i++ ){
rng[i].pb(l[i]);
rng[i].pb(r[i]);
if ( U[i] == i ) U[i] = -1;
if ( D[i] == i ) D[i] = -1;
if ( U[i] != -1 ){
rng[U[i]].pb(r[i]);
}
if ( D[i] != -1 ){
rng[D[i]].pb(r[i]);
}
}
map <ar<int,2>,int> id;
vector <vector<ar<i64,2>>> adj;
int k = 0;
auto get = [&](int u, int v){
if ( !id.count({u, v}) ){
id[{u, v}] = k++;
adj.pb(vector <ar<i64,2>>());
}
return id[{u, v}];
};
for ( int i = 0; i < m + 2; i++ ){
auto &p = rng[i];
sort(all(p));
p.resize(unique(all(p)) - p.begin());
{ // assigning index
for ( auto x: p ) get(x, y[i]);
}
for ( int j = 0; j + 1 < (int)p.size(); j++ ){
int u = get(p[j], y[i]), v = get(p[j + 1], y[i]);
adj[u].pb({v, x[p[j + 1]] - x[p[j]]}); // to the right
adj[v].pb({u, x[p[j + 1]] - x[p[j]]});
//~ cout << "{" << p[j] << " " << y[i] << "} {" << p[j + 1] << " " << y[i] << "} " << x[p[j + 1]] - x[p[j]] << ln;
}
if ( D[i] != -1 ){ // down
int u = get(r[i], y[i]), v = get(r[i], y[D[i]]);
//~ cout << "{" << r[i] << " " << y[i] << "} {" << r[i] << " " << y[D[i]] << "} " << y[i] - y[D[i]] << ln;
adj[u].pb({v, y[i] - y[D[i]]});
adj[v].pb({u, y[i] - y[D[i]]});
}
if ( U[i] != -1 ){ // up
int u = get(r[i], y[i]), v = get(r[i], y[U[i]]);
//~ cout << "{" << r[i] << " " << y[i] << "} {" << r[i] << " " << y[U[i]] << "} " << -y[i] + y[U[i]] << ln;
adj[u].pb({v, -y[i] + y[U[i]]});
adj[v].pb({u, -y[i] + y[U[i]]});
}
}
vector <i64> dp(k, inf);
priority_queue <ar<i64,2>> q;
int u = get(s, 0);
dp[u] = 0;
q.push({0, u});
while ( !q.empty() ){
auto [val, u] = q.top();
q.pop();
if ( -val != dp[u] ) continue;
for ( auto &[v, w]: adj[u] ){
if ( chmin(dp[v], dp[u] + w) ){
q.push({-dp[v], v});
}
}
}
u = get(g, 0);
if ( dp[u] == inf ){
dp[u] = -1;
}
return dp[u];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
534 ms |
67984 KB |
Output is correct |
4 |
Correct |
519 ms |
74108 KB |
Output is correct |
5 |
Incorrect |
382 ms |
59388 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
20928 KB |
Output is correct |
2 |
Correct |
671 ms |
78680 KB |
Output is correct |
3 |
Correct |
733 ms |
80504 KB |
Output is correct |
4 |
Correct |
738 ms |
89436 KB |
Output is correct |
5 |
Correct |
779 ms |
89972 KB |
Output is correct |
6 |
Correct |
670 ms |
86516 KB |
Output is correct |
7 |
Correct |
335 ms |
48452 KB |
Output is correct |
8 |
Correct |
261 ms |
51328 KB |
Output is correct |
9 |
Correct |
607 ms |
85148 KB |
Output is correct |
10 |
Correct |
355 ms |
66008 KB |
Output is correct |
11 |
Correct |
7 ms |
3416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
20928 KB |
Output is correct |
2 |
Correct |
671 ms |
78680 KB |
Output is correct |
3 |
Correct |
733 ms |
80504 KB |
Output is correct |
4 |
Correct |
738 ms |
89436 KB |
Output is correct |
5 |
Correct |
779 ms |
89972 KB |
Output is correct |
6 |
Correct |
670 ms |
86516 KB |
Output is correct |
7 |
Correct |
335 ms |
48452 KB |
Output is correct |
8 |
Correct |
261 ms |
51328 KB |
Output is correct |
9 |
Correct |
607 ms |
85148 KB |
Output is correct |
10 |
Correct |
355 ms |
66008 KB |
Output is correct |
11 |
Correct |
7 ms |
3416 KB |
Output is correct |
12 |
Correct |
721 ms |
81952 KB |
Output is correct |
13 |
Correct |
667 ms |
88540 KB |
Output is correct |
14 |
Correct |
799 ms |
92020 KB |
Output is correct |
15 |
Correct |
506 ms |
74360 KB |
Output is correct |
16 |
Correct |
543 ms |
77340 KB |
Output is correct |
17 |
Correct |
630 ms |
88440 KB |
Output is correct |
18 |
Correct |
499 ms |
72668 KB |
Output is correct |
19 |
Correct |
535 ms |
77180 KB |
Output is correct |
20 |
Correct |
331 ms |
48196 KB |
Output is correct |
21 |
Correct |
16 ms |
7516 KB |
Output is correct |
22 |
Correct |
484 ms |
68220 KB |
Output is correct |
23 |
Correct |
455 ms |
66988 KB |
Output is correct |
24 |
Correct |
372 ms |
58412 KB |
Output is correct |
25 |
Correct |
415 ms |
65296 KB |
Output is correct |
26 |
Correct |
366 ms |
58636 KB |
Output is correct |
27 |
Correct |
760 ms |
92532 KB |
Output is correct |
28 |
Correct |
605 ms |
87260 KB |
Output is correct |
29 |
Correct |
755 ms |
87736 KB |
Output is correct |
30 |
Correct |
300 ms |
48400 KB |
Output is correct |
31 |
Correct |
622 ms |
85968 KB |
Output is correct |
32 |
Correct |
328 ms |
60092 KB |
Output is correct |
33 |
Correct |
343 ms |
60460 KB |
Output is correct |
34 |
Correct |
387 ms |
66500 KB |
Output is correct |
35 |
Correct |
453 ms |
68712 KB |
Output is correct |
36 |
Correct |
360 ms |
56280 KB |
Output is correct |
37 |
Correct |
327 ms |
50124 KB |
Output is correct |
38 |
Correct |
433 ms |
63892 KB |
Output is correct |
39 |
Correct |
515 ms |
76056 KB |
Output is correct |
40 |
Correct |
385 ms |
59684 KB |
Output is correct |
41 |
Correct |
306 ms |
54700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |