#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef long long ll;
typedef vector<ll> vl;
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define bk back()
#define ins insert
#define lb lower_bound
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
const ll INF = 1e18;
int n, m;
const int mx = 100005;
vector<pair<int, ll>> adj[2000005];
ll dij[2000005];
ll solveShort(int N, int A, int B){
for(int i = 0; i < N; i++){
dij[i] = INF;
}
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
dij[A] = 0;
pq.push(mp(dij[A], A));
while(sz(pq)){
ll d = pq.top().f;
int node = pq.top().s;
pq.pop();
if(dij[node] < d) continue;
for(auto u: adj[node]){
ll new_d = d+u.s;
if(dij[u.f] <= new_d) continue;
dij[u.f] = new_d;
pq.push(mp(dij[u.f], u.f));
}
}
if(dij[B] == INF) return -1;
return dij[B];
}
ll min_distance(vi x, vi h, vi l, vi r, vi y, int s, int g) {
n = sz(x);
m = sz(l);
if(s == 0 && g == n-1){
// cout << "HI" << "\n";
// cout.flush();
vi ending_at[mx];
for(int i = 0; i < m; i++){
ending_at[r[i]].pb(i);
}
vi starting_at[mx];
for(int i = 0; i < m; i++){
starting_at[l[i]].pb(i);
}
ending_at[0].pb(m);
starting_at[n-1].pb(m+1);
l.pb(0);
r.pb(0);
l.pb(n-1);
r.pb(n-1);
y.pb(0);
y.pb(0);
// for(int i = 0; i < sz(y); i++){
// cout << i << " " << y[i] << "\n";
// }
vector<pair<pi, ll>> eds;
set<pi> cur_sky_heights; // height, sky index
cur_sky_heights.ins(mp(y[m], m));
for(int i = 0; i < n; i++){
//do updates
for(auto sky: ending_at[i]){
cur_sky_heights.erase(mp(y[sky], sky));
}
for(auto sky: starting_at[i]){
cur_sky_heights.ins(mp(y[sky], sky));
}
// cout << "i=" << i << "\n";
// for(auto u: cur_sky_heights){
// cout << u.f << " " << u.s << "\n";
// }
for(auto sky: ending_at[i]){
auto it_above = cur_sky_heights.lb(mp(y[sky], 0));
auto it_below = cur_sky_heights.lb(mp(y[sky]+1, 0));
if(it_above != cur_sky_heights.end()){
int to_sky = it_above->s;
eds.pb(mp(mp(sky, to_sky), abs(y[sky]-y[to_sky])));
}
if(it_below != cur_sky_heights.begin()){
it_below = prev(it_below);
int to_sky = it_below->s;
eds.pb(mp(mp(sky, to_sky), abs(y[sky]-y[to_sky])));
}
}
}
for(auto u: eds){
// cout << u.f.f << " " << u.f.s << " " << u.s << "\n";
adj[u.f.f].pb(mp(u.f.s, u.s));
}
//m is source, m+1 is sink
return x[n-1]-x[0]+solveShort(m+2, m, m+1);
}
vi sort_by_y;
for(int i = 0; i < m; i++){
sort_by_y.pb(i);
}
sort(all(sort_by_y), [&](const int& a, const int& b){
return y[a] > y[b];
});
vi sort_by_h;
for(int i = 0; i < n; i++){
sort_by_h.pb(i);
}
sort(all(sort_by_h), [&](const int& a, const int& b){
return h[a] > h[b];
});
set<int> active_buildings;
int sort_by_h_ind = 0;
vector<pair<pi, pi>> point_eds; //building index, height
// cout << "HI" << "\n";
// cout.flush();
for(auto sky: sort_by_y){
while(sort_by_h_ind < sz(sort_by_h)){
int building_ind = sort_by_h[sort_by_h_ind];
if(h[building_ind] >= y[sky]){
active_buildings.ins(building_ind);
sort_by_h_ind++;
}
else{
break;
}
}
auto it = active_buildings.lb(l[sky]);
assert(*it == l[sky]);
while(true){
assert(next(it) != active_buildings.end());
point_eds.pb(mp(mp(*it, y[sky]), mp(*next(it), y[sky])));
if(*next(it) == r[sky]) break;
it = next(it);
}
}
// cout << "HI" << "\n";
// cout.flush();
set<int> each_building[mx]; //sky walk heights for each building
for(auto u: point_eds){
// cout << u.f.f << " " << u.f.s << " " << u.s.f << " " << u.s.s << "\n";
each_building[u.f.f].ins(u.f.s);
each_building[u.s.f].ins(u.s.s);
}
for(int i = 0; i < n; i++){
each_building[i].ins(0);
each_building[i].ins(h[i]);
}
for(int i = 0; i < n; i++){
auto it = each_building[i].begin();
while(next(it) != each_building[i].end()){
int y1 = *it;
int y2 = *next(it);
point_eds.pb(mp(mp(i, y1), mp(i, y2)));
it = next(it);
}
}
map<pi, int> compress;
vpi all_nodes;
for(auto u: point_eds){
all_nodes.pb(u.f);
all_nodes.pb(u.s);
}
int cnt = 0;
for(auto u: all_nodes){
if(!compress.count(u)){
compress[u] = cnt;
cnt++;
}
}
// for(auto u: point_eds){
// cout << u.f.f << " " << u.f.s << " " << u.s.f << " " << u.s.s << "\n";
// }
for(auto u: point_eds){
ll dist = INF;
if(u.f.f == u.s.f){
dist = abs(u.s.s-u.f.s);
}
else{
assert(u.s.s == u.f.s);
dist = abs(x[u.s.f]-x[u.f.f]);
}
int a = compress[u.f];
int b = compress[u.s];
adj[a].pb(mp(b, dist));
adj[b].pb(mp(a, dist));
// cout << a << " " << b << " " << dist << "\n";
}
return solveShort(cnt, compress[mp(s, 0)], compress[mp(g, 0)]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
54212 KB |
Output is correct |
2 |
Correct |
30 ms |
54256 KB |
Output is correct |
3 |
Correct |
36 ms |
54328 KB |
Output is correct |
4 |
Correct |
33 ms |
54332 KB |
Output is correct |
5 |
Correct |
32 ms |
54448 KB |
Output is correct |
6 |
Correct |
31 ms |
54476 KB |
Output is correct |
7 |
Correct |
30 ms |
54432 KB |
Output is correct |
8 |
Correct |
31 ms |
54340 KB |
Output is correct |
9 |
Correct |
31 ms |
54300 KB |
Output is correct |
10 |
Correct |
31 ms |
54540 KB |
Output is correct |
11 |
Correct |
30 ms |
54376 KB |
Output is correct |
12 |
Correct |
31 ms |
54292 KB |
Output is correct |
13 |
Correct |
30 ms |
54288 KB |
Output is correct |
14 |
Correct |
39 ms |
54324 KB |
Output is correct |
15 |
Correct |
30 ms |
54296 KB |
Output is correct |
16 |
Correct |
30 ms |
54220 KB |
Output is correct |
17 |
Correct |
31 ms |
54248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
54252 KB |
Output is correct |
2 |
Correct |
31 ms |
54304 KB |
Output is correct |
3 |
Correct |
2738 ms |
243996 KB |
Output is correct |
4 |
Correct |
2369 ms |
278356 KB |
Output is correct |
5 |
Correct |
1843 ms |
249648 KB |
Output is correct |
6 |
Correct |
1585 ms |
227776 KB |
Output is correct |
7 |
Correct |
1694 ms |
249844 KB |
Output is correct |
8 |
Correct |
3542 ms |
295288 KB |
Output is correct |
9 |
Correct |
2004 ms |
244472 KB |
Output is correct |
10 |
Correct |
3531 ms |
358216 KB |
Output is correct |
11 |
Correct |
1319 ms |
162768 KB |
Output is correct |
12 |
Correct |
182 ms |
70152 KB |
Output is correct |
13 |
Correct |
215 ms |
72748 KB |
Output is correct |
14 |
Correct |
596 ms |
132508 KB |
Output is correct |
15 |
Correct |
676 ms |
124520 KB |
Output is correct |
16 |
Correct |
686 ms |
127152 KB |
Output is correct |
17 |
Correct |
720 ms |
125136 KB |
Output is correct |
18 |
Correct |
163 ms |
64904 KB |
Output is correct |
19 |
Correct |
68 ms |
57664 KB |
Output is correct |
20 |
Correct |
260 ms |
88204 KB |
Output is correct |
21 |
Correct |
168 ms |
68828 KB |
Output is correct |
22 |
Correct |
172 ms |
71440 KB |
Output is correct |
23 |
Correct |
242 ms |
70172 KB |
Output is correct |
24 |
Correct |
189 ms |
70636 KB |
Output is correct |
25 |
Correct |
156 ms |
69556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
61368 KB |
Output is correct |
2 |
Correct |
189 ms |
66336 KB |
Output is correct |
3 |
Correct |
186 ms |
68868 KB |
Output is correct |
4 |
Correct |
248 ms |
74828 KB |
Output is correct |
5 |
Correct |
274 ms |
73988 KB |
Output is correct |
6 |
Correct |
233 ms |
73884 KB |
Output is correct |
7 |
Correct |
140 ms |
67212 KB |
Output is correct |
8 |
Correct |
172 ms |
73976 KB |
Output is correct |
9 |
Correct |
240 ms |
74152 KB |
Output is correct |
10 |
Correct |
168 ms |
72228 KB |
Output is correct |
11 |
Incorrect |
51 ms |
55812 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
61368 KB |
Output is correct |
2 |
Correct |
189 ms |
66336 KB |
Output is correct |
3 |
Correct |
186 ms |
68868 KB |
Output is correct |
4 |
Correct |
248 ms |
74828 KB |
Output is correct |
5 |
Correct |
274 ms |
73988 KB |
Output is correct |
6 |
Correct |
233 ms |
73884 KB |
Output is correct |
7 |
Correct |
140 ms |
67212 KB |
Output is correct |
8 |
Correct |
172 ms |
73976 KB |
Output is correct |
9 |
Correct |
240 ms |
74152 KB |
Output is correct |
10 |
Correct |
168 ms |
72228 KB |
Output is correct |
11 |
Incorrect |
51 ms |
55812 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
54212 KB |
Output is correct |
2 |
Correct |
30 ms |
54256 KB |
Output is correct |
3 |
Correct |
36 ms |
54328 KB |
Output is correct |
4 |
Correct |
33 ms |
54332 KB |
Output is correct |
5 |
Correct |
32 ms |
54448 KB |
Output is correct |
6 |
Correct |
31 ms |
54476 KB |
Output is correct |
7 |
Correct |
30 ms |
54432 KB |
Output is correct |
8 |
Correct |
31 ms |
54340 KB |
Output is correct |
9 |
Correct |
31 ms |
54300 KB |
Output is correct |
10 |
Correct |
31 ms |
54540 KB |
Output is correct |
11 |
Correct |
30 ms |
54376 KB |
Output is correct |
12 |
Correct |
31 ms |
54292 KB |
Output is correct |
13 |
Correct |
30 ms |
54288 KB |
Output is correct |
14 |
Correct |
39 ms |
54324 KB |
Output is correct |
15 |
Correct |
30 ms |
54296 KB |
Output is correct |
16 |
Correct |
30 ms |
54220 KB |
Output is correct |
17 |
Correct |
31 ms |
54248 KB |
Output is correct |
18 |
Correct |
32 ms |
54252 KB |
Output is correct |
19 |
Correct |
31 ms |
54304 KB |
Output is correct |
20 |
Correct |
2738 ms |
243996 KB |
Output is correct |
21 |
Correct |
2369 ms |
278356 KB |
Output is correct |
22 |
Correct |
1843 ms |
249648 KB |
Output is correct |
23 |
Correct |
1585 ms |
227776 KB |
Output is correct |
24 |
Correct |
1694 ms |
249844 KB |
Output is correct |
25 |
Correct |
3542 ms |
295288 KB |
Output is correct |
26 |
Correct |
2004 ms |
244472 KB |
Output is correct |
27 |
Correct |
3531 ms |
358216 KB |
Output is correct |
28 |
Correct |
1319 ms |
162768 KB |
Output is correct |
29 |
Correct |
182 ms |
70152 KB |
Output is correct |
30 |
Correct |
215 ms |
72748 KB |
Output is correct |
31 |
Correct |
596 ms |
132508 KB |
Output is correct |
32 |
Correct |
676 ms |
124520 KB |
Output is correct |
33 |
Correct |
686 ms |
127152 KB |
Output is correct |
34 |
Correct |
720 ms |
125136 KB |
Output is correct |
35 |
Correct |
163 ms |
64904 KB |
Output is correct |
36 |
Correct |
68 ms |
57664 KB |
Output is correct |
37 |
Correct |
260 ms |
88204 KB |
Output is correct |
38 |
Correct |
168 ms |
68828 KB |
Output is correct |
39 |
Correct |
172 ms |
71440 KB |
Output is correct |
40 |
Correct |
242 ms |
70172 KB |
Output is correct |
41 |
Correct |
189 ms |
70636 KB |
Output is correct |
42 |
Correct |
156 ms |
69556 KB |
Output is correct |
43 |
Correct |
85 ms |
61368 KB |
Output is correct |
44 |
Correct |
189 ms |
66336 KB |
Output is correct |
45 |
Correct |
186 ms |
68868 KB |
Output is correct |
46 |
Correct |
248 ms |
74828 KB |
Output is correct |
47 |
Correct |
274 ms |
73988 KB |
Output is correct |
48 |
Correct |
233 ms |
73884 KB |
Output is correct |
49 |
Correct |
140 ms |
67212 KB |
Output is correct |
50 |
Correct |
172 ms |
73976 KB |
Output is correct |
51 |
Correct |
240 ms |
74152 KB |
Output is correct |
52 |
Correct |
168 ms |
72228 KB |
Output is correct |
53 |
Incorrect |
51 ms |
55812 KB |
Output isn't correct |