#include "walk.h"
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
struct bridge{
int l, r, y;
int lid = -1, rid = -1;
};
ll min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int from, int to){
int n = X.size();
int m = L.size();
if(from > to) swap(from, to);
vector<vector<int>> sp(m);
for(int i = 0; i < m; i++){
sp[i].eb(L[i]);
sp[i].eb(R[i]);
}
vector<vector<int>> lp(n + 1), rp(n + 1);
for(int i = 0; i < m; i++){
lp[L[i]].eb(i);
rp[R[i]].eb(i);
}
{
vector<int> v;
set<int> now;
for(int i = 0; i < n; i++){
while(!v.empty() && H[v.back()] <= H[i]) v.pob;
v.eb(i);
for(int j : lp[i]) now.insert(j);
if(i == from || i == to){
for(int j : now){
int y = Y[j];
auto it = lower_bound(iter(v), y, [&](int e, int val){ return val <= H[e]; } );
it--;
int pos = *it;
sp[j].eb(pos);
}
}
for(int j : rp[i]) now.erase(j);
}
}
{
vector<int> v;
set<int> now;
for(int i = n - 1; i >= 0; i--){
while(!v.empty() && H[v.back()] <= H[i]) v.pob;
v.eb(i);
for(int j : rp[i]) now.insert(j);
if(i == from || i == to){
for(int j : now){
int y = Y[j];
auto it = lower_bound(iter(v), y, [&](int e, int val){ return val <= H[e]; } );
it--;
int pos = *it;
sp[j].eb(pos);
}
}
for(int j : lp[i]) now.erase(j);
}
}
vector<bridge> bri;
for(int i = 0; i < m; i++){
lsort(sp[i]);
uni(sp[i]);
//cerr << "bridge " << i << " : ";
//printv(sp[i], cerr);
for(int j = 0; j + 1 < (int)sp[i].size(); j++){
bri.eb(bridge({sp[i][j], sp[i][j + 1], Y[i]}));
}
}
fill(iter(lp), vector<int>());
fill(iter(rp), vector<int>());
for(auto i : bri){
lp[i.l].eb(i.y);
rp[i.r].eb(i.y);
}
vector<pii> ps;
{
multiset<int> now;
for(int i = 0; i < n; i++){
for(int j : lp[i]) now.insert(j);
auto add = [&](int y){
//cerr << "add " << X[i] << " " << y << "\n";
ps.eb(mp(X[i], y));
auto it = now.upper_bound(y);
if(it != now.end() && *it <= H[i]) ps.eb(mp(X[i], *it));
it = now.lower_bound(y);
if(it != now.begin()) ps.eb(mp(X[i], *prev(it)));
};
for(int j : lp[i]) add(j);
for(int j : rp[i]) add(j);
for(int j : rp[i]) now.erase(now.find(j));
}
}
ps.eb(mp(X[from], 0));
ps.eb(mp(X[to], 0));
lsort(ps);
uni(ps);
//printv(ps, cerr);
auto getid = [&](int x, int y){
return lower_bound(iter(ps), mp(x, y)) - ps.begin();
};
int sid = getid(X[from], 0);
int gid = getid(X[to], 0);
for(auto& i : bri){
i.lid = getid(X[i.l], i.y);
i.rid = getid(X[i.r], i.y);
}
int sz = ps.size();
vector<vector<pll>> g(sz);
auto addedge = [&](int u, int v){
ll w = abs(ps[u].F - ps[v].F) + abs(ps[u].S - ps[v].S);
//cerr << "addedge " << u << " " << ps[u] << " " << v << " " << ps[v] << " " << w << "\n";
g[u].eb(mp(v, w));
g[v].eb(mp(u, w));
};
for(int i = 0; i + 1 < sz; i++){
if(ps[i].F == ps[i + 1].F){
addedge(i, i + 1);
}
}
vector<int> tmp(sz);
iota(iter(tmp), 0);
auto comp = [&](int a, int b){ return mp(ps[a].S, ps[a].F) < mp(ps[b].S, ps[b].F); };
sort(iter(tmp), comp);
for(auto i : bri){
int pos = lower_bound(iter(tmp), i.lid, comp) - tmp.begin();
while(tmp[pos] != i.rid){
int now = tmp[pos], nxt = tmp[pos + 1];
addedge(now, nxt);
pos++;
}
}
vector<ll> dis(sz, 1LL << 60);
std::priority_queue<pll, vector<pll>, greater<>> pq;
pq.push(mp(0, sid));
dis[sid] = 0;
while(!pq.empty()){
int now = pq.top().S;
ll d = pq.top().F;
pq.pop();
if(d != dis[now]) continue;
for(pll i : g[now]){
if(dis[i.F] <= d + i.S) continue;
dis[i.F] = d + i.S;
pq.push(mp(d + i.S, i.F));
}
}
if(dis[gid] == (1LL << 60)) return -1;
return dis[gid];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
636 ms |
68996 KB |
Output is correct |
4 |
Correct |
593 ms |
76024 KB |
Output is correct |
5 |
Correct |
408 ms |
58944 KB |
Output is correct |
6 |
Correct |
433 ms |
54392 KB |
Output is correct |
7 |
Correct |
452 ms |
59172 KB |
Output is correct |
8 |
Correct |
696 ms |
74304 KB |
Output is correct |
9 |
Correct |
519 ms |
72904 KB |
Output is correct |
10 |
Correct |
651 ms |
82128 KB |
Output is correct |
11 |
Correct |
485 ms |
59068 KB |
Output is correct |
12 |
Correct |
300 ms |
47160 KB |
Output is correct |
13 |
Correct |
632 ms |
86216 KB |
Output is correct |
14 |
Correct |
398 ms |
43740 KB |
Output is correct |
15 |
Correct |
393 ms |
45360 KB |
Output is correct |
16 |
Correct |
322 ms |
47352 KB |
Output is correct |
17 |
Correct |
347 ms |
45512 KB |
Output is correct |
18 |
Correct |
506 ms |
54568 KB |
Output is correct |
19 |
Correct |
10 ms |
2256 KB |
Output is correct |
20 |
Correct |
121 ms |
20952 KB |
Output is correct |
21 |
Correct |
294 ms |
45292 KB |
Output is correct |
22 |
Correct |
277 ms |
46092 KB |
Output is correct |
23 |
Correct |
462 ms |
54008 KB |
Output is correct |
24 |
Correct |
297 ms |
46464 KB |
Output is correct |
25 |
Correct |
302 ms |
45540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
15808 KB |
Output is correct |
2 |
Correct |
829 ms |
88000 KB |
Output is correct |
3 |
Correct |
887 ms |
91800 KB |
Output is correct |
4 |
Correct |
977 ms |
99248 KB |
Output is correct |
5 |
Correct |
1080 ms |
100668 KB |
Output is correct |
6 |
Correct |
993 ms |
94136 KB |
Output is correct |
7 |
Correct |
422 ms |
54072 KB |
Output is correct |
8 |
Correct |
291 ms |
43064 KB |
Output is correct |
9 |
Correct |
933 ms |
88980 KB |
Output is correct |
10 |
Correct |
432 ms |
57384 KB |
Output is correct |
11 |
Correct |
10 ms |
3388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
15808 KB |
Output is correct |
2 |
Correct |
829 ms |
88000 KB |
Output is correct |
3 |
Correct |
887 ms |
91800 KB |
Output is correct |
4 |
Correct |
977 ms |
99248 KB |
Output is correct |
5 |
Correct |
1080 ms |
100668 KB |
Output is correct |
6 |
Correct |
993 ms |
94136 KB |
Output is correct |
7 |
Correct |
422 ms |
54072 KB |
Output is correct |
8 |
Correct |
291 ms |
43064 KB |
Output is correct |
9 |
Correct |
933 ms |
88980 KB |
Output is correct |
10 |
Correct |
432 ms |
57384 KB |
Output is correct |
11 |
Correct |
10 ms |
3388 KB |
Output is correct |
12 |
Correct |
919 ms |
93532 KB |
Output is correct |
13 |
Correct |
817 ms |
102448 KB |
Output is correct |
14 |
Correct |
1074 ms |
104732 KB |
Output is correct |
15 |
Correct |
711 ms |
81404 KB |
Output is correct |
16 |
Correct |
726 ms |
85864 KB |
Output is correct |
17 |
Correct |
828 ms |
102196 KB |
Output is correct |
18 |
Correct |
707 ms |
81428 KB |
Output is correct |
19 |
Correct |
717 ms |
85476 KB |
Output is correct |
20 |
Correct |
452 ms |
55568 KB |
Output is correct |
21 |
Correct |
33 ms |
9252 KB |
Output is correct |
22 |
Correct |
576 ms |
79472 KB |
Output is correct |
23 |
Correct |
516 ms |
74556 KB |
Output is correct |
24 |
Correct |
390 ms |
54292 KB |
Output is correct |
25 |
Correct |
478 ms |
69380 KB |
Output is correct |
26 |
Correct |
358 ms |
42624 KB |
Output is correct |
27 |
Correct |
1285 ms |
104912 KB |
Output is correct |
28 |
Correct |
865 ms |
101164 KB |
Output is correct |
29 |
Correct |
1192 ms |
97948 KB |
Output is correct |
30 |
Correct |
462 ms |
56752 KB |
Output is correct |
31 |
Correct |
1041 ms |
92828 KB |
Output is correct |
32 |
Correct |
390 ms |
53608 KB |
Output is correct |
33 |
Correct |
354 ms |
53024 KB |
Output is correct |
34 |
Correct |
452 ms |
56592 KB |
Output is correct |
35 |
Correct |
467 ms |
60576 KB |
Output is correct |
36 |
Correct |
391 ms |
51284 KB |
Output is correct |
37 |
Correct |
290 ms |
45260 KB |
Output is correct |
38 |
Correct |
314 ms |
46372 KB |
Output is correct |
39 |
Correct |
540 ms |
53872 KB |
Output is correct |
40 |
Correct |
356 ms |
46552 KB |
Output is correct |
41 |
Correct |
322 ms |
45660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
636 ms |
68996 KB |
Output is correct |
21 |
Correct |
593 ms |
76024 KB |
Output is correct |
22 |
Correct |
408 ms |
58944 KB |
Output is correct |
23 |
Correct |
433 ms |
54392 KB |
Output is correct |
24 |
Correct |
452 ms |
59172 KB |
Output is correct |
25 |
Correct |
696 ms |
74304 KB |
Output is correct |
26 |
Correct |
519 ms |
72904 KB |
Output is correct |
27 |
Correct |
651 ms |
82128 KB |
Output is correct |
28 |
Correct |
485 ms |
59068 KB |
Output is correct |
29 |
Correct |
300 ms |
47160 KB |
Output is correct |
30 |
Correct |
632 ms |
86216 KB |
Output is correct |
31 |
Correct |
398 ms |
43740 KB |
Output is correct |
32 |
Correct |
393 ms |
45360 KB |
Output is correct |
33 |
Correct |
322 ms |
47352 KB |
Output is correct |
34 |
Correct |
347 ms |
45512 KB |
Output is correct |
35 |
Correct |
506 ms |
54568 KB |
Output is correct |
36 |
Correct |
10 ms |
2256 KB |
Output is correct |
37 |
Correct |
121 ms |
20952 KB |
Output is correct |
38 |
Correct |
294 ms |
45292 KB |
Output is correct |
39 |
Correct |
277 ms |
46092 KB |
Output is correct |
40 |
Correct |
462 ms |
54008 KB |
Output is correct |
41 |
Correct |
297 ms |
46464 KB |
Output is correct |
42 |
Correct |
302 ms |
45540 KB |
Output is correct |
43 |
Correct |
100 ms |
15808 KB |
Output is correct |
44 |
Correct |
829 ms |
88000 KB |
Output is correct |
45 |
Correct |
887 ms |
91800 KB |
Output is correct |
46 |
Correct |
977 ms |
99248 KB |
Output is correct |
47 |
Correct |
1080 ms |
100668 KB |
Output is correct |
48 |
Correct |
993 ms |
94136 KB |
Output is correct |
49 |
Correct |
422 ms |
54072 KB |
Output is correct |
50 |
Correct |
291 ms |
43064 KB |
Output is correct |
51 |
Correct |
933 ms |
88980 KB |
Output is correct |
52 |
Correct |
432 ms |
57384 KB |
Output is correct |
53 |
Correct |
10 ms |
3388 KB |
Output is correct |
54 |
Correct |
919 ms |
93532 KB |
Output is correct |
55 |
Correct |
817 ms |
102448 KB |
Output is correct |
56 |
Correct |
1074 ms |
104732 KB |
Output is correct |
57 |
Correct |
711 ms |
81404 KB |
Output is correct |
58 |
Correct |
726 ms |
85864 KB |
Output is correct |
59 |
Correct |
828 ms |
102196 KB |
Output is correct |
60 |
Correct |
707 ms |
81428 KB |
Output is correct |
61 |
Correct |
717 ms |
85476 KB |
Output is correct |
62 |
Correct |
452 ms |
55568 KB |
Output is correct |
63 |
Correct |
33 ms |
9252 KB |
Output is correct |
64 |
Correct |
576 ms |
79472 KB |
Output is correct |
65 |
Correct |
516 ms |
74556 KB |
Output is correct |
66 |
Correct |
390 ms |
54292 KB |
Output is correct |
67 |
Correct |
478 ms |
69380 KB |
Output is correct |
68 |
Correct |
358 ms |
42624 KB |
Output is correct |
69 |
Correct |
1285 ms |
104912 KB |
Output is correct |
70 |
Correct |
865 ms |
101164 KB |
Output is correct |
71 |
Correct |
1192 ms |
97948 KB |
Output is correct |
72 |
Correct |
462 ms |
56752 KB |
Output is correct |
73 |
Correct |
1041 ms |
92828 KB |
Output is correct |
74 |
Correct |
390 ms |
53608 KB |
Output is correct |
75 |
Correct |
354 ms |
53024 KB |
Output is correct |
76 |
Correct |
452 ms |
56592 KB |
Output is correct |
77 |
Correct |
467 ms |
60576 KB |
Output is correct |
78 |
Correct |
391 ms |
51284 KB |
Output is correct |
79 |
Correct |
290 ms |
45260 KB |
Output is correct |
80 |
Correct |
314 ms |
46372 KB |
Output is correct |
81 |
Correct |
540 ms |
53872 KB |
Output is correct |
82 |
Correct |
356 ms |
46552 KB |
Output is correct |
83 |
Correct |
322 ms |
45660 KB |
Output is correct |
84 |
Correct |
123 ms |
13784 KB |
Output is correct |
85 |
Correct |
1057 ms |
96400 KB |
Output is correct |
86 |
Correct |
1557 ms |
121308 KB |
Output is correct |
87 |
Correct |
66 ms |
15108 KB |
Output is correct |
88 |
Correct |
85 ms |
15456 KB |
Output is correct |
89 |
Correct |
61 ms |
15196 KB |
Output is correct |
90 |
Correct |
27 ms |
5916 KB |
Output is correct |
91 |
Correct |
2 ms |
468 KB |
Output is correct |
92 |
Correct |
21 ms |
4348 KB |
Output is correct |
93 |
Correct |
333 ms |
40808 KB |
Output is correct |
94 |
Correct |
35 ms |
9676 KB |
Output is correct |
95 |
Correct |
681 ms |
83340 KB |
Output is correct |
96 |
Correct |
574 ms |
75132 KB |
Output is correct |
97 |
Correct |
446 ms |
54824 KB |
Output is correct |
98 |
Correct |
498 ms |
69456 KB |
Output is correct |
99 |
Correct |
1922 ms |
146252 KB |
Output is correct |
100 |
Correct |
955 ms |
103072 KB |
Output is correct |
101 |
Correct |
1255 ms |
112932 KB |
Output is correct |
102 |
Correct |
457 ms |
57024 KB |
Output is correct |
103 |
Correct |
355 ms |
53360 KB |
Output is correct |
104 |
Correct |
345 ms |
52060 KB |
Output is correct |
105 |
Correct |
453 ms |
56484 KB |
Output is correct |
106 |
Correct |
397 ms |
51880 KB |
Output is correct |
107 |
Correct |
422 ms |
49564 KB |
Output is correct |
108 |
Correct |
41 ms |
8132 KB |
Output is correct |
109 |
Correct |
777 ms |
80452 KB |
Output is correct |
110 |
Correct |
808 ms |
100756 KB |
Output is correct |
111 |
Correct |
762 ms |
100572 KB |
Output is correct |
112 |
Correct |
438 ms |
59444 KB |
Output is correct |
113 |
Correct |
399 ms |
55648 KB |
Output is correct |