#include "bits/stdc++.h"
using namespace std;
#include "walk.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#define ar array
typedef long long ll;
const ll inf = 2e18;
const int N = 1e5 + 5;
struct ST{
vector<ll> tree;
vector<ar<int, 2>> c;
int last;
void add(){
tree.push_back(inf);
c.push_back({});
last++;
}
ST(){ add(); }
void umin(int i, ll v, int lx = 0, int rx = N, int x = 0){
if(lx == rx) {
tree[x] = min(tree[x], v);
return;
} int m = (lx + rx) >> 1;
if(i <= m){
if(!c[x][0]) c[x][0] = last, add();
umin(i, v, lx, m, c[x][0]);
} else {
if(!c[x][1]) c[x][1] = last, add();
umin(i, v, m + 1, rx, c[x][1]);
}
tree[x] = inf;
if(c[x][0]) tree[x] = min(tree[x], tree[c[x][0]]);
if(c[x][1]) tree[x] = min(tree[x], tree[c[x][1]]);
}
ll get(int l, int r, int lx = 0, int rx = N, int x = 0){
if(lx > r || rx < l) return inf;
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx) >> 1;
ll res = inf;
if(c[x][0]) res = min(res, get(l, r, lx, m, c[x][0]));
if(c[x][1]) res = min(res, get(l, r, m + 1, rx, c[x][1]));
return res;
}
};
struct ST2{
ST tree[N];
void umin(int j, int i, ll v){
for(;i<N;i+=(i&-i)){
tree[i].umin(j, v);
}
}
ll get(int l, int r, int i){
ll mn = inf;
for(;i>0;i-=(i&-i)) mn = min(mn, tree[i].get(l, r));
return mn;
}
}add, sub;
vector<ar<int, 2>> edges[N * 11];
ll d[N * 11];
#define sow cout<<"here"<<endl;
ll 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();
if(s == 0 && g == n - 1){
int n = x.size(), q = l.size();
vector<int> p(q); iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j){
return r[i] < r[j];
});
vector<int> tot;
vector<ll> dp(q, inf);
for(int i=0;i<q;i++){
tot.push_back(y[i]);
}
sort(tot.begin(), tot.end());
tot.erase(unique(tot.begin(), tot.end()), tot.end());
ll res = inf;
sub.umin(0, 1, 0);
add.umin(0, q + 2, 0);
for(auto i : p){
int j = lower_bound(tot.begin(), tot.end(), y[i]) - tot.begin() + 2;
dp[i] = min(dp[i], sub.get(l[i], r[i], j) + y[i]);
dp[i] = min(dp[i], add.get(l[i], r[i], q - j + 3) - y[i]);
//~ cout<<l[i]<<" "<<r[i]<<" "<<dp[i]<<"\n";
sub.umin(r[i], j, dp[i] - y[i]);
add.umin(r[i], q - j + 3, dp[i] + y[i]);
if(r[i] == n - 1) res = min(res, dp[i] + y[i]);
}
res += (x[n - 1] - x[0]);
if(res < 1e18){
return res;
} else {
return -1;
}
} else {
int q = l.size();
vector<int> p(q); iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j){
return y[i] > y[j];
});
vector<int> P(n); iota(P.begin(), P.end(), 0);
sort(P.begin(), P.end(), [&](int i, int j){
return h[i] > h[j];
});
vector<int> last(n, -1), id(n, -1);
set<int> ss;
int j = 0, v = 0;
for(auto i : p){
while(j < n && h[P[j]] >= y[i]){
ss.insert(P[j]);
j++;
}
//~ for(auto x : ss) cout<<x<<" ";
//~ cout<<"\n";
//~ cout<<l[i]<<" "<<r[i]<<"\n";
//~ int k = lower_bound(x.begin(), x.end(), l[i]) - x.begin();
auto it = ss.lower_bound(l[i]);
int tmp = -1, X = -1;
while(it != ss.end() && *it <= r[i]){
int k = *it;
//~ cout<<k<<" ";
if(~tmp){
edges[v].push_back({tmp, x[k] - X});
edges[tmp].push_back({v, x[k] - X});
}
if(~last[k]){
edges[v].push_back({last[k], y[id[k]] - y[i]});
edges[last[k]].push_back({v, y[id[k]] - y[i]});
}
last[k] = v, id[k] = i;
tmp = v, X = x[*it];
v++;
it++;
}
//~ cout<<"\n";
}
for(int i=0;i<n;i++){
if(~last[i]){
edges[v].push_back({last[i], y[id[i]]});
edges[last[i]].push_back({v, y[id[i]]});
last[i] = v;
v++;
}
}
//~ last[s], last[g]
if(last[s] == -1 || last[g] == -1) return -1;
s = last[s], g = last[g];
memset(d, 31, sizeof d);
priority_queue<ar<ll, 2>, vector<ar<ll, 2>>, greater<ar<ll, 2>> > pq;
d[s] = 0; pq.push({0, s});
while(!pq.empty()){
auto [D, u] = pq.top(); pq.pop();
if(D > d[u]) continue;
for(auto x : edges[u]){
if(d[x[0]] > d[u] + x[1]){
d[x[0]] = d[u] + x[1];
pq.push({d[x[0]], x[0]});
}
}
}
if(d[g] > inf) return -1;
return d[g];
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
58196 KB |
Output is correct |
2 |
Correct |
42 ms |
49584 KB |
Output is correct |
3 |
Correct |
42 ms |
58180 KB |
Output is correct |
4 |
Correct |
36 ms |
49612 KB |
Output is correct |
5 |
Correct |
39 ms |
58244 KB |
Output is correct |
6 |
Correct |
38 ms |
58188 KB |
Output is correct |
7 |
Correct |
41 ms |
58188 KB |
Output is correct |
8 |
Correct |
39 ms |
58156 KB |
Output is correct |
9 |
Correct |
38 ms |
58188 KB |
Output is correct |
10 |
Correct |
39 ms |
58272 KB |
Output is correct |
11 |
Correct |
38 ms |
58108 KB |
Output is correct |
12 |
Correct |
40 ms |
58148 KB |
Output is correct |
13 |
Correct |
39 ms |
58100 KB |
Output is correct |
14 |
Correct |
39 ms |
58144 KB |
Output is correct |
15 |
Correct |
34 ms |
49536 KB |
Output is correct |
16 |
Correct |
46 ms |
49660 KB |
Output is correct |
17 |
Correct |
43 ms |
49664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
58132 KB |
Output is correct |
2 |
Correct |
46 ms |
49536 KB |
Output is correct |
3 |
Correct |
413 ms |
94932 KB |
Output is correct |
4 |
Correct |
560 ms |
103860 KB |
Output is correct |
5 |
Correct |
335 ms |
98524 KB |
Output is correct |
6 |
Correct |
313 ms |
94668 KB |
Output is correct |
7 |
Correct |
344 ms |
98528 KB |
Output is correct |
8 |
Correct |
540 ms |
104364 KB |
Output is correct |
9 |
Correct |
423 ms |
97100 KB |
Output is correct |
10 |
Correct |
696 ms |
118148 KB |
Output is correct |
11 |
Correct |
314 ms |
81404 KB |
Output is correct |
12 |
Correct |
1618 ms |
401824 KB |
Output is correct |
13 |
Correct |
1619 ms |
405388 KB |
Output is correct |
14 |
Correct |
210 ms |
69956 KB |
Output is correct |
15 |
Correct |
279 ms |
79380 KB |
Output is correct |
16 |
Correct |
229 ms |
79320 KB |
Output is correct |
17 |
Correct |
221 ms |
78476 KB |
Output is correct |
18 |
Correct |
879 ms |
152100 KB |
Output is correct |
19 |
Correct |
51 ms |
59172 KB |
Output is correct |
20 |
Correct |
107 ms |
68352 KB |
Output is correct |
21 |
Correct |
551 ms |
112108 KB |
Output is correct |
22 |
Correct |
543 ms |
115484 KB |
Output is correct |
23 |
Correct |
994 ms |
185896 KB |
Output is correct |
24 |
Correct |
587 ms |
137620 KB |
Output is correct |
25 |
Correct |
656 ms |
135052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
66088 KB |
Output is correct |
2 |
Correct |
1529 ms |
245780 KB |
Output is correct |
3 |
Correct |
1865 ms |
312748 KB |
Output is correct |
4 |
Correct |
2103 ms |
400880 KB |
Output is correct |
5 |
Correct |
1720 ms |
362276 KB |
Output is correct |
6 |
Correct |
2324 ms |
357428 KB |
Output is correct |
7 |
Correct |
795 ms |
233108 KB |
Output is correct |
8 |
Correct |
1597 ms |
405104 KB |
Output is correct |
9 |
Correct |
2044 ms |
285628 KB |
Output is correct |
10 |
Correct |
726 ms |
153540 KB |
Output is correct |
11 |
Correct |
52 ms |
50904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
66088 KB |
Output is correct |
2 |
Correct |
1529 ms |
245780 KB |
Output is correct |
3 |
Correct |
1865 ms |
312748 KB |
Output is correct |
4 |
Correct |
2103 ms |
400880 KB |
Output is correct |
5 |
Correct |
1720 ms |
362276 KB |
Output is correct |
6 |
Correct |
2324 ms |
357428 KB |
Output is correct |
7 |
Correct |
795 ms |
233108 KB |
Output is correct |
8 |
Correct |
1597 ms |
405104 KB |
Output is correct |
9 |
Correct |
2044 ms |
285628 KB |
Output is correct |
10 |
Correct |
726 ms |
153540 KB |
Output is correct |
11 |
Correct |
52 ms |
50904 KB |
Output is correct |
12 |
Correct |
1841 ms |
310908 KB |
Output is correct |
13 |
Correct |
2050 ms |
398064 KB |
Output is correct |
14 |
Correct |
1759 ms |
359612 KB |
Output is correct |
15 |
Correct |
1171 ms |
236464 KB |
Output is correct |
16 |
Correct |
1154 ms |
212064 KB |
Output is correct |
17 |
Correct |
1140 ms |
217332 KB |
Output is correct |
18 |
Correct |
1171 ms |
236588 KB |
Output is correct |
19 |
Correct |
1108 ms |
207904 KB |
Output is correct |
20 |
Correct |
925 ms |
226676 KB |
Output is correct |
21 |
Correct |
72 ms |
53708 KB |
Output is correct |
22 |
Correct |
1104 ms |
276192 KB |
Output is correct |
23 |
Correct |
1176 ms |
291516 KB |
Output is correct |
24 |
Correct |
1116 ms |
291784 KB |
Output is correct |
25 |
Correct |
1270 ms |
321280 KB |
Output is correct |
26 |
Correct |
978 ms |
277000 KB |
Output is correct |
27 |
Correct |
2058 ms |
358840 KB |
Output is correct |
28 |
Correct |
1749 ms |
400204 KB |
Output is correct |
29 |
Correct |
2295 ms |
346168 KB |
Output is correct |
30 |
Correct |
838 ms |
233084 KB |
Output is correct |
31 |
Correct |
2013 ms |
284404 KB |
Output is correct |
32 |
Correct |
687 ms |
149364 KB |
Output is correct |
33 |
Correct |
655 ms |
143552 KB |
Output is correct |
34 |
Correct |
886 ms |
175500 KB |
Output is correct |
35 |
Correct |
818 ms |
192900 KB |
Output is correct |
36 |
Correct |
904 ms |
195080 KB |
Output is correct |
37 |
Correct |
656 ms |
112004 KB |
Output is correct |
38 |
Correct |
583 ms |
115508 KB |
Output is correct |
39 |
Correct |
1157 ms |
185864 KB |
Output is correct |
40 |
Correct |
711 ms |
137556 KB |
Output is correct |
41 |
Correct |
725 ms |
135020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
58196 KB |
Output is correct |
2 |
Correct |
42 ms |
49584 KB |
Output is correct |
3 |
Correct |
42 ms |
58180 KB |
Output is correct |
4 |
Correct |
36 ms |
49612 KB |
Output is correct |
5 |
Correct |
39 ms |
58244 KB |
Output is correct |
6 |
Correct |
38 ms |
58188 KB |
Output is correct |
7 |
Correct |
41 ms |
58188 KB |
Output is correct |
8 |
Correct |
39 ms |
58156 KB |
Output is correct |
9 |
Correct |
38 ms |
58188 KB |
Output is correct |
10 |
Correct |
39 ms |
58272 KB |
Output is correct |
11 |
Correct |
38 ms |
58108 KB |
Output is correct |
12 |
Correct |
40 ms |
58148 KB |
Output is correct |
13 |
Correct |
39 ms |
58100 KB |
Output is correct |
14 |
Correct |
39 ms |
58144 KB |
Output is correct |
15 |
Correct |
34 ms |
49536 KB |
Output is correct |
16 |
Correct |
46 ms |
49660 KB |
Output is correct |
17 |
Correct |
43 ms |
49664 KB |
Output is correct |
18 |
Correct |
41 ms |
58132 KB |
Output is correct |
19 |
Correct |
46 ms |
49536 KB |
Output is correct |
20 |
Correct |
413 ms |
94932 KB |
Output is correct |
21 |
Correct |
560 ms |
103860 KB |
Output is correct |
22 |
Correct |
335 ms |
98524 KB |
Output is correct |
23 |
Correct |
313 ms |
94668 KB |
Output is correct |
24 |
Correct |
344 ms |
98528 KB |
Output is correct |
25 |
Correct |
540 ms |
104364 KB |
Output is correct |
26 |
Correct |
423 ms |
97100 KB |
Output is correct |
27 |
Correct |
696 ms |
118148 KB |
Output is correct |
28 |
Correct |
314 ms |
81404 KB |
Output is correct |
29 |
Correct |
1618 ms |
401824 KB |
Output is correct |
30 |
Correct |
1619 ms |
405388 KB |
Output is correct |
31 |
Correct |
210 ms |
69956 KB |
Output is correct |
32 |
Correct |
279 ms |
79380 KB |
Output is correct |
33 |
Correct |
229 ms |
79320 KB |
Output is correct |
34 |
Correct |
221 ms |
78476 KB |
Output is correct |
35 |
Correct |
879 ms |
152100 KB |
Output is correct |
36 |
Correct |
51 ms |
59172 KB |
Output is correct |
37 |
Correct |
107 ms |
68352 KB |
Output is correct |
38 |
Correct |
551 ms |
112108 KB |
Output is correct |
39 |
Correct |
543 ms |
115484 KB |
Output is correct |
40 |
Correct |
994 ms |
185896 KB |
Output is correct |
41 |
Correct |
587 ms |
137620 KB |
Output is correct |
42 |
Correct |
656 ms |
135052 KB |
Output is correct |
43 |
Correct |
313 ms |
66088 KB |
Output is correct |
44 |
Correct |
1529 ms |
245780 KB |
Output is correct |
45 |
Correct |
1865 ms |
312748 KB |
Output is correct |
46 |
Correct |
2103 ms |
400880 KB |
Output is correct |
47 |
Correct |
1720 ms |
362276 KB |
Output is correct |
48 |
Correct |
2324 ms |
357428 KB |
Output is correct |
49 |
Correct |
795 ms |
233108 KB |
Output is correct |
50 |
Correct |
1597 ms |
405104 KB |
Output is correct |
51 |
Correct |
2044 ms |
285628 KB |
Output is correct |
52 |
Correct |
726 ms |
153540 KB |
Output is correct |
53 |
Correct |
52 ms |
50904 KB |
Output is correct |
54 |
Correct |
1841 ms |
310908 KB |
Output is correct |
55 |
Correct |
2050 ms |
398064 KB |
Output is correct |
56 |
Correct |
1759 ms |
359612 KB |
Output is correct |
57 |
Correct |
1171 ms |
236464 KB |
Output is correct |
58 |
Correct |
1154 ms |
212064 KB |
Output is correct |
59 |
Correct |
1140 ms |
217332 KB |
Output is correct |
60 |
Correct |
1171 ms |
236588 KB |
Output is correct |
61 |
Correct |
1108 ms |
207904 KB |
Output is correct |
62 |
Correct |
925 ms |
226676 KB |
Output is correct |
63 |
Correct |
72 ms |
53708 KB |
Output is correct |
64 |
Correct |
1104 ms |
276192 KB |
Output is correct |
65 |
Correct |
1176 ms |
291516 KB |
Output is correct |
66 |
Correct |
1116 ms |
291784 KB |
Output is correct |
67 |
Correct |
1270 ms |
321280 KB |
Output is correct |
68 |
Correct |
978 ms |
277000 KB |
Output is correct |
69 |
Correct |
2058 ms |
358840 KB |
Output is correct |
70 |
Correct |
1749 ms |
400204 KB |
Output is correct |
71 |
Correct |
2295 ms |
346168 KB |
Output is correct |
72 |
Correct |
838 ms |
233084 KB |
Output is correct |
73 |
Correct |
2013 ms |
284404 KB |
Output is correct |
74 |
Correct |
687 ms |
149364 KB |
Output is correct |
75 |
Correct |
655 ms |
143552 KB |
Output is correct |
76 |
Correct |
886 ms |
175500 KB |
Output is correct |
77 |
Correct |
818 ms |
192900 KB |
Output is correct |
78 |
Correct |
904 ms |
195080 KB |
Output is correct |
79 |
Correct |
656 ms |
112004 KB |
Output is correct |
80 |
Correct |
583 ms |
115508 KB |
Output is correct |
81 |
Correct |
1157 ms |
185864 KB |
Output is correct |
82 |
Correct |
711 ms |
137556 KB |
Output is correct |
83 |
Correct |
725 ms |
135020 KB |
Output is correct |
84 |
Correct |
99 ms |
64488 KB |
Output is correct |
85 |
Runtime error |
344 ms |
211752 KB |
Execution killed with signal 11 |
86 |
Halted |
0 ms |
0 KB |
- |