#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) {
int n = x.size(), m = l.size();
// subtask #3
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(0), r.pb(0); // start
y.pb(0), l.pb(n - 1), r.pb(n - 1); // end
qry[0].pb(m), qry[n - 1].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(0, 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(n - 1, 0);
if ( dp[u] == inf ){
dp[u] = -1;
}
return dp[u];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
21444 KB |
Output is correct |
2 |
Correct |
735 ms |
80180 KB |
Output is correct |
3 |
Correct |
752 ms |
84048 KB |
Output is correct |
4 |
Correct |
771 ms |
92092 KB |
Output is correct |
5 |
Correct |
837 ms |
95396 KB |
Output is correct |
6 |
Correct |
705 ms |
85608 KB |
Output is correct |
7 |
Correct |
290 ms |
51428 KB |
Output is correct |
8 |
Correct |
265 ms |
53932 KB |
Output is correct |
9 |
Correct |
649 ms |
78036 KB |
Output is correct |
10 |
Correct |
353 ms |
64140 KB |
Output is correct |
11 |
Correct |
11 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
21444 KB |
Output is correct |
2 |
Correct |
735 ms |
80180 KB |
Output is correct |
3 |
Correct |
752 ms |
84048 KB |
Output is correct |
4 |
Correct |
771 ms |
92092 KB |
Output is correct |
5 |
Correct |
837 ms |
95396 KB |
Output is correct |
6 |
Correct |
705 ms |
85608 KB |
Output is correct |
7 |
Correct |
290 ms |
51428 KB |
Output is correct |
8 |
Correct |
265 ms |
53932 KB |
Output is correct |
9 |
Correct |
649 ms |
78036 KB |
Output is correct |
10 |
Correct |
353 ms |
64140 KB |
Output is correct |
11 |
Correct |
11 ms |
4188 KB |
Output is correct |
12 |
Correct |
764 ms |
82256 KB |
Output is correct |
13 |
Correct |
678 ms |
93096 KB |
Output is correct |
14 |
Correct |
814 ms |
94372 KB |
Output is correct |
15 |
Correct |
534 ms |
76960 KB |
Output is correct |
16 |
Correct |
546 ms |
80508 KB |
Output is correct |
17 |
Correct |
641 ms |
92576 KB |
Output is correct |
18 |
Correct |
518 ms |
78244 KB |
Output is correct |
19 |
Correct |
565 ms |
80732 KB |
Output is correct |
20 |
Correct |
301 ms |
51060 KB |
Output is correct |
21 |
Correct |
22 ms |
9564 KB |
Output is correct |
22 |
Correct |
518 ms |
73376 KB |
Output is correct |
23 |
Correct |
484 ms |
71240 KB |
Output is correct |
24 |
Correct |
404 ms |
62100 KB |
Output is correct |
25 |
Correct |
431 ms |
68404 KB |
Output is correct |
26 |
Correct |
369 ms |
62108 KB |
Output is correct |
27 |
Correct |
823 ms |
94372 KB |
Output is correct |
28 |
Correct |
616 ms |
92840 KB |
Output is correct |
29 |
Correct |
723 ms |
86380 KB |
Output is correct |
30 |
Correct |
308 ms |
51812 KB |
Output is correct |
31 |
Correct |
550 ms |
77864 KB |
Output is correct |
32 |
Correct |
323 ms |
59736 KB |
Output is correct |
33 |
Correct |
336 ms |
63336 KB |
Output is correct |
34 |
Correct |
368 ms |
63900 KB |
Output is correct |
35 |
Correct |
421 ms |
70064 KB |
Output is correct |
36 |
Correct |
328 ms |
59512 KB |
Output is correct |
37 |
Correct |
282 ms |
52852 KB |
Output is correct |
38 |
Correct |
402 ms |
66476 KB |
Output is correct |
39 |
Correct |
413 ms |
66632 KB |
Output is correct |
40 |
Correct |
357 ms |
62380 KB |
Output is correct |
41 |
Correct |
318 ms |
57156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |