#include <bits/stdc++.h>
#include "walk.h"
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef vector <int> vi;
const int N = 1e5 + 7;
const ll inf = 1e18;
ll dis[6*N];
map <int, int> ma[N];
map <int, set <int> > se;
vector <int> ad[N], re[N];
vector <pii> adj[6*N];
inline void Edge(int x, int y, int w) {
adj[x].pb({y, w});
adj[y].pb({x, w});
}
ll min_distance(vi x, vi h, vi l, vi r, vi y, int sta, int en) {
int n = x.size(), m = l.size();
vector <int> v, gl(n, -1), gr(n, -1);
stack <int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && h[st.top()] <= h[i]) st.pop();
if (!st.empty()) gl[i] = st.top();
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = n-1; i >= 0; i--) {
while (!st.empty() && h[st.top()] <= h[i]) st.pop();
if (!st.empty()) gr[i] = st.top();
st.push(i);
}
auto Do = [&](int z) {
v.clear();
for (int i = 0; i < m; i++) if (l[i] < z && z < r[i] && y[i] <= h[z]) {
l.pb(z);
r.pb(r[i]);
y.pb(y[i]);
r[i] = z;
}
m = l.size();
for (int i = 0; i < m; i++) if (l[i] < z && z < r[i] && y[i] > h[z]) v.pb(i);
sort(all(v), [&](int a, int b) {return y[a] < y[b];});
int le = z, ri = z;
for (int i = 0; i < v.size(); i++) {
while (h[le] < y[v[i]]) le = gl[le];
while (h[ri] < y[v[i]]) ri = gr[ri];
if (le != l[v[i]]) {
l.pb(l[v[i]]);
r.pb(le);
y.pb(y[v[i]]);
}
if (ri != r[v[i]]) {
l.pb(ri);
r.pb(r[v[i]]);
y.pb(y[v[i]]);
}
l[v[i]] = le;
r[v[i]] = ri;
}
m = l.size();
};
Do(sta);
Do(en);
int cnt = 0;
ma[sta][0] = cnt++;
ma[en][0] = cnt++;
for (int i = 0; i < m; i++) {
if (ma[l[i]].find(y[i]) == ma[l[i]].end()) ma[l[i]][y[i]] = cnt++;
if (ma[r[i]].find(y[i]) == ma[r[i]].end()) ma[r[i]][y[i]] = cnt++;
ad[l[i]].pb(y[i]);
re[r[i]].pb(y[i]);
se[y[i]].insert(l[i]);
se[y[i]].insert(r[i]);
}
multiset <int> t;
t.insert(0);
for (int i = 0; i < n; i++) {
for (auto it : ad[i]) t.insert(it);
auto down = [&](int it) {
int z = *(--t.lower_bound(it));
if (ma[i].find(z) == ma[i].end()) ma[i][z] = cnt++;
Edge(ma[i][it], ma[i][z], it-z);
se[z].insert(i);
};
for (auto it : ad[i]) down(it);
for (auto it : re[i]) down(it);
for (auto it : re[i]) t.erase(t.find(it));
}
for (int i = 0; i < m; i++) {
auto p = se[y[i]].find(l[i]);
int la = l[i]; ++p;
while (p != se[y[i]].end() && *p <= r[i]) {
Edge(ma[la][y[i]], ma[*p][y[i]], x[*p]-x[la]);
la = *p;
++p;
}
}
for (int i = 1; i < cnt; i++) dis[i] = inf;
set <pii> s;
s.insert({0, 0});
while (!s.empty()) {
auto p = s.begin();
int xx = p->S;
s.erase(p);
for (auto yy : adj[xx]) {
if (dis[xx] + yy.S < dis[yy.F]) {
if (dis[yy.F] != inf) s.erase({dis[yy.F], yy.F});
dis[yy.F] = dis[xx] + yy.S;
s.insert({dis[yy.F], yy.F});
}
}
}
if (dis[1] == inf) return -1;
return dis[1];
}
Compilation message
walk.cpp: In lambda function:
walk.cpp:61:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25176 KB |
Output is correct |
2 |
Correct |
4 ms |
25180 KB |
Output is correct |
3 |
Correct |
4 ms |
25176 KB |
Output is correct |
4 |
Correct |
4 ms |
25176 KB |
Output is correct |
5 |
Correct |
4 ms |
25176 KB |
Output is correct |
6 |
Correct |
4 ms |
25180 KB |
Output is correct |
7 |
Correct |
4 ms |
25264 KB |
Output is correct |
8 |
Correct |
4 ms |
25180 KB |
Output is correct |
9 |
Correct |
4 ms |
25196 KB |
Output is correct |
10 |
Correct |
4 ms |
25180 KB |
Output is correct |
11 |
Correct |
4 ms |
25180 KB |
Output is correct |
12 |
Correct |
3 ms |
25180 KB |
Output is correct |
13 |
Correct |
4 ms |
25180 KB |
Output is correct |
14 |
Correct |
4 ms |
25176 KB |
Output is correct |
15 |
Correct |
4 ms |
25176 KB |
Output is correct |
16 |
Correct |
4 ms |
25220 KB |
Output is correct |
17 |
Correct |
4 ms |
25176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25432 KB |
Output is correct |
2 |
Correct |
4 ms |
25180 KB |
Output is correct |
3 |
Correct |
715 ms |
100952 KB |
Output is correct |
4 |
Correct |
708 ms |
111356 KB |
Output is correct |
5 |
Correct |
399 ms |
91012 KB |
Output is correct |
6 |
Correct |
365 ms |
89684 KB |
Output is correct |
7 |
Correct |
389 ms |
91216 KB |
Output is correct |
8 |
Correct |
731 ms |
103356 KB |
Output is correct |
9 |
Correct |
535 ms |
105820 KB |
Output is correct |
10 |
Correct |
648 ms |
110012 KB |
Output is correct |
11 |
Correct |
552 ms |
96952 KB |
Output is correct |
12 |
Correct |
417 ms |
98572 KB |
Output is correct |
13 |
Correct |
613 ms |
114768 KB |
Output is correct |
14 |
Correct |
399 ms |
91180 KB |
Output is correct |
15 |
Correct |
416 ms |
89352 KB |
Output is correct |
16 |
Correct |
422 ms |
87116 KB |
Output is correct |
17 |
Correct |
366 ms |
84456 KB |
Output is correct |
18 |
Correct |
652 ms |
86212 KB |
Output is correct |
19 |
Correct |
11 ms |
28252 KB |
Output is correct |
20 |
Correct |
138 ms |
58144 KB |
Output is correct |
21 |
Correct |
342 ms |
83536 KB |
Output is correct |
22 |
Correct |
367 ms |
88144 KB |
Output is correct |
23 |
Correct |
549 ms |
100560 KB |
Output is correct |
24 |
Correct |
364 ms |
87364 KB |
Output is correct |
25 |
Correct |
400 ms |
84264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
42480 KB |
Output is correct |
2 |
Correct |
907 ms |
108624 KB |
Output is correct |
3 |
Correct |
853 ms |
110676 KB |
Output is correct |
4 |
Correct |
838 ms |
117252 KB |
Output is correct |
5 |
Correct |
973 ms |
116616 KB |
Output is correct |
6 |
Correct |
956 ms |
113828 KB |
Output is correct |
7 |
Correct |
325 ms |
76040 KB |
Output is correct |
8 |
Correct |
402 ms |
98388 KB |
Output is correct |
9 |
Correct |
949 ms |
112844 KB |
Output is correct |
10 |
Correct |
470 ms |
86944 KB |
Output is correct |
11 |
Correct |
11 ms |
27144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
42480 KB |
Output is correct |
2 |
Correct |
907 ms |
108624 KB |
Output is correct |
3 |
Correct |
853 ms |
110676 KB |
Output is correct |
4 |
Correct |
838 ms |
117252 KB |
Output is correct |
5 |
Correct |
973 ms |
116616 KB |
Output is correct |
6 |
Correct |
956 ms |
113828 KB |
Output is correct |
7 |
Correct |
325 ms |
76040 KB |
Output is correct |
8 |
Correct |
402 ms |
98388 KB |
Output is correct |
9 |
Correct |
949 ms |
112844 KB |
Output is correct |
10 |
Correct |
470 ms |
86944 KB |
Output is correct |
11 |
Correct |
11 ms |
27144 KB |
Output is correct |
12 |
Correct |
871 ms |
110416 KB |
Output is correct |
13 |
Correct |
756 ms |
117200 KB |
Output is correct |
14 |
Correct |
997 ms |
117068 KB |
Output is correct |
15 |
Correct |
623 ms |
106576 KB |
Output is correct |
16 |
Correct |
593 ms |
101200 KB |
Output is correct |
17 |
Correct |
662 ms |
111956 KB |
Output is correct |
18 |
Correct |
649 ms |
106508 KB |
Output is correct |
19 |
Correct |
594 ms |
101532 KB |
Output is correct |
20 |
Correct |
359 ms |
74832 KB |
Output is correct |
21 |
Correct |
28 ms |
30100 KB |
Output is correct |
22 |
Correct |
517 ms |
98688 KB |
Output is correct |
23 |
Correct |
518 ms |
98396 KB |
Output is correct |
24 |
Correct |
405 ms |
91476 KB |
Output is correct |
25 |
Correct |
424 ms |
95824 KB |
Output is correct |
26 |
Correct |
379 ms |
91236 KB |
Output is correct |
27 |
Correct |
1078 ms |
116816 KB |
Output is correct |
28 |
Correct |
726 ms |
117076 KB |
Output is correct |
29 |
Correct |
1016 ms |
113636 KB |
Output is correct |
30 |
Correct |
361 ms |
76160 KB |
Output is correct |
31 |
Correct |
995 ms |
112860 KB |
Output is correct |
32 |
Correct |
430 ms |
84956 KB |
Output is correct |
33 |
Correct |
444 ms |
87632 KB |
Output is correct |
34 |
Correct |
578 ms |
92240 KB |
Output is correct |
35 |
Correct |
507 ms |
92928 KB |
Output is correct |
36 |
Correct |
490 ms |
88312 KB |
Output is correct |
37 |
Correct |
382 ms |
83428 KB |
Output is correct |
38 |
Correct |
415 ms |
88316 KB |
Output is correct |
39 |
Correct |
600 ms |
100484 KB |
Output is correct |
40 |
Correct |
374 ms |
87412 KB |
Output is correct |
41 |
Correct |
384 ms |
84308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25176 KB |
Output is correct |
2 |
Correct |
4 ms |
25180 KB |
Output is correct |
3 |
Correct |
4 ms |
25176 KB |
Output is correct |
4 |
Correct |
4 ms |
25176 KB |
Output is correct |
5 |
Correct |
4 ms |
25176 KB |
Output is correct |
6 |
Correct |
4 ms |
25180 KB |
Output is correct |
7 |
Correct |
4 ms |
25264 KB |
Output is correct |
8 |
Correct |
4 ms |
25180 KB |
Output is correct |
9 |
Correct |
4 ms |
25196 KB |
Output is correct |
10 |
Correct |
4 ms |
25180 KB |
Output is correct |
11 |
Correct |
4 ms |
25180 KB |
Output is correct |
12 |
Correct |
3 ms |
25180 KB |
Output is correct |
13 |
Correct |
4 ms |
25180 KB |
Output is correct |
14 |
Correct |
4 ms |
25176 KB |
Output is correct |
15 |
Correct |
4 ms |
25176 KB |
Output is correct |
16 |
Correct |
4 ms |
25220 KB |
Output is correct |
17 |
Correct |
4 ms |
25176 KB |
Output is correct |
18 |
Correct |
4 ms |
25432 KB |
Output is correct |
19 |
Correct |
4 ms |
25180 KB |
Output is correct |
20 |
Correct |
715 ms |
100952 KB |
Output is correct |
21 |
Correct |
708 ms |
111356 KB |
Output is correct |
22 |
Correct |
399 ms |
91012 KB |
Output is correct |
23 |
Correct |
365 ms |
89684 KB |
Output is correct |
24 |
Correct |
389 ms |
91216 KB |
Output is correct |
25 |
Correct |
731 ms |
103356 KB |
Output is correct |
26 |
Correct |
535 ms |
105820 KB |
Output is correct |
27 |
Correct |
648 ms |
110012 KB |
Output is correct |
28 |
Correct |
552 ms |
96952 KB |
Output is correct |
29 |
Correct |
417 ms |
98572 KB |
Output is correct |
30 |
Correct |
613 ms |
114768 KB |
Output is correct |
31 |
Correct |
399 ms |
91180 KB |
Output is correct |
32 |
Correct |
416 ms |
89352 KB |
Output is correct |
33 |
Correct |
422 ms |
87116 KB |
Output is correct |
34 |
Correct |
366 ms |
84456 KB |
Output is correct |
35 |
Correct |
652 ms |
86212 KB |
Output is correct |
36 |
Correct |
11 ms |
28252 KB |
Output is correct |
37 |
Correct |
138 ms |
58144 KB |
Output is correct |
38 |
Correct |
342 ms |
83536 KB |
Output is correct |
39 |
Correct |
367 ms |
88144 KB |
Output is correct |
40 |
Correct |
549 ms |
100560 KB |
Output is correct |
41 |
Correct |
364 ms |
87364 KB |
Output is correct |
42 |
Correct |
400 ms |
84264 KB |
Output is correct |
43 |
Correct |
100 ms |
42480 KB |
Output is correct |
44 |
Correct |
907 ms |
108624 KB |
Output is correct |
45 |
Correct |
853 ms |
110676 KB |
Output is correct |
46 |
Correct |
838 ms |
117252 KB |
Output is correct |
47 |
Correct |
973 ms |
116616 KB |
Output is correct |
48 |
Correct |
956 ms |
113828 KB |
Output is correct |
49 |
Correct |
325 ms |
76040 KB |
Output is correct |
50 |
Correct |
402 ms |
98388 KB |
Output is correct |
51 |
Correct |
949 ms |
112844 KB |
Output is correct |
52 |
Correct |
470 ms |
86944 KB |
Output is correct |
53 |
Correct |
11 ms |
27144 KB |
Output is correct |
54 |
Correct |
871 ms |
110416 KB |
Output is correct |
55 |
Correct |
756 ms |
117200 KB |
Output is correct |
56 |
Correct |
997 ms |
117068 KB |
Output is correct |
57 |
Correct |
623 ms |
106576 KB |
Output is correct |
58 |
Correct |
593 ms |
101200 KB |
Output is correct |
59 |
Correct |
662 ms |
111956 KB |
Output is correct |
60 |
Correct |
649 ms |
106508 KB |
Output is correct |
61 |
Correct |
594 ms |
101532 KB |
Output is correct |
62 |
Correct |
359 ms |
74832 KB |
Output is correct |
63 |
Correct |
28 ms |
30100 KB |
Output is correct |
64 |
Correct |
517 ms |
98688 KB |
Output is correct |
65 |
Correct |
518 ms |
98396 KB |
Output is correct |
66 |
Correct |
405 ms |
91476 KB |
Output is correct |
67 |
Correct |
424 ms |
95824 KB |
Output is correct |
68 |
Correct |
379 ms |
91236 KB |
Output is correct |
69 |
Correct |
1078 ms |
116816 KB |
Output is correct |
70 |
Correct |
726 ms |
117076 KB |
Output is correct |
71 |
Correct |
1016 ms |
113636 KB |
Output is correct |
72 |
Correct |
361 ms |
76160 KB |
Output is correct |
73 |
Correct |
995 ms |
112860 KB |
Output is correct |
74 |
Correct |
430 ms |
84956 KB |
Output is correct |
75 |
Correct |
444 ms |
87632 KB |
Output is correct |
76 |
Correct |
578 ms |
92240 KB |
Output is correct |
77 |
Correct |
507 ms |
92928 KB |
Output is correct |
78 |
Correct |
490 ms |
88312 KB |
Output is correct |
79 |
Correct |
382 ms |
83428 KB |
Output is correct |
80 |
Correct |
415 ms |
88316 KB |
Output is correct |
81 |
Correct |
600 ms |
100484 KB |
Output is correct |
82 |
Correct |
374 ms |
87412 KB |
Output is correct |
83 |
Correct |
384 ms |
84308 KB |
Output is correct |
84 |
Correct |
77 ms |
39304 KB |
Output is correct |
85 |
Correct |
972 ms |
114108 KB |
Output is correct |
86 |
Correct |
1575 ms |
140572 KB |
Output is correct |
87 |
Correct |
47 ms |
36756 KB |
Output is correct |
88 |
Correct |
43 ms |
35924 KB |
Output is correct |
89 |
Correct |
43 ms |
36692 KB |
Output is correct |
90 |
Correct |
22 ms |
30300 KB |
Output is correct |
91 |
Correct |
5 ms |
25180 KB |
Output is correct |
92 |
Correct |
18 ms |
28764 KB |
Output is correct |
93 |
Correct |
237 ms |
59380 KB |
Output is correct |
94 |
Correct |
26 ms |
30548 KB |
Output is correct |
95 |
Correct |
565 ms |
102428 KB |
Output is correct |
96 |
Correct |
472 ms |
98920 KB |
Output is correct |
97 |
Correct |
419 ms |
91764 KB |
Output is correct |
98 |
Correct |
466 ms |
95944 KB |
Output is correct |
99 |
Correct |
2339 ms |
171960 KB |
Output is correct |
100 |
Correct |
919 ms |
118204 KB |
Output is correct |
101 |
Correct |
1605 ms |
131364 KB |
Output is correct |
102 |
Correct |
326 ms |
77360 KB |
Output is correct |
103 |
Correct |
450 ms |
85076 KB |
Output is correct |
104 |
Correct |
437 ms |
87576 KB |
Output is correct |
105 |
Correct |
556 ms |
90708 KB |
Output is correct |
106 |
Correct |
447 ms |
89428 KB |
Output is correct |
107 |
Correct |
523 ms |
94364 KB |
Output is correct |
108 |
Correct |
34 ms |
32108 KB |
Output is correct |
109 |
Correct |
664 ms |
98040 KB |
Output is correct |
110 |
Correct |
709 ms |
115780 KB |
Output is correct |
111 |
Correct |
671 ms |
115436 KB |
Output is correct |
112 |
Correct |
474 ms |
90452 KB |
Output is correct |
113 |
Correct |
434 ms |
89304 KB |
Output is correct |