#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, s, g;
vector<int> x, h, l, r, y;
struct sky {
int lt, rt, val;
} walk[101010];
bool compare(sky a, sky b) {
if (a.rt == b.rt) return a.lt > b.lt;
return a.rt < b.rt;
}
void sort_end() {
for (int i = 0; i < m; i++) walk[i] = {l[i], r[i], y[i]};
sort(walk, walk + m, compare);
for (int i = 0; i < m; i++) {
l[i] = walk[i].lt;
r[i] = walk[i].rt;
y[i] = walk[i].val;
}
}
vector<int> up, dn;
set< pair<int, int> > pset, mset;
vector<int> add[101010], rem[101010];
void get_updn() {
for (int i = 0; i < m; i++) add[l[i]].push_back(i);
for (int i = 0; i < m; i++) rem[r[i] + 1].push_back(i);
int p = 0;
for (int i = 0; i < m; i++) {
while (p <= r[i]) {
for (int j = 0; j < add[p].size(); j++) {
int tar = add[p][j];
pset.insert({y[tar], tar});
mset.insert({-y[tar], tar});
}
for (int j = 0; j < rem[p].size(); j++) {
int tar = rem[p][j];
pset.erase({y[tar], tar});
mset.erase({-y[tar], tar});
}
p++;
}
if (pset.lower_bound({y[i], i + 1}) == pset.end()) up.push_back(-1);
else up.push_back(pset.lower_bound({y[i], i + 1})->second);
if (mset.lower_bound({-y[i], i + 1}) == mset.end()) dn.push_back(-1);
else dn.push_back(mset.lower_bound({-y[i], i + 1})->second);
}
}
vector< pair<int, ll> > adj[101010];
void get_edge() {
for (int i = 0; i < m; i++) {
adj[i].push_back({up[i], y[up[i]] - y[i]});
adj[i].push_back({dn[i], y[i] - y[dn[i]]});
}
for (int i = 0; i < m; i++) {
if (l[i] == 0) adj[m].push_back({i, y[i]});
if (r[i] == n - 1) adj[i].push_back({m + 1, y[i]});
}
}
ll dis[101010];
priority_queue< pair<ll, int> > pq;
void dijkstra() {
for (int i = 0; i <= m + 1; i++) dis[i] = 1e18;
dis[m] = 0;
pq.push({-dis[m], m});
while (!pq.empty()) {
int now = pq.top().second; ll nowdis = -pq.top().first;
pq.pop();
for (int i = 0; i < adj[now].size(); i++) {
int next = adj[now][i].first;
if (dis[next] > nowdis + adj[now][i].second) {
dis[next] = nowdis + adj[now][i].second;
pq.push({-dis[next], next});
}
}
}
}
ll min_distance(std::vector<int> X, std::vector<int> H, std::vector<int> L, std::vector<int> R, std::vector<int> Y, int S, int G) {
x = X; h = H; l = L; r = R; y = Y; s = S; g = G;
n = x.size();
m = l.size();
sort_end();
//for (int i = 0; i < m; i++) cout << l[i] << " " << r[i] << " " << y[i] << "\n";
//cout << "\n";
get_updn();
//for (int i = 0; i < m; i++) cout << up[i] << " ";
//cout << "\n";
//for (int i = 0; i < m; i++) cout << dn[i] << " ";
//cout << "\n";
get_edge();
dijkstra();
ll ans = dis[m + 1];
if (ans >= 1e18) return -1;
ans = ans + x[n - 1] - x[0];
return ans;
}
Compilation message
walk.cpp: In function 'void get_updn()':
walk.cpp:36:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int j = 0; j < add[p].size(); j++) {
| ~~^~~~~~~~~~~~~~~
walk.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int j = 0; j < rem[p].size(); j++) {
| ~~^~~~~~~~~~~~~~~
walk.cpp: In function 'void dijkstra()':
walk.cpp:77:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 0; i < adj[now].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
13764 KB |
Output is correct |
2 |
Correct |
146 ms |
19524 KB |
Output is correct |
3 |
Correct |
155 ms |
19980 KB |
Output is correct |
4 |
Correct |
165 ms |
24656 KB |
Output is correct |
5 |
Correct |
243 ms |
27164 KB |
Output is correct |
6 |
Correct |
196 ms |
27540 KB |
Output is correct |
7 |
Correct |
73 ms |
18080 KB |
Output is correct |
8 |
Correct |
81 ms |
26904 KB |
Output is correct |
9 |
Correct |
190 ms |
31872 KB |
Output is correct |
10 |
Correct |
123 ms |
27672 KB |
Output is correct |
11 |
Correct |
11 ms |
8532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
13764 KB |
Output is correct |
2 |
Correct |
146 ms |
19524 KB |
Output is correct |
3 |
Correct |
155 ms |
19980 KB |
Output is correct |
4 |
Correct |
165 ms |
24656 KB |
Output is correct |
5 |
Correct |
243 ms |
27164 KB |
Output is correct |
6 |
Correct |
196 ms |
27540 KB |
Output is correct |
7 |
Correct |
73 ms |
18080 KB |
Output is correct |
8 |
Correct |
81 ms |
26904 KB |
Output is correct |
9 |
Correct |
190 ms |
31872 KB |
Output is correct |
10 |
Correct |
123 ms |
27672 KB |
Output is correct |
11 |
Correct |
11 ms |
8532 KB |
Output is correct |
12 |
Correct |
144 ms |
20016 KB |
Output is correct |
13 |
Correct |
181 ms |
24708 KB |
Output is correct |
14 |
Correct |
208 ms |
27064 KB |
Output is correct |
15 |
Correct |
141 ms |
23940 KB |
Output is correct |
16 |
Correct |
120 ms |
23972 KB |
Output is correct |
17 |
Correct |
129 ms |
23932 KB |
Output is correct |
18 |
Correct |
120 ms |
23972 KB |
Output is correct |
19 |
Correct |
125 ms |
23976 KB |
Output is correct |
20 |
Correct |
84 ms |
18328 KB |
Output is correct |
21 |
Correct |
26 ms |
10020 KB |
Output is correct |
22 |
Correct |
108 ms |
21576 KB |
Output is correct |
23 |
Correct |
102 ms |
22080 KB |
Output is correct |
24 |
Correct |
106 ms |
22908 KB |
Output is correct |
25 |
Correct |
95 ms |
21880 KB |
Output is correct |
26 |
Correct |
97 ms |
23592 KB |
Output is correct |
27 |
Correct |
217 ms |
27060 KB |
Output is correct |
28 |
Correct |
129 ms |
24488 KB |
Output is correct |
29 |
Correct |
211 ms |
27560 KB |
Output is correct |
30 |
Correct |
75 ms |
18160 KB |
Output is correct |
31 |
Correct |
194 ms |
32028 KB |
Output is correct |
32 |
Correct |
99 ms |
25480 KB |
Output is correct |
33 |
Correct |
101 ms |
25848 KB |
Output is correct |
34 |
Correct |
113 ms |
28084 KB |
Output is correct |
35 |
Correct |
100 ms |
25716 KB |
Output is correct |
36 |
Correct |
83 ms |
25284 KB |
Output is correct |
37 |
Correct |
72 ms |
24768 KB |
Output is correct |
38 |
Correct |
76 ms |
26892 KB |
Output is correct |
39 |
Correct |
139 ms |
31936 KB |
Output is correct |
40 |
Correct |
75 ms |
26144 KB |
Output is correct |
41 |
Correct |
75 ms |
25208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |