#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000000;
struct segment_tree{
int N;
vector<long long> ST;
segment_tree(int N2){
N = 1;
while (N < N2){
N *= 2;
}
ST = vector<long long>(N * 2 - 1, INF);
}
void update(int i, long long x){
i += N - 1;
ST[i] = x;
while (i > 0){
i = (i - 1) / 2;
ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]);
}
}
long long range_min(int L, int R, int i, int l, int r){
if (r <= L || R <= l){
return INF;
} else if (L <= l && r <= R){
return ST[i];
} else {
int m = (l + r) / 2;
return min(range_min(L, R, i * 2 + 1, l, m), range_min(L, R, i * 2 + 2, m, r));
}
}
long long range_min(int L, int R){
return range_min(L, R, 0, 0, N);
}
};
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g){
int n = x.size();
int m = y.size();
if (s != 0 || g != n - 1){
vector<tuple<int, int, int>> T;
for (int i = 0; i < n; i++){
T.push_back(make_tuple(h[i], 1, i));
}
for (int i = 0; i < m; i++){
T.push_back(make_tuple(y[i], 0, i));
}
sort(T.begin(), T.end(), greater<tuple<int, int, int>>());
vector<vector<int>> id(m);
set<int> st;
for (int i = 0; i < n + m; i++){
int t = get<1>(T[i]);
int p = get<2>(T[i]);
if (t == 0){
auto itr = st.lower_bound(l[p]);
while (true){
id[p].push_back(*itr);
if (*itr == r[p]){
break;
}
itr++;
}
}
if (t == 1){
st.insert(p);
}
}
vector<vector<int>> h2(n);
for (int i = 0; i < n; i++){
h2[i].push_back(0);
}
for (int i = 0; i < m; i++){
for (int j : id[i]){
h2[j].push_back(y[i]);
}
}
for (int i = 0; i < n; i++){
sort(h2[i].begin(), h2[i].end());
h2[i].erase(unique(h2[i].begin(), h2[i].end()), h2[i].end());
}
int V = 0;
vector<vector<int>> id2(n);
for (int i = 0; i < n; i++){
int cnt = h2[i].size();
id2[i] = vector<int>(cnt);
for (int j = 0; j < cnt; j++){
id2[i][j] = V;
V++;
}
}
vector<vector<pair<int, int>>> E(V);
for (int i = 0; i < n; i++){
int cnt = h2[i].size();
for (int j = 0; j < cnt - 1; j++){
int v = id2[i][j];
int w = id2[i][j + 1];
int d = h2[i][j + 1] - h2[i][j];
E[v].push_back(make_pair(d, w));
E[w].push_back(make_pair(d, v));
}
}
for (int i = 0; i < m; i++){
int cnt = id[i].size();
vector<int> t(cnt);
for (int j = 0; j < cnt; j++){
int p = lower_bound(h2[id[i][j]].begin(), h2[id[i][j]].end(), y[i]) - h2[id[i][j]].begin();
t[j] = id2[id[i][j]][p];
}
for (int j = 0; j < cnt - 1; j++){
int v = t[j];
int w = t[j + 1];
int d = x[id[i][j + 1]] - x[id[i][j]];
E[v].push_back(make_pair(d, w));
E[w].push_back(make_pair(d, v));
}
}
int s2 = id2[s][0];
int g2 = id2[g][0];
vector<long long> d(V, -1);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
pq.push(make_pair(0, s2));
while (!pq.empty()){
long long dd = pq.top().first;
int v = pq.top().second;
pq.pop();
if (d[v] == -1){
d[v] = dd;
for (auto P : E[v]){
int w = P.second;
if (d[w] == -1){
pq.push(make_pair(dd + P.first, w));
}
}
}
}
return d[g2];
} else {
vector<pair<int, int>> P(m);
for (int i = 0; i < m; i++){
P[i] = make_pair(y[i], i);
}
sort(P.begin(), P.end());
vector<int> pos(m);
for (int i = 0; i < m; i++){
pos[i] = lower_bound(P.begin(), P.end(), make_pair(y[i], i)) - P.begin();
}
vector<vector<int>> add(n), sub(n);
for (int i = 0; i < m; i++){
add[l[i]].push_back(i);
sub[r[i]].push_back(i);
}
segment_tree ST1(m), ST2(m);
for (int i : add[0]){
ST1.update(pos[i], y[i] * 2);
ST2.update(pos[i], 0);
}
for (int i = 1; i < n - 1; i++){
for (int j : add[i]){
long long mn1 = ST1.range_min(pos[j], m);
long long mn2 = ST2.range_min(0, pos[j]);
long long dp = min(mn1 - y[j], mn2 + y[j]);
ST1.update(pos[j], dp + y[j]);
ST2.update(pos[j], dp - y[j]);
}
for (int j : sub[i]){
ST1.update(pos[j], INF);
ST2.update(pos[j], INF);
}
}
long long ans = ST1.range_min(0, m);
ans += x[n - 1] - x[0];
if (ans > INF / 2){
return -1;
} else {
return ans;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
284 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1028 ms |
82020 KB |
Output is correct |
4 |
Correct |
1117 ms |
101544 KB |
Output is correct |
5 |
Correct |
673 ms |
89400 KB |
Output is correct |
6 |
Correct |
625 ms |
81372 KB |
Output is correct |
7 |
Correct |
670 ms |
89400 KB |
Output is correct |
8 |
Correct |
1335 ms |
102740 KB |
Output is correct |
9 |
Correct |
812 ms |
86788 KB |
Output is correct |
10 |
Correct |
1521 ms |
133092 KB |
Output is correct |
11 |
Correct |
570 ms |
52252 KB |
Output is correct |
12 |
Correct |
255 ms |
20408 KB |
Output is correct |
13 |
Correct |
234 ms |
20408 KB |
Output is correct |
14 |
Correct |
373 ms |
47924 KB |
Output is correct |
15 |
Correct |
390 ms |
51252 KB |
Output is correct |
16 |
Correct |
373 ms |
49256 KB |
Output is correct |
17 |
Correct |
346 ms |
47932 KB |
Output is correct |
18 |
Correct |
95 ms |
15160 KB |
Output is correct |
19 |
Correct |
9 ms |
2576 KB |
Output is correct |
20 |
Correct |
96 ms |
23588 KB |
Output is correct |
21 |
Correct |
200 ms |
17264 KB |
Output is correct |
22 |
Correct |
206 ms |
20400 KB |
Output is correct |
23 |
Correct |
169 ms |
19152 KB |
Output is correct |
24 |
Correct |
217 ms |
19392 KB |
Output is correct |
25 |
Correct |
194 ms |
18216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
5600 KB |
Output is correct |
2 |
Correct |
136 ms |
9164 KB |
Output is correct |
3 |
Correct |
152 ms |
10128 KB |
Output is correct |
4 |
Correct |
215 ms |
18112 KB |
Output is correct |
5 |
Correct |
195 ms |
16584 KB |
Output is correct |
6 |
Correct |
205 ms |
17860 KB |
Output is correct |
7 |
Correct |
113 ms |
13256 KB |
Output is correct |
8 |
Correct |
228 ms |
20400 KB |
Output is correct |
9 |
Correct |
225 ms |
19052 KB |
Output is correct |
10 |
Correct |
168 ms |
17860 KB |
Output is correct |
11 |
Correct |
14 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
5600 KB |
Output is correct |
2 |
Correct |
136 ms |
9164 KB |
Output is correct |
3 |
Correct |
152 ms |
10128 KB |
Output is correct |
4 |
Correct |
215 ms |
18112 KB |
Output is correct |
5 |
Correct |
195 ms |
16584 KB |
Output is correct |
6 |
Correct |
205 ms |
17860 KB |
Output is correct |
7 |
Correct |
113 ms |
13256 KB |
Output is correct |
8 |
Correct |
228 ms |
20400 KB |
Output is correct |
9 |
Correct |
225 ms |
19052 KB |
Output is correct |
10 |
Correct |
168 ms |
17860 KB |
Output is correct |
11 |
Correct |
14 ms |
3404 KB |
Output is correct |
12 |
Correct |
154 ms |
10192 KB |
Output is correct |
13 |
Correct |
218 ms |
18112 KB |
Output is correct |
14 |
Correct |
193 ms |
16560 KB |
Output is correct |
15 |
Correct |
200 ms |
17468 KB |
Output is correct |
16 |
Correct |
209 ms |
17604 KB |
Output is correct |
17 |
Correct |
202 ms |
17476 KB |
Output is correct |
18 |
Correct |
200 ms |
17488 KB |
Output is correct |
19 |
Correct |
206 ms |
17476 KB |
Output is correct |
20 |
Correct |
115 ms |
13108 KB |
Output is correct |
21 |
Correct |
29 ms |
6600 KB |
Output is correct |
22 |
Correct |
169 ms |
16176 KB |
Output is correct |
23 |
Correct |
182 ms |
16448 KB |
Output is correct |
24 |
Correct |
181 ms |
16836 KB |
Output is correct |
25 |
Correct |
179 ms |
16440 KB |
Output is correct |
26 |
Correct |
177 ms |
17588 KB |
Output is correct |
27 |
Correct |
209 ms |
17080 KB |
Output is correct |
28 |
Correct |
212 ms |
18116 KB |
Output is correct |
29 |
Correct |
202 ms |
17848 KB |
Output is correct |
30 |
Correct |
118 ms |
13252 KB |
Output is correct |
31 |
Correct |
207 ms |
18988 KB |
Output is correct |
32 |
Correct |
176 ms |
17472 KB |
Output is correct |
33 |
Correct |
181 ms |
19080 KB |
Output is correct |
34 |
Correct |
168 ms |
18368 KB |
Output is correct |
35 |
Correct |
189 ms |
18668 KB |
Output is correct |
36 |
Correct |
206 ms |
18568 KB |
Output is correct |
37 |
Correct |
210 ms |
17332 KB |
Output is correct |
38 |
Correct |
224 ms |
20452 KB |
Output is correct |
39 |
Correct |
169 ms |
19012 KB |
Output is correct |
40 |
Correct |
210 ms |
19344 KB |
Output is correct |
41 |
Correct |
201 ms |
18328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
284 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1028 ms |
82020 KB |
Output is correct |
21 |
Correct |
1117 ms |
101544 KB |
Output is correct |
22 |
Correct |
673 ms |
89400 KB |
Output is correct |
23 |
Correct |
625 ms |
81372 KB |
Output is correct |
24 |
Correct |
670 ms |
89400 KB |
Output is correct |
25 |
Correct |
1335 ms |
102740 KB |
Output is correct |
26 |
Correct |
812 ms |
86788 KB |
Output is correct |
27 |
Correct |
1521 ms |
133092 KB |
Output is correct |
28 |
Correct |
570 ms |
52252 KB |
Output is correct |
29 |
Correct |
255 ms |
20408 KB |
Output is correct |
30 |
Correct |
234 ms |
20408 KB |
Output is correct |
31 |
Correct |
373 ms |
47924 KB |
Output is correct |
32 |
Correct |
390 ms |
51252 KB |
Output is correct |
33 |
Correct |
373 ms |
49256 KB |
Output is correct |
34 |
Correct |
346 ms |
47932 KB |
Output is correct |
35 |
Correct |
95 ms |
15160 KB |
Output is correct |
36 |
Correct |
9 ms |
2576 KB |
Output is correct |
37 |
Correct |
96 ms |
23588 KB |
Output is correct |
38 |
Correct |
200 ms |
17264 KB |
Output is correct |
39 |
Correct |
206 ms |
20400 KB |
Output is correct |
40 |
Correct |
169 ms |
19152 KB |
Output is correct |
41 |
Correct |
217 ms |
19392 KB |
Output is correct |
42 |
Correct |
194 ms |
18216 KB |
Output is correct |
43 |
Correct |
70 ms |
5600 KB |
Output is correct |
44 |
Correct |
136 ms |
9164 KB |
Output is correct |
45 |
Correct |
152 ms |
10128 KB |
Output is correct |
46 |
Correct |
215 ms |
18112 KB |
Output is correct |
47 |
Correct |
195 ms |
16584 KB |
Output is correct |
48 |
Correct |
205 ms |
17860 KB |
Output is correct |
49 |
Correct |
113 ms |
13256 KB |
Output is correct |
50 |
Correct |
228 ms |
20400 KB |
Output is correct |
51 |
Correct |
225 ms |
19052 KB |
Output is correct |
52 |
Correct |
168 ms |
17860 KB |
Output is correct |
53 |
Correct |
14 ms |
3404 KB |
Output is correct |
54 |
Correct |
154 ms |
10192 KB |
Output is correct |
55 |
Correct |
218 ms |
18112 KB |
Output is correct |
56 |
Correct |
193 ms |
16560 KB |
Output is correct |
57 |
Correct |
200 ms |
17468 KB |
Output is correct |
58 |
Correct |
209 ms |
17604 KB |
Output is correct |
59 |
Correct |
202 ms |
17476 KB |
Output is correct |
60 |
Correct |
200 ms |
17488 KB |
Output is correct |
61 |
Correct |
206 ms |
17476 KB |
Output is correct |
62 |
Correct |
115 ms |
13108 KB |
Output is correct |
63 |
Correct |
29 ms |
6600 KB |
Output is correct |
64 |
Correct |
169 ms |
16176 KB |
Output is correct |
65 |
Correct |
182 ms |
16448 KB |
Output is correct |
66 |
Correct |
181 ms |
16836 KB |
Output is correct |
67 |
Correct |
179 ms |
16440 KB |
Output is correct |
68 |
Correct |
177 ms |
17588 KB |
Output is correct |
69 |
Correct |
209 ms |
17080 KB |
Output is correct |
70 |
Correct |
212 ms |
18116 KB |
Output is correct |
71 |
Correct |
202 ms |
17848 KB |
Output is correct |
72 |
Correct |
118 ms |
13252 KB |
Output is correct |
73 |
Correct |
207 ms |
18988 KB |
Output is correct |
74 |
Correct |
176 ms |
17472 KB |
Output is correct |
75 |
Correct |
181 ms |
19080 KB |
Output is correct |
76 |
Correct |
168 ms |
18368 KB |
Output is correct |
77 |
Correct |
189 ms |
18668 KB |
Output is correct |
78 |
Correct |
206 ms |
18568 KB |
Output is correct |
79 |
Correct |
210 ms |
17332 KB |
Output is correct |
80 |
Correct |
224 ms |
20452 KB |
Output is correct |
81 |
Correct |
169 ms |
19012 KB |
Output is correct |
82 |
Correct |
210 ms |
19344 KB |
Output is correct |
83 |
Correct |
201 ms |
18328 KB |
Output is correct |
84 |
Correct |
76 ms |
12036 KB |
Output is correct |
85 |
Execution timed out |
4085 ms |
494212 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |