#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0; i<n; ++i)
#define pb push_back
using ll = long long;
const ll inf = 1e18;
#define pi pair<ll,ll>
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define int ll
const int sz=1<<30;
struct sgt {
sgt *L, *R; int s;
sgt() {
L=R=NULL; s=inf;
}
void set(int l, int r, int i, int x) {
if (r-l==1) {
s=x; return;
}
int m=(l+r)>>1;
if (i<m) {
if (!L) L=new sgt();
L->set(l,m,i,x);
} else {
if (!R) R=new sgt();
R->set(m,r,i,x);
}
s = min(L?L->s:inf, R?R->s:inf);
}
void set(int i, int x) {
set(0,sz,i,x);
}
int query(int l, int r, int lx, int rx) {
if (rx<=l || r<=lx) return inf;
if (lx<=l && r<=rx) return s;
int m=(l+r)>>1;
int lq=L?L->query(l,m,lx,rx):inf;
int rq=R?R->query(m,r,lx,rx):inf;
return min(lq,rq);
}
int query(int l, int r) {
return query(0,sz,l,r);
}
};
const int N=1e5+55;
vector<pi> adj[10*N];
ll d[10*N];
map<int,int> c[N]; int _z=0; const int Z=1e9+5;
int ind(int x, int y) {
if (c[x].count(y)) return c[x][y];
return c[x][y]=_z++;
}
#undef int
ll p12(vector<int>x, vector<int>h, vector<int>l, vector<int>r, vector<int>y, int s, int g) {
int n=x.size(), m=l.size();
vector<vector<int>> g1(n);
vector<vector<int>> g2(m);
g1[s].pb(0);
g1[g].pb(0);
vector<pi> v;
forn(i,n) v.pb({h[i],i});
forn(i,m) v.pb({y[i],-i-1});
sort(rall(v));
set<int> st;
for(auto&z:v) {
int y=z.f, i=z.s;
if (i>=0) {
st.insert(i); continue;
}
i*=-1; --i;
auto it=st.upper_bound(l[i]);
vector<int> v;
while (1) {
int j=*it;
v.pb(j);
if (j==r[i]) break;
++it;
}
if (v.size() > 10 && max(n,m)>50) return -2;
g1[l[i]].pb(y);
int last=l[i];
for(auto&j:v) {
adj[ind(last,y)].pb({ind(j,y),x[j]-x[last]});
adj[ind(j,y)].pb({ind(last,y),x[j]-x[last]});
g1[j].pb(y);
last=j;
}
}
forn(i,n) {
sort(all(g1[i]));
int sz=g1[i].size();
forn(j,sz-1) {
adj[ind(i,g1[i][j])].pb({ind(i,g1[i][j+1]),g1[i][j+1]-g1[i][j]});
adj[ind(i,g1[i][j+1])].pb({ind(i,g1[i][j]),g1[i][j+1]-g1[i][j]});
}
}
forn(i,_z) d[i]=inf;
priority_queue<pair<ll,ll>> q; q.push({0,ind(s,0)});
while(q.size()) {
auto it=q.top(); q.pop();
ll di=-it.f, u=it.s;
if (d[u]<=di) continue;
d[u]=di;
for(auto&e:adj[u]) {
int v=e.f, w=e.s;
if (d[v]!=inf) continue;
q.push({-di-w,v});
}
}
if (d[ind(g,0)]==inf) return -1;
return d[ind(g,0)];
}
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(), m=l.size();
if (s!=0 || g!=n-1) return p12(x,h,l,r,y,s,g);
#define int ll
sgt bottom, top;
bottom.set(0,0);
top.set(0,0);
vector<vector<int>> add(n), del(n);
del[0].pb(0);
add[n-1].pb(0);
forn(i,m) {
add[l[i]].pb(y[i]);
del[r[i]].pb(y[i]);
}
forn(i,n) {
vector<pi> toset;
for(auto&y:add[i]) {
int f = bottom.query(0,y+1);
int s = top.query(y,sz);
if (min(f,s)==inf) return -1;
f+=y; s-=y;
int x=min(f,s);
toset.pb({y,x});
}
for(auto&y:del[i]) {
bottom.set(y,inf);
top.set(y,inf);
}
for(auto&x:toset) {
bottom.set(x.f,x.s-x.f);
top.set(x.f,x.s+x.f);
}
}
int f = top.s+x[n-1]-x[0];
return f;
#undef int
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28468 KB |
Output is correct |
2 |
Correct |
15 ms |
28488 KB |
Output is correct |
3 |
Correct |
15 ms |
28500 KB |
Output is correct |
4 |
Correct |
15 ms |
28500 KB |
Output is correct |
5 |
Correct |
15 ms |
28628 KB |
Output is correct |
6 |
Correct |
15 ms |
28532 KB |
Output is correct |
7 |
Correct |
16 ms |
28500 KB |
Output is correct |
8 |
Correct |
15 ms |
28504 KB |
Output is correct |
9 |
Correct |
15 ms |
28480 KB |
Output is correct |
10 |
Correct |
15 ms |
28628 KB |
Output is correct |
11 |
Correct |
15 ms |
28500 KB |
Output is correct |
12 |
Correct |
15 ms |
28480 KB |
Output is correct |
13 |
Correct |
15 ms |
28500 KB |
Output is correct |
14 |
Correct |
14 ms |
28452 KB |
Output is correct |
15 |
Correct |
15 ms |
28516 KB |
Output is correct |
16 |
Correct |
15 ms |
28500 KB |
Output is correct |
17 |
Correct |
14 ms |
28480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28388 KB |
Output is correct |
2 |
Correct |
15 ms |
28480 KB |
Output is correct |
3 |
Correct |
1039 ms |
168064 KB |
Output is correct |
4 |
Correct |
864 ms |
176724 KB |
Output is correct |
5 |
Correct |
542 ms |
156868 KB |
Output is correct |
6 |
Correct |
475 ms |
141772 KB |
Output is correct |
7 |
Correct |
537 ms |
156972 KB |
Output is correct |
8 |
Correct |
1450 ms |
206736 KB |
Output is correct |
9 |
Correct |
663 ms |
155408 KB |
Output is correct |
10 |
Correct |
1267 ms |
233912 KB |
Output is correct |
11 |
Correct |
464 ms |
103680 KB |
Output is correct |
12 |
Correct |
318 ms |
130228 KB |
Output is correct |
13 |
Correct |
325 ms |
130048 KB |
Output is correct |
14 |
Correct |
224 ms |
78296 KB |
Output is correct |
15 |
Correct |
252 ms |
79556 KB |
Output is correct |
16 |
Correct |
246 ms |
80056 KB |
Output is correct |
17 |
Correct |
253 ms |
78664 KB |
Output is correct |
18 |
Correct |
169 ms |
55272 KB |
Output is correct |
19 |
Correct |
20 ms |
31008 KB |
Output is correct |
20 |
Correct |
89 ms |
53804 KB |
Output is correct |
21 |
Correct |
125 ms |
42916 KB |
Output is correct |
22 |
Correct |
133 ms |
46524 KB |
Output is correct |
23 |
Correct |
163 ms |
54384 KB |
Output is correct |
24 |
Correct |
135 ms |
46284 KB |
Output is correct |
25 |
Correct |
129 ms |
43956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
32656 KB |
Output is correct |
2 |
Correct |
355 ms |
124392 KB |
Output is correct |
3 |
Correct |
344 ms |
111024 KB |
Output is correct |
4 |
Correct |
395 ms |
131608 KB |
Output is correct |
5 |
Correct |
427 ms |
132556 KB |
Output is correct |
6 |
Correct |
413 ms |
134632 KB |
Output is correct |
7 |
Correct |
180 ms |
87072 KB |
Output is correct |
8 |
Correct |
312 ms |
126220 KB |
Output is correct |
9 |
Correct |
415 ms |
130644 KB |
Output is correct |
10 |
Correct |
152 ms |
51120 KB |
Output is correct |
11 |
Correct |
23 ms |
32348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
32656 KB |
Output is correct |
2 |
Correct |
355 ms |
124392 KB |
Output is correct |
3 |
Correct |
344 ms |
111024 KB |
Output is correct |
4 |
Correct |
395 ms |
131608 KB |
Output is correct |
5 |
Correct |
427 ms |
132556 KB |
Output is correct |
6 |
Correct |
413 ms |
134632 KB |
Output is correct |
7 |
Correct |
180 ms |
87072 KB |
Output is correct |
8 |
Correct |
312 ms |
126220 KB |
Output is correct |
9 |
Correct |
415 ms |
130644 KB |
Output is correct |
10 |
Correct |
152 ms |
51120 KB |
Output is correct |
11 |
Correct |
23 ms |
32348 KB |
Output is correct |
12 |
Correct |
350 ms |
118816 KB |
Output is correct |
13 |
Correct |
83 ms |
45216 KB |
Output is correct |
14 |
Correct |
406 ms |
127016 KB |
Output is correct |
15 |
Correct |
230 ms |
77772 KB |
Output is correct |
16 |
Correct |
243 ms |
77300 KB |
Output is correct |
17 |
Correct |
255 ms |
77156 KB |
Output is correct |
18 |
Correct |
229 ms |
77800 KB |
Output is correct |
19 |
Correct |
239 ms |
77364 KB |
Output is correct |
20 |
Correct |
182 ms |
86108 KB |
Output is correct |
21 |
Correct |
35 ms |
37964 KB |
Output is correct |
22 |
Correct |
246 ms |
106764 KB |
Output is correct |
23 |
Correct |
250 ms |
108220 KB |
Output is correct |
24 |
Correct |
257 ms |
113992 KB |
Output is correct |
25 |
Correct |
292 ms |
117976 KB |
Output is correct |
26 |
Correct |
270 ms |
126120 KB |
Output is correct |
27 |
Correct |
408 ms |
127824 KB |
Output is correct |
28 |
Correct |
72 ms |
45276 KB |
Output is correct |
29 |
Correct |
401 ms |
125964 KB |
Output is correct |
30 |
Correct |
176 ms |
87128 KB |
Output is correct |
31 |
Correct |
409 ms |
130700 KB |
Output is correct |
32 |
Correct |
138 ms |
46984 KB |
Output is correct |
33 |
Correct |
138 ms |
48716 KB |
Output is correct |
34 |
Correct |
153 ms |
51432 KB |
Output is correct |
35 |
Correct |
182 ms |
64396 KB |
Output is correct |
36 |
Correct |
186 ms |
61004 KB |
Output is correct |
37 |
Correct |
125 ms |
43024 KB |
Output is correct |
38 |
Correct |
130 ms |
46412 KB |
Output is correct |
39 |
Correct |
165 ms |
54496 KB |
Output is correct |
40 |
Correct |
143 ms |
46212 KB |
Output is correct |
41 |
Correct |
130 ms |
43980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28468 KB |
Output is correct |
2 |
Correct |
15 ms |
28488 KB |
Output is correct |
3 |
Correct |
15 ms |
28500 KB |
Output is correct |
4 |
Correct |
15 ms |
28500 KB |
Output is correct |
5 |
Correct |
15 ms |
28628 KB |
Output is correct |
6 |
Correct |
15 ms |
28532 KB |
Output is correct |
7 |
Correct |
16 ms |
28500 KB |
Output is correct |
8 |
Correct |
15 ms |
28504 KB |
Output is correct |
9 |
Correct |
15 ms |
28480 KB |
Output is correct |
10 |
Correct |
15 ms |
28628 KB |
Output is correct |
11 |
Correct |
15 ms |
28500 KB |
Output is correct |
12 |
Correct |
15 ms |
28480 KB |
Output is correct |
13 |
Correct |
15 ms |
28500 KB |
Output is correct |
14 |
Correct |
14 ms |
28452 KB |
Output is correct |
15 |
Correct |
15 ms |
28516 KB |
Output is correct |
16 |
Correct |
15 ms |
28500 KB |
Output is correct |
17 |
Correct |
14 ms |
28480 KB |
Output is correct |
18 |
Correct |
14 ms |
28388 KB |
Output is correct |
19 |
Correct |
15 ms |
28480 KB |
Output is correct |
20 |
Correct |
1039 ms |
168064 KB |
Output is correct |
21 |
Correct |
864 ms |
176724 KB |
Output is correct |
22 |
Correct |
542 ms |
156868 KB |
Output is correct |
23 |
Correct |
475 ms |
141772 KB |
Output is correct |
24 |
Correct |
537 ms |
156972 KB |
Output is correct |
25 |
Correct |
1450 ms |
206736 KB |
Output is correct |
26 |
Correct |
663 ms |
155408 KB |
Output is correct |
27 |
Correct |
1267 ms |
233912 KB |
Output is correct |
28 |
Correct |
464 ms |
103680 KB |
Output is correct |
29 |
Correct |
318 ms |
130228 KB |
Output is correct |
30 |
Correct |
325 ms |
130048 KB |
Output is correct |
31 |
Correct |
224 ms |
78296 KB |
Output is correct |
32 |
Correct |
252 ms |
79556 KB |
Output is correct |
33 |
Correct |
246 ms |
80056 KB |
Output is correct |
34 |
Correct |
253 ms |
78664 KB |
Output is correct |
35 |
Correct |
169 ms |
55272 KB |
Output is correct |
36 |
Correct |
20 ms |
31008 KB |
Output is correct |
37 |
Correct |
89 ms |
53804 KB |
Output is correct |
38 |
Correct |
125 ms |
42916 KB |
Output is correct |
39 |
Correct |
133 ms |
46524 KB |
Output is correct |
40 |
Correct |
163 ms |
54384 KB |
Output is correct |
41 |
Correct |
135 ms |
46284 KB |
Output is correct |
42 |
Correct |
129 ms |
43956 KB |
Output is correct |
43 |
Correct |
68 ms |
32656 KB |
Output is correct |
44 |
Correct |
355 ms |
124392 KB |
Output is correct |
45 |
Correct |
344 ms |
111024 KB |
Output is correct |
46 |
Correct |
395 ms |
131608 KB |
Output is correct |
47 |
Correct |
427 ms |
132556 KB |
Output is correct |
48 |
Correct |
413 ms |
134632 KB |
Output is correct |
49 |
Correct |
180 ms |
87072 KB |
Output is correct |
50 |
Correct |
312 ms |
126220 KB |
Output is correct |
51 |
Correct |
415 ms |
130644 KB |
Output is correct |
52 |
Correct |
152 ms |
51120 KB |
Output is correct |
53 |
Correct |
23 ms |
32348 KB |
Output is correct |
54 |
Correct |
350 ms |
118816 KB |
Output is correct |
55 |
Correct |
83 ms |
45216 KB |
Output is correct |
56 |
Correct |
406 ms |
127016 KB |
Output is correct |
57 |
Correct |
230 ms |
77772 KB |
Output is correct |
58 |
Correct |
243 ms |
77300 KB |
Output is correct |
59 |
Correct |
255 ms |
77156 KB |
Output is correct |
60 |
Correct |
229 ms |
77800 KB |
Output is correct |
61 |
Correct |
239 ms |
77364 KB |
Output is correct |
62 |
Correct |
182 ms |
86108 KB |
Output is correct |
63 |
Correct |
35 ms |
37964 KB |
Output is correct |
64 |
Correct |
246 ms |
106764 KB |
Output is correct |
65 |
Correct |
250 ms |
108220 KB |
Output is correct |
66 |
Correct |
257 ms |
113992 KB |
Output is correct |
67 |
Correct |
292 ms |
117976 KB |
Output is correct |
68 |
Correct |
270 ms |
126120 KB |
Output is correct |
69 |
Correct |
408 ms |
127824 KB |
Output is correct |
70 |
Correct |
72 ms |
45276 KB |
Output is correct |
71 |
Correct |
401 ms |
125964 KB |
Output is correct |
72 |
Correct |
176 ms |
87128 KB |
Output is correct |
73 |
Correct |
409 ms |
130700 KB |
Output is correct |
74 |
Correct |
138 ms |
46984 KB |
Output is correct |
75 |
Correct |
138 ms |
48716 KB |
Output is correct |
76 |
Correct |
153 ms |
51432 KB |
Output is correct |
77 |
Correct |
182 ms |
64396 KB |
Output is correct |
78 |
Correct |
186 ms |
61004 KB |
Output is correct |
79 |
Correct |
125 ms |
43024 KB |
Output is correct |
80 |
Correct |
130 ms |
46412 KB |
Output is correct |
81 |
Correct |
165 ms |
54496 KB |
Output is correct |
82 |
Correct |
143 ms |
46212 KB |
Output is correct |
83 |
Correct |
130 ms |
43980 KB |
Output is correct |
84 |
Incorrect |
27 ms |
32972 KB |
Output isn't correct |
85 |
Halted |
0 ms |
0 KB |
- |