#include <bits/stdc++.h>
struct SG {
int h, l, r;
SG() { }
SG(int a, int b, int c) : h(a) , l(b) , r(c) { }
};
int n, m;
std::vector <std::pair <int, int>> g[2];
std::vector <std::vector <std::pair <int, int>>> gg;
std::vector <std::vector <int>> del[2];
std::vector <int> h;
std::vector <SG> sg;
void fix(int node) {
std::vector <std::pair <int, int>> dir[2];
dir[0] = dir[1] = { { h[node], node } };
for (int i = node + 1; i < n; i++) {
if (h[i] <= dir[1].back().first) {
continue;
}
dir[1].emplace_back(h[i], i);
}
for (int i = node - 1; i >= 0; i--) {
if (h[i] <= dir[0].back().first) {
continue;
}
dir[0].emplace_back(h[i], i);
}
auto cop = sg;
cop.clear();
for (const auto& s : sg) {
if (s.l < node && node < s.r) {
int lb[2] = {
(int) (std::lower_bound(dir[0].begin(), dir[0].end(), std::make_pair(s.h, -1)) - dir[0].begin()),
(int) (std::lower_bound(dir[1].begin(), dir[1].end(), std::make_pair(s.h, -1)) - dir[1].begin())
};
if (s.l != dir[0][lb[0]].second) {
cop.emplace_back(s.h, s.l, dir[0][lb[0]].second);
}
if (dir[0][lb[0]].second != dir[1][lb[1]].second) {
cop.emplace_back(s.h, dir[0][lb[0]].second, dir[1][lb[1]].second);
}
if (dir[1][lb[1]].second != s.r) {
cop.emplace_back(s.h, dir[1][lb[1]].second, s.r);
}
} else {
cop.emplace_back(s);
}
}
sg = cop;
}
long long min_distance(std::vector <int> a, std::vector <int> _h, std::vector <int> l, std::vector <int> r, std::vector <int> y, int f, int t) {
n = a.size();
m = l.size();
sg.resize(m);
h = _h;
for (int i = 0; i < m; i++) {
sg[i] = { y[i], l[i], r[i] };
}
fix(f);
fix(t);
del[0].resize(4'000'000);
del[1].resize(4'000'000);
for (const auto& s : sg) {
del[0][s.l].emplace_back(s.h);
del[1][s.r].emplace_back(s.h);
}
std::multiset <int> st = { 0 };
for (int i = 0; i < n; i++) {
for (int x : del[0][i]) {
st.insert(x);
}
for (int k = 0; k < 2; k++) {
for (int x : del[k][i]) {
g[0].emplace_back(a[i], x);
g[0].emplace_back(a[i], *prev(st.lower_bound(x)));
}
}
for (int x : del[1][i]) {
st.erase(st.find(x));
}
}
std::sort(g[0].begin(), g[0].end());
for (auto p : g[0]) {
if (g[1].empty() || g[1].back() != p) {
g[1].push_back(p);
}
}
g[0] = g[1];
g[1].clear();
for (auto p : g[0]) {
g[1].emplace_back(p.second, p.first);
}
std::sort(g[1].begin(), g[1].end());
gg.resize(4'000'000);
for (int i = 1; i < (int) g[0].size(); i++) {
if (g[0][i - 1].first != g[0][i].first) {
continue;
}
gg[i - 1].emplace_back(i - 0, g[0][i].second - g[0][i - 1].second);
gg[i - 0].emplace_back(i - 1, g[0][i].second - g[0][i - 1].second);
}
for (const auto& s : sg) {
int lb = std::lower_bound(g[1].begin(), g[1].end(), std::make_pair(s.h, a[s.l])) - g[1].begin();
while (g[1][lb] != (std::pair <int, int>) { s.h, a[s.r] }) {
std::pair <int, int> p[2] = {
{ g[1][lb - 0].second, g[1][lb - 0].first },
{ g[1][lb + 1].second, g[1][lb + 1].first }
};
lb++;
gg[std::lower_bound(g[0].begin(), g[0].end(), p[0]) - g[0].begin()].emplace_back(std::lower_bound(g[0].begin(), g[0].end(), p[1]) - g[0].begin(), p[1].first - p[0].first);
gg[std::lower_bound(g[0].begin(), g[0].end(), p[1]) - g[0].begin()].emplace_back(std::lower_bound(g[0].begin(), g[0].end(), p[0]) - g[0].begin(), p[1].first - p[0].first);
}
}
std::vector <long long> dd(4'000'000, 1LL << 60);
std::priority_queue <std::pair <long long, int>> pq;
{
int tmp = std::lower_bound(g[0].begin(), g[0].end(), std::make_pair(a[f], 0)) - g[0].begin();
if (tmp == (int) g[0].size() || g[0][tmp] != (std::pair <int, int>) { a[f], 0 }) {
return -1;
}
pq.push({ dd[tmp] = 0, tmp });
}
while (pq.size()) {
int v = pq.top().second;
pq.pop();
for (auto p : gg[v]) {
if (dd[p.first] <= dd[v] + p.second) {
continue;
}
dd[p.first] = dd[v] + p.second;
pq.push({ -dd[p.first], p.first });
}
}
int lb = std::lower_bound(g[0].begin(), g[0].end(), std::make_pair(a[t], 0)) - g[0].begin();
return lb == (int) g[0].size() || g[0][lb] != (std::pair <int, int>) { a[t], 0 } || dd[lb] == (1LL << 60) ? -1LL : dd[lb];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
313292 KB |
Output is correct |
2 |
Correct |
165 ms |
313340 KB |
Output is correct |
3 |
Correct |
147 ms |
313356 KB |
Output is correct |
4 |
Correct |
140 ms |
313392 KB |
Output is correct |
5 |
Correct |
140 ms |
313328 KB |
Output is correct |
6 |
Correct |
138 ms |
313420 KB |
Output is correct |
7 |
Correct |
135 ms |
313312 KB |
Output is correct |
8 |
Correct |
136 ms |
313460 KB |
Output is correct |
9 |
Correct |
138 ms |
313332 KB |
Output is correct |
10 |
Correct |
153 ms |
313352 KB |
Output is correct |
11 |
Correct |
139 ms |
313292 KB |
Output is correct |
12 |
Correct |
137 ms |
313412 KB |
Output is correct |
13 |
Correct |
150 ms |
313304 KB |
Output is correct |
14 |
Correct |
140 ms |
313412 KB |
Output is correct |
15 |
Correct |
141 ms |
313388 KB |
Output is correct |
16 |
Correct |
138 ms |
313420 KB |
Output is correct |
17 |
Correct |
136 ms |
313348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
313352 KB |
Output is correct |
2 |
Correct |
137 ms |
313304 KB |
Output is correct |
3 |
Correct |
705 ms |
342804 KB |
Output is correct |
4 |
Correct |
632 ms |
347544 KB |
Output is correct |
5 |
Correct |
471 ms |
338972 KB |
Output is correct |
6 |
Correct |
430 ms |
338356 KB |
Output is correct |
7 |
Correct |
457 ms |
339176 KB |
Output is correct |
8 |
Correct |
638 ms |
344000 KB |
Output is correct |
9 |
Correct |
541 ms |
344816 KB |
Output is correct |
10 |
Correct |
636 ms |
346696 KB |
Output is correct |
11 |
Correct |
515 ms |
340956 KB |
Output is correct |
12 |
Correct |
435 ms |
344644 KB |
Output is correct |
13 |
Correct |
595 ms |
349352 KB |
Output is correct |
14 |
Correct |
416 ms |
339248 KB |
Output is correct |
15 |
Correct |
433 ms |
343076 KB |
Output is correct |
16 |
Correct |
447 ms |
342376 KB |
Output is correct |
17 |
Correct |
423 ms |
341384 KB |
Output is correct |
18 |
Correct |
527 ms |
339760 KB |
Output is correct |
19 |
Correct |
152 ms |
314708 KB |
Output is correct |
20 |
Correct |
232 ms |
326868 KB |
Output is correct |
21 |
Correct |
395 ms |
340704 KB |
Output is correct |
22 |
Correct |
398 ms |
344800 KB |
Output is correct |
23 |
Correct |
507 ms |
345252 KB |
Output is correct |
24 |
Correct |
404 ms |
343448 KB |
Output is correct |
25 |
Correct |
440 ms |
341300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
321672 KB |
Output is correct |
2 |
Correct |
743 ms |
346504 KB |
Output is correct |
3 |
Correct |
731 ms |
347204 KB |
Output is correct |
4 |
Correct |
832 ms |
350564 KB |
Output is correct |
5 |
Correct |
921 ms |
352232 KB |
Output is correct |
6 |
Correct |
936 ms |
349748 KB |
Output is correct |
7 |
Correct |
445 ms |
333820 KB |
Output is correct |
8 |
Correct |
471 ms |
344632 KB |
Output is correct |
9 |
Correct |
816 ms |
350320 KB |
Output is correct |
10 |
Correct |
460 ms |
341076 KB |
Output is correct |
11 |
Correct |
164 ms |
314316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
321672 KB |
Output is correct |
2 |
Correct |
743 ms |
346504 KB |
Output is correct |
3 |
Correct |
731 ms |
347204 KB |
Output is correct |
4 |
Correct |
832 ms |
350564 KB |
Output is correct |
5 |
Correct |
921 ms |
352232 KB |
Output is correct |
6 |
Correct |
936 ms |
349748 KB |
Output is correct |
7 |
Correct |
445 ms |
333820 KB |
Output is correct |
8 |
Correct |
471 ms |
344632 KB |
Output is correct |
9 |
Correct |
816 ms |
350320 KB |
Output is correct |
10 |
Correct |
460 ms |
341076 KB |
Output is correct |
11 |
Correct |
164 ms |
314316 KB |
Output is correct |
12 |
Correct |
838 ms |
347332 KB |
Output is correct |
13 |
Correct |
751 ms |
349548 KB |
Output is correct |
14 |
Correct |
935 ms |
352052 KB |
Output is correct |
15 |
Correct |
773 ms |
349084 KB |
Output is correct |
16 |
Correct |
661 ms |
345700 KB |
Output is correct |
17 |
Correct |
741 ms |
349432 KB |
Output is correct |
18 |
Correct |
736 ms |
349088 KB |
Output is correct |
19 |
Correct |
668 ms |
345620 KB |
Output is correct |
20 |
Correct |
488 ms |
333004 KB |
Output is correct |
21 |
Correct |
170 ms |
315592 KB |
Output is correct |
22 |
Correct |
584 ms |
342388 KB |
Output is correct |
23 |
Correct |
535 ms |
342204 KB |
Output is correct |
24 |
Correct |
462 ms |
339260 KB |
Output is correct |
25 |
Correct |
526 ms |
340740 KB |
Output is correct |
26 |
Correct |
395 ms |
339368 KB |
Output is correct |
27 |
Correct |
862 ms |
352228 KB |
Output is correct |
28 |
Correct |
660 ms |
349452 KB |
Output is correct |
29 |
Correct |
784 ms |
349780 KB |
Output is correct |
30 |
Correct |
430 ms |
333852 KB |
Output is correct |
31 |
Correct |
804 ms |
350256 KB |
Output is correct |
32 |
Correct |
427 ms |
340812 KB |
Output is correct |
33 |
Correct |
415 ms |
342836 KB |
Output is correct |
34 |
Correct |
505 ms |
342716 KB |
Output is correct |
35 |
Correct |
485 ms |
344044 KB |
Output is correct |
36 |
Correct |
424 ms |
341996 KB |
Output is correct |
37 |
Correct |
401 ms |
340516 KB |
Output is correct |
38 |
Correct |
420 ms |
344744 KB |
Output is correct |
39 |
Correct |
528 ms |
345252 KB |
Output is correct |
40 |
Correct |
414 ms |
343364 KB |
Output is correct |
41 |
Correct |
418 ms |
341216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
313292 KB |
Output is correct |
2 |
Correct |
165 ms |
313340 KB |
Output is correct |
3 |
Correct |
147 ms |
313356 KB |
Output is correct |
4 |
Correct |
140 ms |
313392 KB |
Output is correct |
5 |
Correct |
140 ms |
313328 KB |
Output is correct |
6 |
Correct |
138 ms |
313420 KB |
Output is correct |
7 |
Correct |
135 ms |
313312 KB |
Output is correct |
8 |
Correct |
136 ms |
313460 KB |
Output is correct |
9 |
Correct |
138 ms |
313332 KB |
Output is correct |
10 |
Correct |
153 ms |
313352 KB |
Output is correct |
11 |
Correct |
139 ms |
313292 KB |
Output is correct |
12 |
Correct |
137 ms |
313412 KB |
Output is correct |
13 |
Correct |
150 ms |
313304 KB |
Output is correct |
14 |
Correct |
140 ms |
313412 KB |
Output is correct |
15 |
Correct |
141 ms |
313388 KB |
Output is correct |
16 |
Correct |
138 ms |
313420 KB |
Output is correct |
17 |
Correct |
136 ms |
313348 KB |
Output is correct |
18 |
Correct |
135 ms |
313352 KB |
Output is correct |
19 |
Correct |
137 ms |
313304 KB |
Output is correct |
20 |
Correct |
705 ms |
342804 KB |
Output is correct |
21 |
Correct |
632 ms |
347544 KB |
Output is correct |
22 |
Correct |
471 ms |
338972 KB |
Output is correct |
23 |
Correct |
430 ms |
338356 KB |
Output is correct |
24 |
Correct |
457 ms |
339176 KB |
Output is correct |
25 |
Correct |
638 ms |
344000 KB |
Output is correct |
26 |
Correct |
541 ms |
344816 KB |
Output is correct |
27 |
Correct |
636 ms |
346696 KB |
Output is correct |
28 |
Correct |
515 ms |
340956 KB |
Output is correct |
29 |
Correct |
435 ms |
344644 KB |
Output is correct |
30 |
Correct |
595 ms |
349352 KB |
Output is correct |
31 |
Correct |
416 ms |
339248 KB |
Output is correct |
32 |
Correct |
433 ms |
343076 KB |
Output is correct |
33 |
Correct |
447 ms |
342376 KB |
Output is correct |
34 |
Correct |
423 ms |
341384 KB |
Output is correct |
35 |
Correct |
527 ms |
339760 KB |
Output is correct |
36 |
Correct |
152 ms |
314708 KB |
Output is correct |
37 |
Correct |
232 ms |
326868 KB |
Output is correct |
38 |
Correct |
395 ms |
340704 KB |
Output is correct |
39 |
Correct |
398 ms |
344800 KB |
Output is correct |
40 |
Correct |
507 ms |
345252 KB |
Output is correct |
41 |
Correct |
404 ms |
343448 KB |
Output is correct |
42 |
Correct |
440 ms |
341300 KB |
Output is correct |
43 |
Correct |
236 ms |
321672 KB |
Output is correct |
44 |
Correct |
743 ms |
346504 KB |
Output is correct |
45 |
Correct |
731 ms |
347204 KB |
Output is correct |
46 |
Correct |
832 ms |
350564 KB |
Output is correct |
47 |
Correct |
921 ms |
352232 KB |
Output is correct |
48 |
Correct |
936 ms |
349748 KB |
Output is correct |
49 |
Correct |
445 ms |
333820 KB |
Output is correct |
50 |
Correct |
471 ms |
344632 KB |
Output is correct |
51 |
Correct |
816 ms |
350320 KB |
Output is correct |
52 |
Correct |
460 ms |
341076 KB |
Output is correct |
53 |
Correct |
164 ms |
314316 KB |
Output is correct |
54 |
Correct |
838 ms |
347332 KB |
Output is correct |
55 |
Correct |
751 ms |
349548 KB |
Output is correct |
56 |
Correct |
935 ms |
352052 KB |
Output is correct |
57 |
Correct |
773 ms |
349084 KB |
Output is correct |
58 |
Correct |
661 ms |
345700 KB |
Output is correct |
59 |
Correct |
741 ms |
349432 KB |
Output is correct |
60 |
Correct |
736 ms |
349088 KB |
Output is correct |
61 |
Correct |
668 ms |
345620 KB |
Output is correct |
62 |
Correct |
488 ms |
333004 KB |
Output is correct |
63 |
Correct |
170 ms |
315592 KB |
Output is correct |
64 |
Correct |
584 ms |
342388 KB |
Output is correct |
65 |
Correct |
535 ms |
342204 KB |
Output is correct |
66 |
Correct |
462 ms |
339260 KB |
Output is correct |
67 |
Correct |
526 ms |
340740 KB |
Output is correct |
68 |
Correct |
395 ms |
339368 KB |
Output is correct |
69 |
Correct |
862 ms |
352228 KB |
Output is correct |
70 |
Correct |
660 ms |
349452 KB |
Output is correct |
71 |
Correct |
784 ms |
349780 KB |
Output is correct |
72 |
Correct |
430 ms |
333852 KB |
Output is correct |
73 |
Correct |
804 ms |
350256 KB |
Output is correct |
74 |
Correct |
427 ms |
340812 KB |
Output is correct |
75 |
Correct |
415 ms |
342836 KB |
Output is correct |
76 |
Correct |
505 ms |
342716 KB |
Output is correct |
77 |
Correct |
485 ms |
344044 KB |
Output is correct |
78 |
Correct |
424 ms |
341996 KB |
Output is correct |
79 |
Correct |
401 ms |
340516 KB |
Output is correct |
80 |
Correct |
420 ms |
344744 KB |
Output is correct |
81 |
Correct |
528 ms |
345252 KB |
Output is correct |
82 |
Correct |
414 ms |
343364 KB |
Output is correct |
83 |
Correct |
418 ms |
341216 KB |
Output is correct |
84 |
Correct |
217 ms |
320392 KB |
Output is correct |
85 |
Correct |
781 ms |
348916 KB |
Output is correct |
86 |
Correct |
1167 ms |
365472 KB |
Output is correct |
87 |
Correct |
217 ms |
321116 KB |
Output is correct |
88 |
Correct |
200 ms |
320304 KB |
Output is correct |
89 |
Correct |
205 ms |
321032 KB |
Output is correct |
90 |
Correct |
166 ms |
316360 KB |
Output is correct |
91 |
Correct |
140 ms |
313480 KB |
Output is correct |
92 |
Correct |
155 ms |
315196 KB |
Output is correct |
93 |
Correct |
340 ms |
330140 KB |
Output is correct |
94 |
Correct |
165 ms |
317840 KB |
Output is correct |
95 |
Correct |
590 ms |
347612 KB |
Output is correct |
96 |
Correct |
518 ms |
346192 KB |
Output is correct |
97 |
Correct |
466 ms |
343032 KB |
Output is correct |
98 |
Correct |
532 ms |
344340 KB |
Output is correct |
99 |
Correct |
1571 ms |
379196 KB |
Output is correct |
100 |
Correct |
769 ms |
354788 KB |
Output is correct |
101 |
Correct |
1035 ms |
361756 KB |
Output is correct |
102 |
Correct |
465 ms |
336956 KB |
Output is correct |
103 |
Correct |
450 ms |
343868 KB |
Output is correct |
104 |
Correct |
422 ms |
346068 KB |
Output is correct |
105 |
Correct |
474 ms |
345648 KB |
Output is correct |
106 |
Correct |
441 ms |
345428 KB |
Output is correct |
107 |
Correct |
468 ms |
346912 KB |
Output is correct |
108 |
Correct |
175 ms |
316688 KB |
Output is correct |
109 |
Correct |
642 ms |
347280 KB |
Output is correct |
110 |
Correct |
715 ms |
352868 KB |
Output is correct |
111 |
Correct |
661 ms |
352880 KB |
Output is correct |
112 |
Correct |
487 ms |
345892 KB |
Output is correct |
113 |
Correct |
464 ms |
345352 KB |
Output is correct |