#include "walk.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef pair <int, int> pii;
typedef long long ll;
const int N = 3e5 + 10;
int n, m;
vector<int> x, h, l, r, y;
int seg[N << 2];
int Build(int id, int L, int R){
if(L + 1 == R) return seg[id] = h[L];
int mid = (L + R) >> 1;
return seg[id] = max( Build(id << 1, L, mid), Build(id << 1 | 1, mid, R) );
}
int Seg_Get(int id, int l, int r, int L, int R){
if(r <= L || R <= l) return -1;
if(l <= L && R <= r) return seg[id];
int mid = (L + R) >> 1;
return max( Seg_Get(id << 1, l, r, L, mid), Seg_Get(id << 1 | 1, l, r, mid, R) );
}
int Get_Max(int l, int r){ // [l, r]
// cerr << ""
if(r < l) return -1;
return Seg_Get(1, l, r + 1, 0, n);
}
vector< pair<ll, ll> > G[N];
void Add_Edge(int a, int b){
// cerr << "!! " << a << ' ' << b << '\n';
int ls = max(l[a], l[b]);
int rs = min(r[a], r[b]);
if(max(y[a], y[b]) <= Get_Max(ls, rs)){
G[a].pb({b, abs(y[a] - y[b])});
G[b].pb({a, abs(y[a] - y[b])});
}
// cerr << "!! " << a << ' ' << b << '\n';
}
vector<int> ins[N], dl[N];
void Add(vector<int> sg){
for(int i = 0; i < N; i++) ins[i].clear(), dl[i].clear();
for(int i : sg){
ins[l[i]].pb(i);
dl[r[i]].pb(i);
}
set< pair<int, int> > st; // y id
for(int i = 0; i < N; i++){
// cerr << "^^ " << i << '\n';
for(auto u : ins[i]){
// cerr << "&& " << u << ' ' << y[u] << '\n';
st.insert({y[u], u});
auto it = st.find({y[u], u});
if(it != st.begin())
Add_Edge(it->S, prev(it) -> S);
if(next(it) != st.end())
Add_Edge(it->S, next(it) -> S);
}
// cerr << "^^ " << i << '\n';
for(auto u : dl[i]){
auto it = st.find({y[u], u});
if(it != st.begin() && next(it) != st.end())
Add_Edge(next(it) -> S, prev(it) -> S);
st.erase(it);
}
}
}
ll dis[N];
typedef pair<ll, ll> pll;
ll Djik(int src, int des){
memset(dis, 31, sizeof dis);
dis[src] = 0;
set<pll> st;
st.insert({dis[src], src});
while(!st.empty()){
int fr = st.begin() -> S;
st.erase(st.begin());
for(auto [adj, w] : G[fr]){
assert(w >= 0);
if(dis[adj] > dis[fr] + w){
st.erase({dis[adj], adj});
dis[adj] = dis[fr] + w;
st.insert({dis[adj], adj});
}
}
}
return dis[des];
}
int Get_Last(int l, int r, int yy){
for(int i = r; i >= l; i--)
if(h[i] >= yy)
return i;
return l;
}
int Get_First(int l, int r, int yy){
for(int i = l; i <= r; i++)
if(h[i] >= yy)
return i;
return r;
}
ll min_distance(vector<int> _x, vector<int> _h, vector<int> _l, vector<int> _r, vector<int> _y, int s, int g) {
if(s > g) swap(s, g);
x = _x;
h = _h;
n = _x.size();
ll ans = x[g] - x[s];
vector<int> lft, mid, rgt;
int u, v;
for(int i = 0; i < (int) _l.size(); i++){
u = _l[i];
v = _r[i];
int a = -1, b = -1, c = -1;
if(u <= s){
a = l.size();
l.pb(u);
r.pb(min(s, v));
y.pb(_y[i]);
}
if(s <= v && u <= g){
b = l.size();
l.pb(max(u, s));
r.pb(min(g, v));
y.pb(_y[i]);
}
if(g <= v){
c = l.size();
l.pb(max(u, g));
r.pb(v);
y.pb(_y[i]);
}
if(a != -1) lft.pb(a);
if(b != -1) mid.pb(b);
if(c != -1) rgt.pb(c);
if(a != -1 && b != -1){
G[a].pb({b, 2ll * (x[s] - x[ Get_Last(0, s, _y[i]) ] )});
G[b].pb({a, 2ll * (x[ Get_First(s, g, _y[i]) ] - x[s])});
}
if(b != -1 && c != -1){
G[b].pb({c, 2ll * (x[ Get_First(g, n - 1, _y[i]) ] - x[g]) });
G[c].pb({b, 2ll * (x[g] - x[ Get_Last(s, g, _y[i]) ])});
}
}
l.pb(s); r.pb(s); y.pb(0);
l.pb(s); r.pb(s); y.pb(0);
l.pb(g); r.pb(g); y.pb(0);
l.pb(g); r.pb(g); y.pb(0);
int src = l.size() - 3;
int des = l.size() - 1;
mid.pb(src - 1);
lft.pb(src);
G[src].pb({src - 1, 0});
mid.pb(des - 1);
rgt.pb(des);
G[des - 1].pb({des, 0});
m = l.size();
for(int i = 0; i < m; i++)
assert(l[i] <= r[i]);
// for(int i = 0; i < m; i++)
// assert(0 <= l[i]);
// for(int i = 0; i < m; i++)
// assert(r[i] <= n);
// l = _l;
// r = _r;
// y = _y;
// for(int i = 0; i < m; i++) mid.pb(i);
Build(1, 0, n);
// debug("A");
Add(lft);
// debug("B");
Add(mid);
// debug("C");
Add(rgt);
// debug("D");
ans += Djik(src, des);
ll max_len = 2000000000ll;
return ans > 1ll * (n + m) * max_len ? -1 : ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23756 KB |
Output is correct |
2 |
Correct |
20 ms |
23752 KB |
Output is correct |
3 |
Correct |
21 ms |
23756 KB |
Output is correct |
4 |
Correct |
20 ms |
23788 KB |
Output is correct |
5 |
Correct |
21 ms |
23768 KB |
Output is correct |
6 |
Correct |
20 ms |
23760 KB |
Output is correct |
7 |
Correct |
21 ms |
23764 KB |
Output is correct |
8 |
Correct |
21 ms |
23756 KB |
Output is correct |
9 |
Correct |
19 ms |
23748 KB |
Output is correct |
10 |
Correct |
21 ms |
23772 KB |
Output is correct |
11 |
Correct |
21 ms |
23756 KB |
Output is correct |
12 |
Correct |
20 ms |
23712 KB |
Output is correct |
13 |
Correct |
20 ms |
23756 KB |
Output is correct |
14 |
Correct |
21 ms |
23756 KB |
Output is correct |
15 |
Correct |
19 ms |
23752 KB |
Output is correct |
16 |
Correct |
20 ms |
23800 KB |
Output is correct |
17 |
Correct |
21 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23744 KB |
Output is correct |
2 |
Correct |
21 ms |
23756 KB |
Output is correct |
3 |
Correct |
287 ms |
43960 KB |
Output is correct |
4 |
Correct |
287 ms |
48612 KB |
Output is correct |
5 |
Correct |
218 ms |
42136 KB |
Output is correct |
6 |
Correct |
494 ms |
42756 KB |
Output is correct |
7 |
Correct |
211 ms |
42244 KB |
Output is correct |
8 |
Correct |
280 ms |
43992 KB |
Output is correct |
9 |
Correct |
244 ms |
47068 KB |
Output is correct |
10 |
Correct |
346 ms |
48152 KB |
Output is correct |
11 |
Correct |
255 ms |
44508 KB |
Output is correct |
12 |
Correct |
222 ms |
42412 KB |
Output is correct |
13 |
Correct |
303 ms |
50156 KB |
Output is correct |
14 |
Correct |
1718 ms |
48240 KB |
Output is correct |
15 |
Correct |
1237 ms |
46012 KB |
Output is correct |
16 |
Correct |
290 ms |
46796 KB |
Output is correct |
17 |
Correct |
249 ms |
45660 KB |
Output is correct |
18 |
Correct |
1186 ms |
96480 KB |
Output is correct |
19 |
Correct |
38 ms |
25284 KB |
Output is correct |
20 |
Correct |
888 ms |
39476 KB |
Output is correct |
21 |
Correct |
200 ms |
45824 KB |
Output is correct |
22 |
Correct |
233 ms |
45316 KB |
Output is correct |
23 |
Correct |
610 ms |
65052 KB |
Output is correct |
24 |
Correct |
215 ms |
45732 KB |
Output is correct |
25 |
Correct |
221 ms |
45560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
34112 KB |
Output is correct |
2 |
Correct |
351 ms |
43512 KB |
Output is correct |
3 |
Correct |
367 ms |
44324 KB |
Output is correct |
4 |
Correct |
456 ms |
49496 KB |
Output is correct |
5 |
Correct |
678 ms |
52292 KB |
Output is correct |
6 |
Correct |
751 ms |
56656 KB |
Output is correct |
7 |
Correct |
211 ms |
39412 KB |
Output is correct |
8 |
Correct |
189 ms |
42444 KB |
Output is correct |
9 |
Correct |
752 ms |
64016 KB |
Output is correct |
10 |
Correct |
402 ms |
56100 KB |
Output is correct |
11 |
Correct |
32 ms |
25848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
34112 KB |
Output is correct |
2 |
Correct |
351 ms |
43512 KB |
Output is correct |
3 |
Correct |
367 ms |
44324 KB |
Output is correct |
4 |
Correct |
456 ms |
49496 KB |
Output is correct |
5 |
Correct |
678 ms |
52292 KB |
Output is correct |
6 |
Correct |
751 ms |
56656 KB |
Output is correct |
7 |
Correct |
211 ms |
39412 KB |
Output is correct |
8 |
Correct |
189 ms |
42444 KB |
Output is correct |
9 |
Correct |
752 ms |
64016 KB |
Output is correct |
10 |
Correct |
402 ms |
56100 KB |
Output is correct |
11 |
Correct |
32 ms |
25848 KB |
Output is correct |
12 |
Correct |
360 ms |
44192 KB |
Output is correct |
13 |
Correct |
443 ms |
49584 KB |
Output is correct |
14 |
Correct |
603 ms |
52260 KB |
Output is correct |
15 |
Correct |
374 ms |
44964 KB |
Output is correct |
16 |
Correct |
412 ms |
52524 KB |
Output is correct |
17 |
Correct |
395 ms |
48992 KB |
Output is correct |
18 |
Correct |
354 ms |
44984 KB |
Output is correct |
19 |
Correct |
473 ms |
52368 KB |
Output is correct |
20 |
Correct |
268 ms |
40084 KB |
Output is correct |
21 |
Correct |
53 ms |
27976 KB |
Output is correct |
22 |
Correct |
275 ms |
44508 KB |
Output is correct |
23 |
Correct |
243 ms |
44120 KB |
Output is correct |
24 |
Correct |
231 ms |
41548 KB |
Output is correct |
25 |
Correct |
246 ms |
43660 KB |
Output is correct |
26 |
Correct |
215 ms |
40164 KB |
Output is correct |
27 |
Correct |
663 ms |
52668 KB |
Output is correct |
28 |
Correct |
375 ms |
49368 KB |
Output is correct |
29 |
Correct |
664 ms |
56664 KB |
Output is correct |
30 |
Correct |
228 ms |
39532 KB |
Output is correct |
31 |
Correct |
807 ms |
64076 KB |
Output is correct |
32 |
Correct |
305 ms |
50080 KB |
Output is correct |
33 |
Correct |
303 ms |
46176 KB |
Output is correct |
34 |
Correct |
427 ms |
55152 KB |
Output is correct |
35 |
Correct |
322 ms |
46808 KB |
Output is correct |
36 |
Correct |
213 ms |
44200 KB |
Output is correct |
37 |
Correct |
211 ms |
43840 KB |
Output is correct |
38 |
Correct |
233 ms |
42912 KB |
Output is correct |
39 |
Correct |
551 ms |
62988 KB |
Output is correct |
40 |
Correct |
217 ms |
43608 KB |
Output is correct |
41 |
Correct |
227 ms |
43704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
23756 KB |
Output is correct |
2 |
Correct |
20 ms |
23752 KB |
Output is correct |
3 |
Correct |
21 ms |
23756 KB |
Output is correct |
4 |
Correct |
20 ms |
23788 KB |
Output is correct |
5 |
Correct |
21 ms |
23768 KB |
Output is correct |
6 |
Correct |
20 ms |
23760 KB |
Output is correct |
7 |
Correct |
21 ms |
23764 KB |
Output is correct |
8 |
Correct |
21 ms |
23756 KB |
Output is correct |
9 |
Correct |
19 ms |
23748 KB |
Output is correct |
10 |
Correct |
21 ms |
23772 KB |
Output is correct |
11 |
Correct |
21 ms |
23756 KB |
Output is correct |
12 |
Correct |
20 ms |
23712 KB |
Output is correct |
13 |
Correct |
20 ms |
23756 KB |
Output is correct |
14 |
Correct |
21 ms |
23756 KB |
Output is correct |
15 |
Correct |
19 ms |
23752 KB |
Output is correct |
16 |
Correct |
20 ms |
23800 KB |
Output is correct |
17 |
Correct |
21 ms |
23756 KB |
Output is correct |
18 |
Correct |
21 ms |
23744 KB |
Output is correct |
19 |
Correct |
21 ms |
23756 KB |
Output is correct |
20 |
Correct |
287 ms |
43960 KB |
Output is correct |
21 |
Correct |
287 ms |
48612 KB |
Output is correct |
22 |
Correct |
218 ms |
42136 KB |
Output is correct |
23 |
Correct |
494 ms |
42756 KB |
Output is correct |
24 |
Correct |
211 ms |
42244 KB |
Output is correct |
25 |
Correct |
280 ms |
43992 KB |
Output is correct |
26 |
Correct |
244 ms |
47068 KB |
Output is correct |
27 |
Correct |
346 ms |
48152 KB |
Output is correct |
28 |
Correct |
255 ms |
44508 KB |
Output is correct |
29 |
Correct |
222 ms |
42412 KB |
Output is correct |
30 |
Correct |
303 ms |
50156 KB |
Output is correct |
31 |
Correct |
1718 ms |
48240 KB |
Output is correct |
32 |
Correct |
1237 ms |
46012 KB |
Output is correct |
33 |
Correct |
290 ms |
46796 KB |
Output is correct |
34 |
Correct |
249 ms |
45660 KB |
Output is correct |
35 |
Correct |
1186 ms |
96480 KB |
Output is correct |
36 |
Correct |
38 ms |
25284 KB |
Output is correct |
37 |
Correct |
888 ms |
39476 KB |
Output is correct |
38 |
Correct |
200 ms |
45824 KB |
Output is correct |
39 |
Correct |
233 ms |
45316 KB |
Output is correct |
40 |
Correct |
610 ms |
65052 KB |
Output is correct |
41 |
Correct |
215 ms |
45732 KB |
Output is correct |
42 |
Correct |
221 ms |
45560 KB |
Output is correct |
43 |
Correct |
117 ms |
34112 KB |
Output is correct |
44 |
Correct |
351 ms |
43512 KB |
Output is correct |
45 |
Correct |
367 ms |
44324 KB |
Output is correct |
46 |
Correct |
456 ms |
49496 KB |
Output is correct |
47 |
Correct |
678 ms |
52292 KB |
Output is correct |
48 |
Correct |
751 ms |
56656 KB |
Output is correct |
49 |
Correct |
211 ms |
39412 KB |
Output is correct |
50 |
Correct |
189 ms |
42444 KB |
Output is correct |
51 |
Correct |
752 ms |
64016 KB |
Output is correct |
52 |
Correct |
402 ms |
56100 KB |
Output is correct |
53 |
Correct |
32 ms |
25848 KB |
Output is correct |
54 |
Correct |
360 ms |
44192 KB |
Output is correct |
55 |
Correct |
443 ms |
49584 KB |
Output is correct |
56 |
Correct |
603 ms |
52260 KB |
Output is correct |
57 |
Correct |
374 ms |
44964 KB |
Output is correct |
58 |
Correct |
412 ms |
52524 KB |
Output is correct |
59 |
Correct |
395 ms |
48992 KB |
Output is correct |
60 |
Correct |
354 ms |
44984 KB |
Output is correct |
61 |
Correct |
473 ms |
52368 KB |
Output is correct |
62 |
Correct |
268 ms |
40084 KB |
Output is correct |
63 |
Correct |
53 ms |
27976 KB |
Output is correct |
64 |
Correct |
275 ms |
44508 KB |
Output is correct |
65 |
Correct |
243 ms |
44120 KB |
Output is correct |
66 |
Correct |
231 ms |
41548 KB |
Output is correct |
67 |
Correct |
246 ms |
43660 KB |
Output is correct |
68 |
Correct |
215 ms |
40164 KB |
Output is correct |
69 |
Correct |
663 ms |
52668 KB |
Output is correct |
70 |
Correct |
375 ms |
49368 KB |
Output is correct |
71 |
Correct |
664 ms |
56664 KB |
Output is correct |
72 |
Correct |
228 ms |
39532 KB |
Output is correct |
73 |
Correct |
807 ms |
64076 KB |
Output is correct |
74 |
Correct |
305 ms |
50080 KB |
Output is correct |
75 |
Correct |
303 ms |
46176 KB |
Output is correct |
76 |
Correct |
427 ms |
55152 KB |
Output is correct |
77 |
Correct |
322 ms |
46808 KB |
Output is correct |
78 |
Correct |
213 ms |
44200 KB |
Output is correct |
79 |
Correct |
211 ms |
43840 KB |
Output is correct |
80 |
Correct |
233 ms |
42912 KB |
Output is correct |
81 |
Correct |
551 ms |
62988 KB |
Output is correct |
82 |
Correct |
217 ms |
43608 KB |
Output is correct |
83 |
Correct |
227 ms |
43704 KB |
Output is correct |
84 |
Correct |
110 ms |
32428 KB |
Output is correct |
85 |
Correct |
416 ms |
48280 KB |
Output is correct |
86 |
Correct |
1006 ms |
73128 KB |
Output is correct |
87 |
Correct |
66 ms |
30912 KB |
Output is correct |
88 |
Correct |
86 ms |
32132 KB |
Output is correct |
89 |
Correct |
69 ms |
30920 KB |
Output is correct |
90 |
Correct |
46 ms |
26696 KB |
Output is correct |
91 |
Correct |
22 ms |
23892 KB |
Output is correct |
92 |
Correct |
32 ms |
25300 KB |
Output is correct |
93 |
Correct |
168 ms |
38588 KB |
Output is correct |
94 |
Correct |
54 ms |
29568 KB |
Output is correct |
95 |
Correct |
322 ms |
48684 KB |
Output is correct |
96 |
Correct |
313 ms |
47916 KB |
Output is correct |
97 |
Correct |
798 ms |
47980 KB |
Output is correct |
98 |
Correct |
238 ms |
46812 KB |
Output is correct |
99 |
Correct |
1444 ms |
89076 KB |
Output is correct |
100 |
Correct |
396 ms |
53172 KB |
Output is correct |
101 |
Correct |
899 ms |
68180 KB |
Output is correct |
102 |
Correct |
197 ms |
42284 KB |
Output is correct |
103 |
Correct |
313 ms |
52048 KB |
Output is correct |
104 |
Correct |
311 ms |
48944 KB |
Output is correct |
105 |
Correct |
440 ms |
56592 KB |
Output is correct |
106 |
Correct |
814 ms |
53956 KB |
Output is correct |
107 |
Correct |
934 ms |
54568 KB |
Output is correct |
108 |
Correct |
57 ms |
26336 KB |
Output is correct |
109 |
Correct |
484 ms |
49672 KB |
Output is correct |
110 |
Correct |
362 ms |
52248 KB |
Output is correct |
111 |
Correct |
387 ms |
52228 KB |
Output is correct |
112 |
Correct |
372 ms |
49388 KB |
Output is correct |
113 |
Correct |
344 ms |
47420 KB |
Output is correct |