#include "walk.h"
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
struct walk{
ll l,r,y;
};
vector <walk> all;
const ll MAXN = 1e5 + 100;
const ll INF = 1e18;
vector <pll> g[MAXN*20];
ll dist[MAXN*20];
ll dijkstra(ll s,ll t){
memset(dist,0x3f,sizeof dist);
dist[s] = 0;
priority_queue <pll,vector <pll> ,greater <> > q;
q.push(MP(dist[s],s));
while (!q.empty()){
auto u = q.top().se;
ll val = q.top().fi;
q.pop();
if (dist[u] != val)continue;
for (auto tmp:g[u]){
ll v = tmp.fi,w = tmp.se;
if (dist[u] + w < dist[v]){
dist[v] = dist[u] + w;
q.push(MP(dist[v],v));
}
}
}
if (dist[t] >=INF)dist[t] = -1;
return dist[t];
}
ll SUSSYBAKA;
struct point{
ll x,y,id;
point(ll x1=-1,ll y1=-1):x(x1),y(y1),id(x1==-1?-1:SUSSYBAKA++){
}
bool operator < (const point &p)const {
return MP(x,y)<MP(p.x,p.y);
}
bool operator == (const point &p)const {
return MP(x,y)==MP(p.x,p.y);
}
};
vector <point> vertices;
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 m = sz(l);
ll n = sz(X);
for (ll i = 0;i < m;i ++){
all.push_back({l[i],r[i],y[i]});
}
sort(all.begin(),all.end(),[](walk a1,walk a2){return a1.y<a2.y;});
vector <ll> order;
order.resize(n);
iota(order.begin(),order.end(),0);
sort(order.begin(),order.end(),[&](ll x,ll y){return h[x] > h[y];});
vertices.emplace_back(X[S],0);
vertices.emplace_back(X[G],0);
for (auto x:all){
vertices.emplace_back(X[x.l],x.y);
vertices.emplace_back(X[x.r],x.y);
}
auto add_vertex = [&](bool inv){
vector <ll> bd;
for (ll i = inv?n-1:0;inv?i>=0:i<n;inv?i--:i++){
// cout<<i<<endl;
if (sz(bd) && h[bd.back()] < h[i])bd.pop_back();
bd.push_back(i);
if (i==S||i==G){
ll ptr = sz(bd)-1;
for (auto x:all){
while (ptr>=0&&h[bd[ptr]] < x.y)ptr--;
if (ptr != -1 && x.l <= bd[ptr] && bd[ptr] <= x.r){
vertices.emplace_back(X[bd[ptr]],x.y);
}
}
}
}
};
add_vertex(0);
add_vertex(1);
// return -1;
vector <pair <pll,bool> > event;
for (auto x:all){
event.emplace_back(MP(X[x.l],x.y),1);
event.emplace_back(MP(X[x.r]+1,x.y),0);
}
sort(event.begin(),event.end());
sort(vertices.begin(),vertices.end());
vertices.resize(unique(vertices.begin(),vertices.end())-vertices.begin());
{
ll ptr = 0;
multiset <ll> ms;
vector <pll> Tm;
for (auto x:vertices){
while (ptr<sz(event) && event[ptr].fi.fi <= x.x){
if (event[ptr].se)ms.insert(event[ptr].fi.se);
else ms.erase(ms.find((event[ptr].fi.se)));
ptr++;
}
auto tmp = ms.upper_bound(x.y);
if (tmp != ms.end() && (*tmp) <= h[lower_bound(X.begin(),X.end(),x.x)-X.begin()])Tm.emplace_back(x.x,*(tmp));
tmp = ms.lower_bound(x.y);
if (tmp != ms.begin() && (*tmp) <= h[lower_bound(X.begin(),X.end(),x.x)-X.begin()])Tm.emplace_back(x.x,*prev(tmp));
}
for (auto x:Tm)vertices.emplace_back(x.fi,x.se);
}
sort(vertices.begin(),vertices.end());
vertices.resize(unique(vertices.begin(),vertices.end())-vertices.begin());
auto add_edge = [&](point a1,point a2){
ll w = abs(a1.x-a2.x) + abs(a1.y-a2.y);
// cout<<a1.x<<' '<<a1.y<<' '<<a2.x<<' '<<a2.y<<'\n';
g[a1.id].push_back(MP(a2.id,w));
g[a2.id].push_back(MP(a1.id,w));
};
// for (auto x:vertices){
// cout<<x.x<<' '<<x.y<<' '<<x.id<<'\n';
// }
for (ll j = 0;j + 1 < sz(vertices);j ++){
if (vertices[j].x == vertices[j+1].x)add_edge(vertices[j],vertices[j+1]);
}
auto cmp_y = [](point a1,point a2){return MP(a1.y,a1.x) < MP(a2.y,a2.x);};
sort(vertices.begin(),vertices.end(),cmp_y);
auto fi = [&](pll x){
return lower_bound(vertices.begin(),vertices.end(),x,
[](point a1,pll val){return MP(a1.y,a1.x) < MP(val.se,val.fi);})-vertices.begin();
};
vector <ll> cnt(sz(vertices));
for (auto x:all){
cnt[fi(MP(X[x.l],x.y))] ++;
cnt[fi(MP(X[x.r],x.y))] --;
}
for (ll i = 0;i < sz(vertices);i ++){
if (i)cnt[i]+=cnt[i-1];
if (cnt[i]){
add_edge(vertices[i],vertices[i+1]);
}
}
for (ll i = 0;i < sz(vertices);i ++){
if (MP(vertices[i].x,vertices[i].y) == MP(ll(X[S]),0LL))S = vertices[i].id;
if (MP(vertices[i].x,vertices[i].y) == MP(ll(X[G]),0LL))G = vertices[i].id;
}
return dijkstra(S,G);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
63068 KB |
Output is correct |
2 |
Correct |
10 ms |
63068 KB |
Output is correct |
3 |
Correct |
10 ms |
63068 KB |
Output is correct |
4 |
Correct |
12 ms |
63064 KB |
Output is correct |
5 |
Correct |
11 ms |
63068 KB |
Output is correct |
6 |
Correct |
12 ms |
63068 KB |
Output is correct |
7 |
Correct |
10 ms |
63068 KB |
Output is correct |
8 |
Correct |
12 ms |
63068 KB |
Output is correct |
9 |
Correct |
11 ms |
63068 KB |
Output is correct |
10 |
Correct |
14 ms |
63068 KB |
Output is correct |
11 |
Correct |
10 ms |
63132 KB |
Output is correct |
12 |
Correct |
10 ms |
63068 KB |
Output is correct |
13 |
Correct |
10 ms |
62928 KB |
Output is correct |
14 |
Correct |
10 ms |
63068 KB |
Output is correct |
15 |
Correct |
10 ms |
63064 KB |
Output is correct |
16 |
Correct |
10 ms |
63068 KB |
Output is correct |
17 |
Correct |
10 ms |
63068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
63064 KB |
Output is correct |
2 |
Correct |
9 ms |
63068 KB |
Output is correct |
3 |
Correct |
411 ms |
127104 KB |
Output is correct |
4 |
Correct |
422 ms |
130812 KB |
Output is correct |
5 |
Correct |
265 ms |
115276 KB |
Output is correct |
6 |
Correct |
255 ms |
112684 KB |
Output is correct |
7 |
Correct |
278 ms |
115768 KB |
Output is correct |
8 |
Correct |
441 ms |
132200 KB |
Output is correct |
9 |
Correct |
344 ms |
125812 KB |
Output is correct |
10 |
Correct |
437 ms |
132896 KB |
Output is correct |
11 |
Correct |
348 ms |
117904 KB |
Output is correct |
12 |
Correct |
245 ms |
99188 KB |
Output is correct |
13 |
Correct |
422 ms |
134976 KB |
Output is correct |
14 |
Correct |
231 ms |
97076 KB |
Output is correct |
15 |
Correct |
223 ms |
99892 KB |
Output is correct |
16 |
Correct |
217 ms |
102800 KB |
Output is correct |
17 |
Correct |
223 ms |
99396 KB |
Output is correct |
18 |
Correct |
252 ms |
115244 KB |
Output is correct |
19 |
Correct |
19 ms |
65064 KB |
Output is correct |
20 |
Correct |
113 ms |
81724 KB |
Output is correct |
21 |
Correct |
221 ms |
103016 KB |
Output is correct |
22 |
Correct |
211 ms |
100976 KB |
Output is correct |
23 |
Correct |
261 ms |
106820 KB |
Output is correct |
24 |
Correct |
207 ms |
101952 KB |
Output is correct |
25 |
Correct |
221 ms |
101228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
76244 KB |
Output is correct |
2 |
Correct |
482 ms |
138040 KB |
Output is correct |
3 |
Correct |
540 ms |
142292 KB |
Output is correct |
4 |
Correct |
605 ms |
147108 KB |
Output is correct |
5 |
Correct |
637 ms |
149428 KB |
Output is correct |
6 |
Correct |
600 ms |
143356 KB |
Output is correct |
7 |
Correct |
268 ms |
106536 KB |
Output is correct |
8 |
Correct |
238 ms |
97908 KB |
Output is correct |
9 |
Correct |
534 ms |
137684 KB |
Output is correct |
10 |
Correct |
248 ms |
112956 KB |
Output is correct |
11 |
Correct |
18 ms |
64324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
76244 KB |
Output is correct |
2 |
Correct |
482 ms |
138040 KB |
Output is correct |
3 |
Correct |
540 ms |
142292 KB |
Output is correct |
4 |
Correct |
605 ms |
147108 KB |
Output is correct |
5 |
Correct |
637 ms |
149428 KB |
Output is correct |
6 |
Correct |
600 ms |
143356 KB |
Output is correct |
7 |
Correct |
268 ms |
106536 KB |
Output is correct |
8 |
Correct |
238 ms |
97908 KB |
Output is correct |
9 |
Correct |
534 ms |
137684 KB |
Output is correct |
10 |
Correct |
248 ms |
112956 KB |
Output is correct |
11 |
Correct |
18 ms |
64324 KB |
Output is correct |
12 |
Correct |
496 ms |
140404 KB |
Output is correct |
13 |
Correct |
450 ms |
145708 KB |
Output is correct |
14 |
Correct |
631 ms |
149336 KB |
Output is correct |
15 |
Correct |
455 ms |
126904 KB |
Output is correct |
16 |
Correct |
463 ms |
134452 KB |
Output is correct |
17 |
Correct |
519 ms |
145816 KB |
Output is correct |
18 |
Correct |
449 ms |
127344 KB |
Output is correct |
19 |
Correct |
454 ms |
133036 KB |
Output is correct |
20 |
Correct |
289 ms |
105064 KB |
Output is correct |
21 |
Correct |
36 ms |
66212 KB |
Output is correct |
22 |
Correct |
350 ms |
126880 KB |
Output is correct |
23 |
Correct |
311 ms |
122656 KB |
Output is correct |
24 |
Correct |
257 ms |
109152 KB |
Output is correct |
25 |
Correct |
276 ms |
118696 KB |
Output is correct |
26 |
Correct |
196 ms |
96432 KB |
Output is correct |
27 |
Correct |
610 ms |
149844 KB |
Output is correct |
28 |
Correct |
416 ms |
145184 KB |
Output is correct |
29 |
Correct |
609 ms |
144068 KB |
Output is correct |
30 |
Correct |
296 ms |
106156 KB |
Output is correct |
31 |
Correct |
587 ms |
138684 KB |
Output is correct |
32 |
Correct |
254 ms |
105520 KB |
Output is correct |
33 |
Correct |
232 ms |
105128 KB |
Output is correct |
34 |
Correct |
246 ms |
107352 KB |
Output is correct |
35 |
Correct |
307 ms |
111620 KB |
Output is correct |
36 |
Correct |
276 ms |
102548 KB |
Output is correct |
37 |
Correct |
216 ms |
100132 KB |
Output is correct |
38 |
Correct |
253 ms |
98152 KB |
Output is correct |
39 |
Correct |
271 ms |
104116 KB |
Output is correct |
40 |
Correct |
208 ms |
100440 KB |
Output is correct |
41 |
Correct |
227 ms |
99248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
63068 KB |
Output is correct |
2 |
Correct |
10 ms |
63068 KB |
Output is correct |
3 |
Correct |
10 ms |
63068 KB |
Output is correct |
4 |
Correct |
12 ms |
63064 KB |
Output is correct |
5 |
Correct |
11 ms |
63068 KB |
Output is correct |
6 |
Correct |
12 ms |
63068 KB |
Output is correct |
7 |
Correct |
10 ms |
63068 KB |
Output is correct |
8 |
Correct |
12 ms |
63068 KB |
Output is correct |
9 |
Correct |
11 ms |
63068 KB |
Output is correct |
10 |
Correct |
14 ms |
63068 KB |
Output is correct |
11 |
Correct |
10 ms |
63132 KB |
Output is correct |
12 |
Correct |
10 ms |
63068 KB |
Output is correct |
13 |
Correct |
10 ms |
62928 KB |
Output is correct |
14 |
Correct |
10 ms |
63068 KB |
Output is correct |
15 |
Correct |
10 ms |
63064 KB |
Output is correct |
16 |
Correct |
10 ms |
63068 KB |
Output is correct |
17 |
Correct |
10 ms |
63068 KB |
Output is correct |
18 |
Correct |
10 ms |
63064 KB |
Output is correct |
19 |
Correct |
9 ms |
63068 KB |
Output is correct |
20 |
Correct |
411 ms |
127104 KB |
Output is correct |
21 |
Correct |
422 ms |
130812 KB |
Output is correct |
22 |
Correct |
265 ms |
115276 KB |
Output is correct |
23 |
Correct |
255 ms |
112684 KB |
Output is correct |
24 |
Correct |
278 ms |
115768 KB |
Output is correct |
25 |
Correct |
441 ms |
132200 KB |
Output is correct |
26 |
Correct |
344 ms |
125812 KB |
Output is correct |
27 |
Correct |
437 ms |
132896 KB |
Output is correct |
28 |
Correct |
348 ms |
117904 KB |
Output is correct |
29 |
Correct |
245 ms |
99188 KB |
Output is correct |
30 |
Correct |
422 ms |
134976 KB |
Output is correct |
31 |
Correct |
231 ms |
97076 KB |
Output is correct |
32 |
Correct |
223 ms |
99892 KB |
Output is correct |
33 |
Correct |
217 ms |
102800 KB |
Output is correct |
34 |
Correct |
223 ms |
99396 KB |
Output is correct |
35 |
Correct |
252 ms |
115244 KB |
Output is correct |
36 |
Correct |
19 ms |
65064 KB |
Output is correct |
37 |
Correct |
113 ms |
81724 KB |
Output is correct |
38 |
Correct |
221 ms |
103016 KB |
Output is correct |
39 |
Correct |
211 ms |
100976 KB |
Output is correct |
40 |
Correct |
261 ms |
106820 KB |
Output is correct |
41 |
Correct |
207 ms |
101952 KB |
Output is correct |
42 |
Correct |
221 ms |
101228 KB |
Output is correct |
43 |
Correct |
79 ms |
76244 KB |
Output is correct |
44 |
Correct |
482 ms |
138040 KB |
Output is correct |
45 |
Correct |
540 ms |
142292 KB |
Output is correct |
46 |
Correct |
605 ms |
147108 KB |
Output is correct |
47 |
Correct |
637 ms |
149428 KB |
Output is correct |
48 |
Correct |
600 ms |
143356 KB |
Output is correct |
49 |
Correct |
268 ms |
106536 KB |
Output is correct |
50 |
Correct |
238 ms |
97908 KB |
Output is correct |
51 |
Correct |
534 ms |
137684 KB |
Output is correct |
52 |
Correct |
248 ms |
112956 KB |
Output is correct |
53 |
Correct |
18 ms |
64324 KB |
Output is correct |
54 |
Correct |
496 ms |
140404 KB |
Output is correct |
55 |
Correct |
450 ms |
145708 KB |
Output is correct |
56 |
Correct |
631 ms |
149336 KB |
Output is correct |
57 |
Correct |
455 ms |
126904 KB |
Output is correct |
58 |
Correct |
463 ms |
134452 KB |
Output is correct |
59 |
Correct |
519 ms |
145816 KB |
Output is correct |
60 |
Correct |
449 ms |
127344 KB |
Output is correct |
61 |
Correct |
454 ms |
133036 KB |
Output is correct |
62 |
Correct |
289 ms |
105064 KB |
Output is correct |
63 |
Correct |
36 ms |
66212 KB |
Output is correct |
64 |
Correct |
350 ms |
126880 KB |
Output is correct |
65 |
Correct |
311 ms |
122656 KB |
Output is correct |
66 |
Correct |
257 ms |
109152 KB |
Output is correct |
67 |
Correct |
276 ms |
118696 KB |
Output is correct |
68 |
Correct |
196 ms |
96432 KB |
Output is correct |
69 |
Correct |
610 ms |
149844 KB |
Output is correct |
70 |
Correct |
416 ms |
145184 KB |
Output is correct |
71 |
Correct |
609 ms |
144068 KB |
Output is correct |
72 |
Correct |
296 ms |
106156 KB |
Output is correct |
73 |
Correct |
587 ms |
138684 KB |
Output is correct |
74 |
Correct |
254 ms |
105520 KB |
Output is correct |
75 |
Correct |
232 ms |
105128 KB |
Output is correct |
76 |
Correct |
246 ms |
107352 KB |
Output is correct |
77 |
Correct |
307 ms |
111620 KB |
Output is correct |
78 |
Correct |
276 ms |
102548 KB |
Output is correct |
79 |
Correct |
216 ms |
100132 KB |
Output is correct |
80 |
Correct |
253 ms |
98152 KB |
Output is correct |
81 |
Correct |
271 ms |
104116 KB |
Output is correct |
82 |
Correct |
208 ms |
100440 KB |
Output is correct |
83 |
Correct |
227 ms |
99248 KB |
Output is correct |
84 |
Correct |
71 ms |
74352 KB |
Output is correct |
85 |
Correct |
541 ms |
145564 KB |
Output is correct |
86 |
Correct |
784 ms |
166368 KB |
Output is correct |
87 |
Correct |
53 ms |
73368 KB |
Output is correct |
88 |
Correct |
60 ms |
73684 KB |
Output is correct |
89 |
Correct |
54 ms |
73132 KB |
Output is correct |
90 |
Correct |
31 ms |
67168 KB |
Output is correct |
91 |
Correct |
12 ms |
63068 KB |
Output is correct |
92 |
Correct |
26 ms |
66192 KB |
Output is correct |
93 |
Correct |
208 ms |
96040 KB |
Output is correct |
94 |
Correct |
37 ms |
68456 KB |
Output is correct |
95 |
Correct |
379 ms |
134832 KB |
Output is correct |
96 |
Correct |
308 ms |
127440 KB |
Output is correct |
97 |
Correct |
256 ms |
111928 KB |
Output is correct |
98 |
Correct |
301 ms |
124588 KB |
Output is correct |
99 |
Correct |
882 ms |
183148 KB |
Output is correct |
100 |
Correct |
590 ms |
152088 KB |
Output is correct |
101 |
Correct |
699 ms |
157340 KB |
Output is correct |
102 |
Correct |
277 ms |
109452 KB |
Output is correct |
103 |
Correct |
229 ms |
111088 KB |
Output is correct |
104 |
Correct |
229 ms |
107776 KB |
Output is correct |
105 |
Correct |
264 ms |
111208 KB |
Output is correct |
106 |
Correct |
224 ms |
108528 KB |
Output is correct |
107 |
Correct |
231 ms |
103616 KB |
Output is correct |
108 |
Correct |
45 ms |
69564 KB |
Output is correct |
109 |
Correct |
472 ms |
132152 KB |
Output is correct |
110 |
Correct |
434 ms |
148088 KB |
Output is correct |
111 |
Correct |
408 ms |
147564 KB |
Output is correct |
112 |
Correct |
282 ms |
112104 KB |
Output is correct |
113 |
Correct |
272 ms |
111132 KB |
Output is correct |