#include "walk.h"
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#define fs first
#define se second
using namespace std;
typedef long long llong;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;
const llong inf = 4e18;
int n, m;
int X[100001], H[100001];
int L[100001], R[100001], Y[100001];
llong dijkstra(int s, int e, const vector<vector<pii>> &edge) {
int n = int(edge.size()) - 1;
vector<llong> D(n + 1, inf);
priority_queue<pll> pq;
D[s] = 0;
pq.emplace(0, s);
while (!pq.empty()) {
llong x, d;
tie(d, x) = pq.top();
pq.pop();
d = -d;
if (D[x] != d) continue;
for (pii i : edge[x]) {
llong nd = d + i.se;
if (nd < D[i.fs]) {
D[i.fs] = nd;
pq.emplace(-nd, i.fs);
}
}
}
return D[e] < inf ? D[e] : -1;
}
llong solve(int st, int ed) {
vector<vector<int>> P(n + 1), S(n + 1), E(n + 1), M(20, vector<int>(n + 1));
for (int i = 1; i <= n; ++i) {
M[0][i] = H[i];
}
for (int i = 1; i < 20; ++i) {
for (int j = 1; j <= n; ++j) {
M[i][j] = M[i - 1][j];
int k = j + (1 << i - 1);
if (k <= n) M[i][j] = max(M[i][j], M[i - 1][k]);
}
}
P[st].push_back(0);
P[ed].push_back(0);
S[st].push_back(0);
S[ed].push_back(0);
E[st].push_back(0);
E[ed].push_back(0);
for (int i = 1; i <= m; ++i) {
P[L[i]].push_back(Y[i]);
P[R[i]].push_back(Y[i]);
S[L[i]].push_back(Y[i]);
E[R[i]].push_back(Y[i]);
if (L[i] < st && st < R[i]) {
int prv = st, nxt = st;
for (int b = 20; b--; ) {
if (M[b][nxt] < Y[i]) nxt += (1 << b);
if ((1 << b) < prv && M[b][prv - (1 << b) + 1] < Y[i]) prv -= (1 << b);
}
P[prv].push_back(Y[i]);
P[nxt].push_back(Y[i]);
}
if (L[i] < ed && ed < R[i]) {
int prv = ed, nxt = ed;
for (int b = 20; b--; ) {
if (M[b][nxt] < Y[i]) nxt += (1 << b);
if ((1 << b) < prv && M[b][prv - (1 << b) + 1] < Y[i]) prv -= (1 << b);
}
P[prv].push_back(Y[i]);
P[nxt].push_back(Y[i]);
}
}
map<int, vector<pii>> I;
vector<vector<pii>> edge(1);
auto add_edge = [&](int a, int b, int c) {
edge[a].emplace_back(b, c);
edge[b].emplace_back(a, c);
};
multiset<int> sp;
int idx = 0;
for (int i = 1; i <= n; ++i) {
for (int j : S[i]) sp.insert(j);
sort(P[i].begin(), P[i].end());
P[i].erase(unique(P[i].begin(), P[i].end()), P[i].end());
vector<pii> V;
vector<int> N;
for (int j : P[i]) {
N.push_back(j);
auto itL = sp.lower_bound(j);
auto itU = sp.upper_bound(j);
if (itL != sp.begin()) {
int x = *prev(itL);
V.emplace_back(j, x);
}
if (itU != sp.end() && *itU <= H[i]) {
int x = *itU;
V.emplace_back(x, j);
}
}
sort(V.begin(), V.end());
V.erase(unique(V.begin(), V.end()), V.end());
for (pii j : V) {
N.push_back(j.fs);
N.push_back(j.se);
}
sort(N.begin(), N.end());
N.erase(unique(N.begin(), N.end()), N.end());
edge.resize(edge.size() + N.size());
for (pii j : V) {
int a = upper_bound(N.begin(), N.end(), j.fs) - N.begin() + idx;
int b = upper_bound(N.begin(), N.end(), j.se) - N.begin() + idx;
add_edge(a, b, abs(j.fs - j.se));
}
for (int j : N) I[j].emplace_back(i, ++idx);
for (int j : E[i]) sp.erase(sp.find(j));
}
for (int i = 1; i <= m; ++i) {
const vector<pii> &V = I[Y[i]];
auto it = lower_bound(V.begin(), V.end(), pii(L[i], 0));
int pri = -1, prx;
while (it != V.end() && it->fs <= R[i]) {
if (pri != -1) add_edge(pri, it->se, X[it->fs] - prx);
prx = X[it->fs];
pri = it->se;
++it;
}
}
const vector<pii> &V = I[0];
if (int(V.size()) < 2) return -1;
st = V[0].se, ed = V.back().se;
return dijkstra(st, ed, edge);
}
llong min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
n = x.size();
if (s == g) return 0;
m = l.size();
for (int i = 1; i <= n; ++i) {
X[i] = x[i - 1];
H[i] = h[i - 1];
}
for (int i = 1; i <= m; ++i) {
L[i] = l[i - 1] + 1;
R[i] = r[i - 1] + 1;
Y[i] = y[i - 1];
}
return solve(s + 1, g + 1);
}
Compilation message
walk.cpp: In function 'llong solve(int, int)':
walk.cpp:51:33: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
int k = j + (1 << i - 1);
~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
981 ms |
59088 KB |
Output is correct |
4 |
Correct |
1077 ms |
79852 KB |
Output is correct |
5 |
Correct |
785 ms |
64712 KB |
Output is correct |
6 |
Correct |
790 ms |
61016 KB |
Output is correct |
7 |
Correct |
792 ms |
65064 KB |
Output is correct |
8 |
Correct |
1088 ms |
63020 KB |
Output is correct |
9 |
Correct |
1055 ms |
70836 KB |
Output is correct |
10 |
Correct |
1170 ms |
80872 KB |
Output is correct |
11 |
Correct |
870 ms |
54316 KB |
Output is correct |
12 |
Correct |
748 ms |
53984 KB |
Output is correct |
13 |
Correct |
1189 ms |
85388 KB |
Output is correct |
14 |
Correct |
660 ms |
50268 KB |
Output is correct |
15 |
Correct |
580 ms |
46156 KB |
Output is correct |
16 |
Correct |
426 ms |
44812 KB |
Output is correct |
17 |
Correct |
435 ms |
42916 KB |
Output is correct |
18 |
Correct |
599 ms |
58876 KB |
Output is correct |
19 |
Correct |
18 ms |
2936 KB |
Output is correct |
20 |
Correct |
183 ms |
25456 KB |
Output is correct |
21 |
Correct |
366 ms |
40120 KB |
Output is correct |
22 |
Correct |
426 ms |
44508 KB |
Output is correct |
23 |
Correct |
774 ms |
58568 KB |
Output is correct |
24 |
Correct |
380 ms |
44692 KB |
Output is correct |
25 |
Correct |
435 ms |
42512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
11184 KB |
Output is correct |
2 |
Correct |
1432 ms |
72016 KB |
Output is correct |
3 |
Correct |
1717 ms |
76536 KB |
Output is correct |
4 |
Correct |
2048 ms |
96604 KB |
Output is correct |
5 |
Correct |
2157 ms |
97288 KB |
Output is correct |
6 |
Correct |
1935 ms |
93024 KB |
Output is correct |
7 |
Correct |
780 ms |
59340 KB |
Output is correct |
8 |
Correct |
738 ms |
53980 KB |
Output is correct |
9 |
Correct |
1945 ms |
90212 KB |
Output is correct |
10 |
Correct |
609 ms |
57508 KB |
Output is correct |
11 |
Correct |
31 ms |
9208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
11184 KB |
Output is correct |
2 |
Correct |
1432 ms |
72016 KB |
Output is correct |
3 |
Correct |
1717 ms |
76536 KB |
Output is correct |
4 |
Correct |
2048 ms |
96604 KB |
Output is correct |
5 |
Correct |
2157 ms |
97288 KB |
Output is correct |
6 |
Correct |
1935 ms |
93024 KB |
Output is correct |
7 |
Correct |
780 ms |
59340 KB |
Output is correct |
8 |
Correct |
738 ms |
53980 KB |
Output is correct |
9 |
Correct |
1945 ms |
90212 KB |
Output is correct |
10 |
Correct |
609 ms |
57508 KB |
Output is correct |
11 |
Correct |
31 ms |
9208 KB |
Output is correct |
12 |
Correct |
1721 ms |
76548 KB |
Output is correct |
13 |
Correct |
1731 ms |
96556 KB |
Output is correct |
14 |
Correct |
2126 ms |
97428 KB |
Output is correct |
15 |
Correct |
1164 ms |
72952 KB |
Output is correct |
16 |
Correct |
1217 ms |
75628 KB |
Output is correct |
17 |
Correct |
1251 ms |
88216 KB |
Output is correct |
18 |
Correct |
1012 ms |
73008 KB |
Output is correct |
19 |
Correct |
1123 ms |
75564 KB |
Output is correct |
20 |
Correct |
838 ms |
58228 KB |
Output is correct |
21 |
Correct |
65 ms |
18416 KB |
Output is correct |
22 |
Correct |
1016 ms |
76072 KB |
Output is correct |
23 |
Correct |
922 ms |
72428 KB |
Output is correct |
24 |
Correct |
728 ms |
56940 KB |
Output is correct |
25 |
Correct |
895 ms |
69984 KB |
Output is correct |
26 |
Correct |
577 ms |
49780 KB |
Output is correct |
27 |
Correct |
2180 ms |
98156 KB |
Output is correct |
28 |
Correct |
1440 ms |
95336 KB |
Output is correct |
29 |
Correct |
1893 ms |
93228 KB |
Output is correct |
30 |
Correct |
770 ms |
59140 KB |
Output is correct |
31 |
Correct |
1830 ms |
90100 KB |
Output is correct |
32 |
Correct |
455 ms |
48824 KB |
Output is correct |
33 |
Correct |
480 ms |
51328 KB |
Output is correct |
34 |
Correct |
644 ms |
56336 KB |
Output is correct |
35 |
Correct |
701 ms |
56932 KB |
Output is correct |
36 |
Correct |
545 ms |
49272 KB |
Output is correct |
37 |
Correct |
400 ms |
40116 KB |
Output is correct |
38 |
Correct |
407 ms |
44640 KB |
Output is correct |
39 |
Correct |
752 ms |
58336 KB |
Output is correct |
40 |
Correct |
445 ms |
44672 KB |
Output is correct |
41 |
Correct |
431 ms |
42512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
256 KB |
Output is correct |
20 |
Correct |
981 ms |
59088 KB |
Output is correct |
21 |
Correct |
1077 ms |
79852 KB |
Output is correct |
22 |
Correct |
785 ms |
64712 KB |
Output is correct |
23 |
Correct |
790 ms |
61016 KB |
Output is correct |
24 |
Correct |
792 ms |
65064 KB |
Output is correct |
25 |
Correct |
1088 ms |
63020 KB |
Output is correct |
26 |
Correct |
1055 ms |
70836 KB |
Output is correct |
27 |
Correct |
1170 ms |
80872 KB |
Output is correct |
28 |
Correct |
870 ms |
54316 KB |
Output is correct |
29 |
Correct |
748 ms |
53984 KB |
Output is correct |
30 |
Correct |
1189 ms |
85388 KB |
Output is correct |
31 |
Correct |
660 ms |
50268 KB |
Output is correct |
32 |
Correct |
580 ms |
46156 KB |
Output is correct |
33 |
Correct |
426 ms |
44812 KB |
Output is correct |
34 |
Correct |
435 ms |
42916 KB |
Output is correct |
35 |
Correct |
599 ms |
58876 KB |
Output is correct |
36 |
Correct |
18 ms |
2936 KB |
Output is correct |
37 |
Correct |
183 ms |
25456 KB |
Output is correct |
38 |
Correct |
366 ms |
40120 KB |
Output is correct |
39 |
Correct |
426 ms |
44508 KB |
Output is correct |
40 |
Correct |
774 ms |
58568 KB |
Output is correct |
41 |
Correct |
380 ms |
44692 KB |
Output is correct |
42 |
Correct |
435 ms |
42512 KB |
Output is correct |
43 |
Correct |
107 ms |
11184 KB |
Output is correct |
44 |
Correct |
1432 ms |
72016 KB |
Output is correct |
45 |
Correct |
1717 ms |
76536 KB |
Output is correct |
46 |
Correct |
2048 ms |
96604 KB |
Output is correct |
47 |
Correct |
2157 ms |
97288 KB |
Output is correct |
48 |
Correct |
1935 ms |
93024 KB |
Output is correct |
49 |
Correct |
780 ms |
59340 KB |
Output is correct |
50 |
Correct |
738 ms |
53980 KB |
Output is correct |
51 |
Correct |
1945 ms |
90212 KB |
Output is correct |
52 |
Correct |
609 ms |
57508 KB |
Output is correct |
53 |
Correct |
31 ms |
9208 KB |
Output is correct |
54 |
Correct |
1721 ms |
76548 KB |
Output is correct |
55 |
Correct |
1731 ms |
96556 KB |
Output is correct |
56 |
Correct |
2126 ms |
97428 KB |
Output is correct |
57 |
Correct |
1164 ms |
72952 KB |
Output is correct |
58 |
Correct |
1217 ms |
75628 KB |
Output is correct |
59 |
Correct |
1251 ms |
88216 KB |
Output is correct |
60 |
Correct |
1012 ms |
73008 KB |
Output is correct |
61 |
Correct |
1123 ms |
75564 KB |
Output is correct |
62 |
Correct |
838 ms |
58228 KB |
Output is correct |
63 |
Correct |
65 ms |
18416 KB |
Output is correct |
64 |
Correct |
1016 ms |
76072 KB |
Output is correct |
65 |
Correct |
922 ms |
72428 KB |
Output is correct |
66 |
Correct |
728 ms |
56940 KB |
Output is correct |
67 |
Correct |
895 ms |
69984 KB |
Output is correct |
68 |
Correct |
577 ms |
49780 KB |
Output is correct |
69 |
Correct |
2180 ms |
98156 KB |
Output is correct |
70 |
Correct |
1440 ms |
95336 KB |
Output is correct |
71 |
Correct |
1893 ms |
93228 KB |
Output is correct |
72 |
Correct |
770 ms |
59140 KB |
Output is correct |
73 |
Correct |
1830 ms |
90100 KB |
Output is correct |
74 |
Correct |
455 ms |
48824 KB |
Output is correct |
75 |
Correct |
480 ms |
51328 KB |
Output is correct |
76 |
Correct |
644 ms |
56336 KB |
Output is correct |
77 |
Correct |
701 ms |
56932 KB |
Output is correct |
78 |
Correct |
545 ms |
49272 KB |
Output is correct |
79 |
Correct |
400 ms |
40116 KB |
Output is correct |
80 |
Correct |
407 ms |
44640 KB |
Output is correct |
81 |
Correct |
752 ms |
58336 KB |
Output is correct |
82 |
Correct |
445 ms |
44672 KB |
Output is correct |
83 |
Correct |
431 ms |
42512 KB |
Output is correct |
84 |
Correct |
88 ms |
9568 KB |
Output is correct |
85 |
Correct |
1568 ms |
77904 KB |
Output is correct |
86 |
Correct |
2269 ms |
110276 KB |
Output is correct |
87 |
Correct |
92 ms |
24412 KB |
Output is correct |
88 |
Correct |
105 ms |
24968 KB |
Output is correct |
89 |
Correct |
92 ms |
24540 KB |
Output is correct |
90 |
Correct |
32 ms |
5828 KB |
Output is correct |
91 |
Correct |
7 ms |
632 KB |
Output is correct |
92 |
Correct |
33 ms |
5112 KB |
Output is correct |
93 |
Correct |
550 ms |
48244 KB |
Output is correct |
94 |
Correct |
69 ms |
20464 KB |
Output is correct |
95 |
Correct |
1087 ms |
83084 KB |
Output is correct |
96 |
Correct |
937 ms |
76640 KB |
Output is correct |
97 |
Correct |
741 ms |
61160 KB |
Output is correct |
98 |
Correct |
728 ms |
73652 KB |
Output is correct |
99 |
Correct |
2559 ms |
122792 KB |
Output is correct |
100 |
Correct |
1623 ms |
100232 KB |
Output is correct |
101 |
Correct |
1926 ms |
104364 KB |
Output is correct |
102 |
Correct |
643 ms |
62268 KB |
Output is correct |
103 |
Correct |
472 ms |
51628 KB |
Output is correct |
104 |
Correct |
505 ms |
54688 KB |
Output is correct |
105 |
Correct |
694 ms |
59012 KB |
Output is correct |
106 |
Correct |
602 ms |
56448 KB |
Output is correct |
107 |
Correct |
547 ms |
56108 KB |
Output is correct |
108 |
Correct |
80 ms |
8376 KB |
Output is correct |
109 |
Correct |
1467 ms |
82148 KB |
Output is correct |
110 |
Correct |
1347 ms |
98088 KB |
Output is correct |
111 |
Correct |
1374 ms |
96644 KB |
Output is correct |
112 |
Correct |
672 ms |
57984 KB |
Output is correct |
113 |
Correct |
652 ms |
55520 KB |
Output is correct |