#include "walk.h"
#include <bits/stdc++.h>
#ifndef EVAL
#include "grader.cpp"
#endif
using std::cin;
using std::cout;
using std::vector;
using std::string;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define ln "\n"
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple
#define all(a) a.begin(),a.end()
constexpr ll inf = (1ll<<60);
template<typename T>
struct li_chao_tree{
ll N;
vector<std::pair<T,T>> lines;
vector<T> x;
std::map<ll,ll> rev;
li_chao_tree(vector<T> X, T xINF){
N = 1;
while(N <= x.size()) N *= 2;
lines.resize(2*N, mp(0,inf));
x.resize(N);
rep(i,0,N){
if(i < X.size()){
x[i] = X[i];
rev[x[i]] = i;
}
else x[i] = xINF;
}
}
ll ID(T X){
return rev[X];
}
T value(std::pair<T,T> line, T X){
return line.first*X+line.second;
}
void add_segment(ll a, ll b, std::pair<T,T> line, ll now = 1, ll l = 0, ll r = -1){
if(r == -1) r = N;
if(b <= l || r <= a) return;
if(a <= l && r <= b){
if(N <= now){
if(value(lines[now], x[l]) > value(line, x[l])) lines[now] = line;
return;
}
ll m = (l+r)/2;
bool left = (value(lines[now],x[l]) < value(line, x[l]));
bool mid = (value(lines[now],x[m]) < value(line, x[m]));
bool right = (value(lines[now],x[r]) < value(line, x[r]));
if(left && mid && right){
std::swap(line, lines[now]);
return;
}
if(!left && !mid && !right){
return;
}
if(left && mid){
std::swap(line, lines[now]);
add_segment(a,b,line,now*2+1,m,r);
return;
}
if(mid && right){
std::swap(line, lines[now]);
add_segment(a,b,line,now*2,l,m);
return;
}
if(left){
add_segment(a,b,line,now*2,l,m);
return;
}
if(right){
add_segment(a,b,line,now*2+1,m,r);
return;
}
}
else{
add_segment(a,b,line,now*2,l,(l+r)/2);
add_segment(a,b,line,now*2+1,(l+r)/2,r);
}
}
T calc(ll idx, T X){
T ret = inf;
idx += N;
while(idx){
ret = std::min(idx, value(lines[idx], X));
idx /= 2;
}
return ret;
}
};
template<typename T>
struct segment_tree{
ll N;
vector<T> node;
T valINF;
segment_tree(int n, T valINF_): valINF(valINF_){
N = 1;
while(N <= n) N *= 2;
node.resize(2*N, valINF);
}
void set(ll idx, T val){
idx += N;
node[idx] = val;
idx /= 2;
while(idx){
node[idx] = compare(node[idx*2], node[idx*2+1]);
idx /= 2;
}
}
T calc(ll a, ll b, ll now = 1, ll l = 0, ll r = -1){
if(r == -1) r = N;
if(b <= l || r <= a) return valINF;
if(a <= l && r <= b) return node[now];
return compare(calc(a,b,now*2,l,(l+r)/2), calc(a,b,now*2+1,(l+r)/2,r));
}
T compare(T l, T r){
return std::min(l, r);
}
};
void dec(std::map<ll,ll> &map, ll el){
map[el]--;
if(map[el] == 0) map.erase(el);
}
ll solve_subtask_1_2(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g){
ll N = x.size();
segment_tree<pii> seg(N,mp(inf,inf));
vii edge(N), weight(N);
vector<std::map<ll,ll>> rev(N);
rep(i,0,N) rev[i][0] = i;
ll K = N;
rep(i,0,N) seg.set(i, mp(-h[i], i));
rep(i,0,l.size()){
vi data;
while(true){
auto el = seg.calc(l[i], r[i]+1);
if(-el.first < y[i]) break;
data.pb(el.second);
seg.set(el.second, mp(inf, inf));
}
sort(all(data));
for(auto el: data){
if(!rev[el].count(y[i])){
rev[el][y[i]] = K++;
weight.pb(vi());
edge.pb(vi());
}
}
rep(j,1,data.size()){
ll now = rev[data[j-1]][y[i]];
ll next = rev[data[j]][y[i]];
edge[now].pb(next);
edge[next].pb(now);
ll cost = x[data[j]]-x[data[j-1]];
weight[now].pb(cost);
weight[next].pb(cost);
}
for(auto el: data){
seg.set(el, mp(-h[el], el));
}
}
rep(i,0,N){
vi data, height;
for(auto el: rev[i]){
data.pb(el.second);
height.pb(el.first);
}
rep(j,1,data.size()){
ll now = data[j-1];
ll next = data[j];
edge[now].pb(next);
edge[next].pb(now);
ll cost = height[j]-height[j-1];
weight[now].pb(cost);
weight[next].pb(cost);
}
}
std::priority_queue<pii> que;
vi dist(K, inf);
dist[rev[s][0]] = 0;
que.push(mp(0,rev[s][0]));
while(!que.empty()){
auto el = que.top(); que.pop();
ll now = el.second;
if(dist[now] < -el.first) continue;
rep(i,0,edge[now].size()){
ll next = edge[now][i];
ll sum = dist[now]+weight[now][i];
if(sum < dist[next]){
dist[next] = sum;
que.push(mp(-sum, next));
}
}
}
ll ans = dist[rev[g][0]];
if(ans == inf) ans = -1;
return ans;
}
ll solve_subtask_3_4(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g){
ll N = x.size();
vi y_mem;
std::map<ll,ll> mem;
ll M = 0;
{
mem[0] = 0;
for(auto el: h) mem[el] = 0;
for(auto el: y) mem[el] = 0;
for(auto &el: mem){
y_mem.pb(el.first);
el.second = M++;
}
}
segment_tree<ll> left(M, inf), right(M, inf);
vector<std::map<ll,ll>> value(M);
rep(i,0,M) value[i][inf] = 1;
vii erase_idx(N), erase_val(N);
vii add(N);
rep(i,0,l.size()){
add[l[i]].pb(i);
}
rep(t,0,N){
for(auto i: add[t]){
ll val = left.calc(0, mem[y[i]]+1)+y[i];
val = std::min(val, right.calc(mem[y[i]], M)-y[i]);
if(t == 0) val = y[i];
value[mem[y[i]]][val]++;
left.set(mem[y[i]], (*value[mem[y[i]]].begin()).first-y[i]);
right.set(mem[y[i]], (*value[mem[y[i]]].begin()).first+y[i]);
erase_idx[r[i]].pb(y[i]);
erase_val[r[i]].pb(val);
}
if(t == N-1) break;
rep(i,0,erase_idx[t].size()){
ll Y = erase_idx[t][i];
ll V = erase_val[t][i];
dec(value[mem[Y]], V);
left.set(mem[Y], (*value[mem[Y]].begin()).first-Y);
right.set(mem[Y], (*value[mem[Y]].begin()).first+Y);
}
}
ll ans = right.calc(0,M)+x[N-1]-x[0];
if(ans >= inf/2) ans = -1;
return ans;
}
long long 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) {
ll N = x.size();
if(s == 0 && g == N-1) return solve_subtask_3_4(x,h,l,r,y,s,g);
return solve_subtask_1_2(x,h,l,r,y,s,g);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
428 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1451 ms |
161092 KB |
Output is correct |
4 |
Correct |
1420 ms |
188884 KB |
Output is correct |
5 |
Correct |
1016 ms |
165120 KB |
Output is correct |
6 |
Correct |
904 ms |
148548 KB |
Output is correct |
7 |
Correct |
1008 ms |
165324 KB |
Output is correct |
8 |
Correct |
1897 ms |
205904 KB |
Output is correct |
9 |
Correct |
1169 ms |
162032 KB |
Output is correct |
10 |
Correct |
1978 ms |
253080 KB |
Output is correct |
11 |
Correct |
759 ms |
95504 KB |
Output is correct |
12 |
Correct |
498 ms |
70788 KB |
Output is correct |
13 |
Correct |
494 ms |
70748 KB |
Output is correct |
14 |
Correct |
450 ms |
73440 KB |
Output is correct |
15 |
Correct |
486 ms |
75332 KB |
Output is correct |
16 |
Correct |
518 ms |
77260 KB |
Output is correct |
17 |
Correct |
485 ms |
74532 KB |
Output is correct |
18 |
Correct |
265 ms |
46404 KB |
Output is correct |
19 |
Correct |
11 ms |
3960 KB |
Output is correct |
20 |
Correct |
126 ms |
36688 KB |
Output is correct |
21 |
Correct |
92 ms |
20648 KB |
Output is correct |
22 |
Correct |
72 ms |
25712 KB |
Output is correct |
23 |
Correct |
204 ms |
37312 KB |
Output is correct |
24 |
Correct |
149 ms |
25296 KB |
Output is correct |
25 |
Correct |
79 ms |
22220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
5204 KB |
Output is correct |
2 |
Correct |
343 ms |
29696 KB |
Output is correct |
3 |
Correct |
377 ms |
31100 KB |
Output is correct |
4 |
Correct |
439 ms |
41976 KB |
Output is correct |
5 |
Correct |
451 ms |
46596 KB |
Output is correct |
6 |
Correct |
447 ms |
44288 KB |
Output is correct |
7 |
Correct |
176 ms |
27172 KB |
Output is correct |
8 |
Correct |
411 ms |
44924 KB |
Output is correct |
9 |
Correct |
383 ms |
46528 KB |
Output is correct |
10 |
Correct |
197 ms |
31940 KB |
Output is correct |
11 |
Correct |
15 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
5204 KB |
Output is correct |
2 |
Correct |
343 ms |
29696 KB |
Output is correct |
3 |
Correct |
377 ms |
31100 KB |
Output is correct |
4 |
Correct |
439 ms |
41976 KB |
Output is correct |
5 |
Correct |
451 ms |
46596 KB |
Output is correct |
6 |
Correct |
447 ms |
44288 KB |
Output is correct |
7 |
Correct |
176 ms |
27172 KB |
Output is correct |
8 |
Correct |
411 ms |
44924 KB |
Output is correct |
9 |
Correct |
383 ms |
46528 KB |
Output is correct |
10 |
Correct |
197 ms |
31940 KB |
Output is correct |
11 |
Correct |
15 ms |
5068 KB |
Output is correct |
12 |
Correct |
464 ms |
32828 KB |
Output is correct |
13 |
Correct |
610 ms |
64296 KB |
Output is correct |
14 |
Correct |
635 ms |
68732 KB |
Output is correct |
15 |
Correct |
221 ms |
27268 KB |
Output is correct |
16 |
Correct |
322 ms |
51528 KB |
Output is correct |
17 |
Correct |
315 ms |
51492 KB |
Output is correct |
18 |
Correct |
195 ms |
27264 KB |
Output is correct |
19 |
Correct |
298 ms |
51436 KB |
Output is correct |
20 |
Correct |
282 ms |
51124 KB |
Output is correct |
21 |
Correct |
72 ms |
32192 KB |
Output is correct |
22 |
Correct |
340 ms |
52540 KB |
Output is correct |
23 |
Correct |
319 ms |
51956 KB |
Output is correct |
24 |
Correct |
328 ms |
51256 KB |
Output is correct |
25 |
Correct |
373 ms |
58052 KB |
Output is correct |
26 |
Correct |
347 ms |
54204 KB |
Output is correct |
27 |
Correct |
614 ms |
68164 KB |
Output is correct |
28 |
Correct |
534 ms |
63848 KB |
Output is correct |
29 |
Correct |
576 ms |
66368 KB |
Output is correct |
30 |
Correct |
271 ms |
51100 KB |
Output is correct |
31 |
Correct |
489 ms |
68328 KB |
Output is correct |
32 |
Correct |
147 ms |
24136 KB |
Output is correct |
33 |
Correct |
154 ms |
26464 KB |
Output is correct |
34 |
Correct |
212 ms |
31360 KB |
Output is correct |
35 |
Correct |
223 ms |
31416 KB |
Output is correct |
36 |
Correct |
173 ms |
29100 KB |
Output is correct |
37 |
Correct |
75 ms |
18200 KB |
Output is correct |
38 |
Correct |
88 ms |
22844 KB |
Output is correct |
39 |
Correct |
204 ms |
34632 KB |
Output is correct |
40 |
Correct |
141 ms |
22416 KB |
Output is correct |
41 |
Correct |
78 ms |
19796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
428 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1451 ms |
161092 KB |
Output is correct |
21 |
Correct |
1420 ms |
188884 KB |
Output is correct |
22 |
Correct |
1016 ms |
165120 KB |
Output is correct |
23 |
Correct |
904 ms |
148548 KB |
Output is correct |
24 |
Correct |
1008 ms |
165324 KB |
Output is correct |
25 |
Correct |
1897 ms |
205904 KB |
Output is correct |
26 |
Correct |
1169 ms |
162032 KB |
Output is correct |
27 |
Correct |
1978 ms |
253080 KB |
Output is correct |
28 |
Correct |
759 ms |
95504 KB |
Output is correct |
29 |
Correct |
498 ms |
70788 KB |
Output is correct |
30 |
Correct |
494 ms |
70748 KB |
Output is correct |
31 |
Correct |
450 ms |
73440 KB |
Output is correct |
32 |
Correct |
486 ms |
75332 KB |
Output is correct |
33 |
Correct |
518 ms |
77260 KB |
Output is correct |
34 |
Correct |
485 ms |
74532 KB |
Output is correct |
35 |
Correct |
265 ms |
46404 KB |
Output is correct |
36 |
Correct |
11 ms |
3960 KB |
Output is correct |
37 |
Correct |
126 ms |
36688 KB |
Output is correct |
38 |
Correct |
92 ms |
20648 KB |
Output is correct |
39 |
Correct |
72 ms |
25712 KB |
Output is correct |
40 |
Correct |
204 ms |
37312 KB |
Output is correct |
41 |
Correct |
149 ms |
25296 KB |
Output is correct |
42 |
Correct |
79 ms |
22220 KB |
Output is correct |
43 |
Correct |
30 ms |
5204 KB |
Output is correct |
44 |
Correct |
343 ms |
29696 KB |
Output is correct |
45 |
Correct |
377 ms |
31100 KB |
Output is correct |
46 |
Correct |
439 ms |
41976 KB |
Output is correct |
47 |
Correct |
451 ms |
46596 KB |
Output is correct |
48 |
Correct |
447 ms |
44288 KB |
Output is correct |
49 |
Correct |
176 ms |
27172 KB |
Output is correct |
50 |
Correct |
411 ms |
44924 KB |
Output is correct |
51 |
Correct |
383 ms |
46528 KB |
Output is correct |
52 |
Correct |
197 ms |
31940 KB |
Output is correct |
53 |
Correct |
15 ms |
5068 KB |
Output is correct |
54 |
Correct |
464 ms |
32828 KB |
Output is correct |
55 |
Correct |
610 ms |
64296 KB |
Output is correct |
56 |
Correct |
635 ms |
68732 KB |
Output is correct |
57 |
Correct |
221 ms |
27268 KB |
Output is correct |
58 |
Correct |
322 ms |
51528 KB |
Output is correct |
59 |
Correct |
315 ms |
51492 KB |
Output is correct |
60 |
Correct |
195 ms |
27264 KB |
Output is correct |
61 |
Correct |
298 ms |
51436 KB |
Output is correct |
62 |
Correct |
282 ms |
51124 KB |
Output is correct |
63 |
Correct |
72 ms |
32192 KB |
Output is correct |
64 |
Correct |
340 ms |
52540 KB |
Output is correct |
65 |
Correct |
319 ms |
51956 KB |
Output is correct |
66 |
Correct |
328 ms |
51256 KB |
Output is correct |
67 |
Correct |
373 ms |
58052 KB |
Output is correct |
68 |
Correct |
347 ms |
54204 KB |
Output is correct |
69 |
Correct |
614 ms |
68164 KB |
Output is correct |
70 |
Correct |
534 ms |
63848 KB |
Output is correct |
71 |
Correct |
576 ms |
66368 KB |
Output is correct |
72 |
Correct |
271 ms |
51100 KB |
Output is correct |
73 |
Correct |
489 ms |
68328 KB |
Output is correct |
74 |
Correct |
147 ms |
24136 KB |
Output is correct |
75 |
Correct |
154 ms |
26464 KB |
Output is correct |
76 |
Correct |
212 ms |
31360 KB |
Output is correct |
77 |
Correct |
223 ms |
31416 KB |
Output is correct |
78 |
Correct |
173 ms |
29100 KB |
Output is correct |
79 |
Correct |
75 ms |
18200 KB |
Output is correct |
80 |
Correct |
88 ms |
22844 KB |
Output is correct |
81 |
Correct |
204 ms |
34632 KB |
Output is correct |
82 |
Correct |
141 ms |
22416 KB |
Output is correct |
83 |
Correct |
78 ms |
19796 KB |
Output is correct |
84 |
Correct |
106 ms |
17556 KB |
Output is correct |
85 |
Execution timed out |
4091 ms |
1008752 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |