#include "walk.h"
#include<bits/stdc++.h>
using namespace std;
vector<int>l,r,y,x,h;
const int base = 1<<17;
int drzewo[2*base];
int wiekszeL(int x, int val){
x+=base;
while(drzewo[x]<val && x&(x-1)){
if(x%2==1)x/=2;
else x--;
}
if(drzewo[x]>=val){
while(x<base)
if(drzewo[x*2+1]>=val)
x=x*2+1;
else
x=x*2;
return x-base;
}
else
return -1;
}
int wiekszeR(int x, int val){
x+=base;
while(drzewo[x]<val && x&(x+1)){
if(x%2==0)x/=2;
else x++;
}
if(drzewo[x]>=val){
while(x<base)
if(drzewo[x*2]>=val)
x=x*2;
else
x=x*2+1;
return x-base;
}
else
return base;
}
int tree[2*base];
void ustaw(int w, int l, int p, int a, int b, int v){
if(a<=l && p<=b){
tree[w] = v;
return;
}
if(b<l || p<a)return;
if(tree[w]!=-1)
tree[w*2] = tree[w*2+1] = tree[w];
ustaw(w*2, l, (l+p)/2, a, b, v);
ustaw(w*2+1, (l+p)/2+1, p, a, b, v);
tree[w] = -1;
}
int odczyt(int w){
int res = -1;
w+=base;
while(w>0){
if(tree[w]!=-1)
res = tree[w];
w/=2;
}
return res;
}
void dodaj(int a, int b, int h){
l.push_back(a);
r.push_back(b);
y.push_back(h);
}
vector<pair<int,long long>>graf[700010];
long long dist[700010];
bool odw[700010];
map<pair<int,int>,int>mapa;
long long odl(pair<int,int>a, pair<int,int>b){
return abs(a.second-b.second);
}
long long odl1(pair<int,int>a, pair<int,int>b){
return abs(x[a.first]-x[b.first]);
}
map<int,vector<pair<int,int>>>poziomy;
long long min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int s, int g) {
x = X;
h = H;
// podzielic kazda pozioma
// dla kazdej poziomej wyznaczyc pozioma w dol z obu koncy
vector<tuple<int,int,int,int>>przedzialy;
int n = x.size();
int m = L.size();
int i;
for(i=0;i<n;i++)
drzewo[i+base]=h[i];
for(i=base-1;i>0;i--)
drzewo[i]=max(drzewo[i*2], drzewo[i*2+1]);
if(s>g)swap(s,g);
for(i=0;i<m;i++){
poziomy[Y[i]].push_back({L[i],R[i]});
if(L[i]<s && R[i]>g){
dodaj(L[i],s,Y[i]);
dodaj(s,g,Y[i]);
dodaj(g,R[i],Y[i]);
}
else if(L[i]<s && R[i]>s){
dodaj(L[i],s,Y[i]);
dodaj(s,R[i],Y[i]);
}
else if(g<R[i] && L[i]<g){
dodaj(L[i],g,Y[i]);
dodaj(g,R[i],Y[i]);
}
else{
dodaj(L[i],R[i],Y[i]);
}
}
m = l.size();
for(i=0;i<m;i++)
przedzialy.push_back({y[i],l[i],r[i],0});
sort(przedzialy.begin(), przedzialy.end());
for(i=0;i<m;i++)
get<3>(przedzialy[i]) = i;
for(i=0;i<n;i++)tree[i+base] = -1;
for(i=base-1;i>0;i--)
tree[i] = -1;
ustaw(1, 0,base-1,s,s,0);
ustaw(1, 0,base-1,g,g,0);
mapa[{s,0}]=0;
mapa[{g,0}]=1;
for(auto j: przedzialy){
auto [Y,L,R,i] = j;
int a = wiekszeR(L, Y);
if(a<=R){
int b = odczyt(a);
if(b!=-1 && mapa.find({a, b})==mapa.end())
mapa[{a,b}] = mapa.size();
if(mapa.find({a, Y})==mapa.end())
mapa[{a,Y}] = mapa.size();
}
a = wiekszeL(R,Y);
if(a>=L){
int b = odczyt(a);
if(b!=-1 &&mapa.find({a, b})==mapa.end())
mapa[{a,b}] = mapa.size();
if(mapa.find({a, Y})==mapa.end())
mapa[{a,Y}] = mapa.size();
}
ustaw(1, 0, base-1, L, R, Y);
}
vector<pair<pair<int,int>,int>>wartosci;
for(auto j=mapa.begin();j!=mapa.end();j++){
auto xd = *j;
wartosci.push_back({xd.first, xd.second});
auto next = j;
next++;
if(next==mapa.end())break;
if((*next).first.first==(*j).first.first){
graf[(*next).second].push_back({(*j).second, odl((*j).first, (*next).first)});
graf[(*j).second].push_back({(*next).second, odl((*j).first, (*next).first)});
}
}
for(auto &j: poziomy){
auto &vec = j.second;
vector<pair<int,int>>dobre;
sort(vec.begin(), vec.end());
int koniec = (*vec.begin()).second, pocz = (*vec.begin()).first;
for(auto k: vec){
if(k.first<=koniec)
koniec = max(k.second, koniec);
else{
dobre.push_back({pocz, koniec});
pocz = k.first;
koniec = k.second;
}
}
dobre.push_back({pocz,koniec});
vec = dobre;
}
mapa.clear();
for(auto j:wartosci)
mapa[{j.first.second,j.first.first}]=j.second;
for(auto j=mapa.begin();j!=mapa.end();j++){
if((*j).first.first==0)continue;
auto next = j;
next++;
if(next==mapa.end())break;
if((*next).first.first==(*j).first.first){
int a =(*j).first.second, b = (*next).first.second;
auto &pom = poziomy[(*j).first.first];
if(upper_bound(pom.begin(), pom.end(), make_pair(a,1000000000))==upper_bound(pom.begin(), pom.end(), make_pair(b,1000000000))){
graf[(*next).second].push_back({(*j).second, x[(*next).first.second]-x[(*j).first.second]});
graf[(*j).second].push_back({(*next).second, x[(*next).first.second]-x[(*j).first.second]});
}
}
}
priority_queue<pair<long long, int>>kolejka;
dist[0] = 1;
kolejka.push({-1,0});
while(kolejka.size()){
auto a = kolejka.top();
kolejka.pop();
if(odw[a.second])continue;
odw[a.second]=1;
for(auto j: graf[a.second]){
if(dist[j.first]==0 || dist[j.first]>dist[a.second]+j.second){
dist[j.first]=dist[a.second]+j.second;
kolejka.push({-dist[j.first], j.first});
}
}
}
return dist[1]-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21852 KB |
Output is correct |
2 |
Correct |
3 ms |
21704 KB |
Output is correct |
3 |
Correct |
3 ms |
21852 KB |
Output is correct |
4 |
Correct |
3 ms |
21852 KB |
Output is correct |
5 |
Correct |
4 ms |
21852 KB |
Output is correct |
6 |
Correct |
3 ms |
21716 KB |
Output is correct |
7 |
Correct |
4 ms |
21852 KB |
Output is correct |
8 |
Correct |
3 ms |
21852 KB |
Output is correct |
9 |
Correct |
3 ms |
21852 KB |
Output is correct |
10 |
Correct |
4 ms |
21852 KB |
Output is correct |
11 |
Correct |
3 ms |
21852 KB |
Output is correct |
12 |
Correct |
3 ms |
21852 KB |
Output is correct |
13 |
Correct |
3 ms |
21852 KB |
Output is correct |
14 |
Correct |
3 ms |
21864 KB |
Output is correct |
15 |
Correct |
3 ms |
21852 KB |
Output is correct |
16 |
Correct |
3 ms |
21852 KB |
Output is correct |
17 |
Correct |
4 ms |
21896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21852 KB |
Output is correct |
2 |
Correct |
3 ms |
21712 KB |
Output is correct |
3 |
Correct |
639 ms |
93652 KB |
Output is correct |
4 |
Correct |
553 ms |
93776 KB |
Output is correct |
5 |
Correct |
333 ms |
74020 KB |
Output is correct |
6 |
Correct |
325 ms |
71396 KB |
Output is correct |
7 |
Correct |
341 ms |
74168 KB |
Output is correct |
8 |
Correct |
678 ms |
95780 KB |
Output is correct |
9 |
Correct |
486 ms |
91312 KB |
Output is correct |
10 |
Correct |
583 ms |
94648 KB |
Output is correct |
11 |
Correct |
484 ms |
83312 KB |
Output is correct |
12 |
Correct |
331 ms |
66484 KB |
Output is correct |
13 |
Correct |
576 ms |
96940 KB |
Output is correct |
14 |
Correct |
286 ms |
66732 KB |
Output is correct |
15 |
Correct |
253 ms |
62332 KB |
Output is correct |
16 |
Correct |
208 ms |
59568 KB |
Output is correct |
17 |
Correct |
207 ms |
57548 KB |
Output is correct |
18 |
Correct |
271 ms |
75476 KB |
Output is correct |
19 |
Correct |
14 ms |
24024 KB |
Output is correct |
20 |
Correct |
126 ms |
43996 KB |
Output is correct |
21 |
Correct |
198 ms |
58296 KB |
Output is correct |
22 |
Correct |
200 ms |
56928 KB |
Output is correct |
23 |
Correct |
270 ms |
70328 KB |
Output is correct |
24 |
Correct |
196 ms |
57540 KB |
Output is correct |
25 |
Correct |
201 ms |
57456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
33988 KB |
Output is correct |
2 |
Correct |
664 ms |
100500 KB |
Output is correct |
3 |
Correct |
738 ms |
104372 KB |
Output is correct |
4 |
Correct |
748 ms |
106624 KB |
Output is correct |
5 |
Correct |
751 ms |
109232 KB |
Output is correct |
6 |
Correct |
665 ms |
103148 KB |
Output is correct |
7 |
Correct |
304 ms |
63936 KB |
Output is correct |
8 |
Correct |
339 ms |
66288 KB |
Output is correct |
9 |
Correct |
617 ms |
99764 KB |
Output is correct |
10 |
Correct |
237 ms |
64688 KB |
Output is correct |
11 |
Correct |
10 ms |
23388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
33988 KB |
Output is correct |
2 |
Correct |
664 ms |
100500 KB |
Output is correct |
3 |
Correct |
738 ms |
104372 KB |
Output is correct |
4 |
Correct |
748 ms |
106624 KB |
Output is correct |
5 |
Correct |
751 ms |
109232 KB |
Output is correct |
6 |
Correct |
665 ms |
103148 KB |
Output is correct |
7 |
Correct |
304 ms |
63936 KB |
Output is correct |
8 |
Correct |
339 ms |
66288 KB |
Output is correct |
9 |
Correct |
617 ms |
99764 KB |
Output is correct |
10 |
Correct |
237 ms |
64688 KB |
Output is correct |
11 |
Correct |
10 ms |
23388 KB |
Output is correct |
12 |
Correct |
764 ms |
103720 KB |
Output is correct |
13 |
Correct |
645 ms |
103592 KB |
Output is correct |
14 |
Correct |
736 ms |
108376 KB |
Output is correct |
15 |
Correct |
465 ms |
87540 KB |
Output is correct |
16 |
Correct |
457 ms |
88104 KB |
Output is correct |
17 |
Correct |
504 ms |
98976 KB |
Output is correct |
18 |
Correct |
473 ms |
86944 KB |
Output is correct |
19 |
Correct |
455 ms |
88260 KB |
Output is correct |
20 |
Correct |
322 ms |
62400 KB |
Output is correct |
21 |
Correct |
20 ms |
25688 KB |
Output is correct |
22 |
Correct |
465 ms |
86960 KB |
Output is correct |
23 |
Correct |
422 ms |
86188 KB |
Output is correct |
24 |
Correct |
313 ms |
70272 KB |
Output is correct |
25 |
Correct |
390 ms |
82004 KB |
Output is correct |
26 |
Correct |
241 ms |
62900 KB |
Output is correct |
27 |
Correct |
742 ms |
108112 KB |
Output is correct |
28 |
Correct |
640 ms |
102572 KB |
Output is correct |
29 |
Correct |
687 ms |
102444 KB |
Output is correct |
30 |
Correct |
324 ms |
63492 KB |
Output is correct |
31 |
Correct |
659 ms |
100272 KB |
Output is correct |
32 |
Correct |
219 ms |
60924 KB |
Output is correct |
33 |
Correct |
202 ms |
60168 KB |
Output is correct |
34 |
Correct |
240 ms |
66480 KB |
Output is correct |
35 |
Correct |
285 ms |
67752 KB |
Output is correct |
36 |
Correct |
258 ms |
62056 KB |
Output is correct |
37 |
Correct |
198 ms |
57648 KB |
Output is correct |
38 |
Correct |
204 ms |
56764 KB |
Output is correct |
39 |
Correct |
258 ms |
70324 KB |
Output is correct |
40 |
Correct |
204 ms |
57540 KB |
Output is correct |
41 |
Correct |
202 ms |
57264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21852 KB |
Output is correct |
2 |
Correct |
3 ms |
21704 KB |
Output is correct |
3 |
Correct |
3 ms |
21852 KB |
Output is correct |
4 |
Correct |
3 ms |
21852 KB |
Output is correct |
5 |
Correct |
4 ms |
21852 KB |
Output is correct |
6 |
Correct |
3 ms |
21716 KB |
Output is correct |
7 |
Correct |
4 ms |
21852 KB |
Output is correct |
8 |
Correct |
3 ms |
21852 KB |
Output is correct |
9 |
Correct |
3 ms |
21852 KB |
Output is correct |
10 |
Correct |
4 ms |
21852 KB |
Output is correct |
11 |
Correct |
3 ms |
21852 KB |
Output is correct |
12 |
Correct |
3 ms |
21852 KB |
Output is correct |
13 |
Correct |
3 ms |
21852 KB |
Output is correct |
14 |
Correct |
3 ms |
21864 KB |
Output is correct |
15 |
Correct |
3 ms |
21852 KB |
Output is correct |
16 |
Correct |
3 ms |
21852 KB |
Output is correct |
17 |
Correct |
4 ms |
21896 KB |
Output is correct |
18 |
Correct |
4 ms |
21852 KB |
Output is correct |
19 |
Correct |
3 ms |
21712 KB |
Output is correct |
20 |
Correct |
639 ms |
93652 KB |
Output is correct |
21 |
Correct |
553 ms |
93776 KB |
Output is correct |
22 |
Correct |
333 ms |
74020 KB |
Output is correct |
23 |
Correct |
325 ms |
71396 KB |
Output is correct |
24 |
Correct |
341 ms |
74168 KB |
Output is correct |
25 |
Correct |
678 ms |
95780 KB |
Output is correct |
26 |
Correct |
486 ms |
91312 KB |
Output is correct |
27 |
Correct |
583 ms |
94648 KB |
Output is correct |
28 |
Correct |
484 ms |
83312 KB |
Output is correct |
29 |
Correct |
331 ms |
66484 KB |
Output is correct |
30 |
Correct |
576 ms |
96940 KB |
Output is correct |
31 |
Correct |
286 ms |
66732 KB |
Output is correct |
32 |
Correct |
253 ms |
62332 KB |
Output is correct |
33 |
Correct |
208 ms |
59568 KB |
Output is correct |
34 |
Correct |
207 ms |
57548 KB |
Output is correct |
35 |
Correct |
271 ms |
75476 KB |
Output is correct |
36 |
Correct |
14 ms |
24024 KB |
Output is correct |
37 |
Correct |
126 ms |
43996 KB |
Output is correct |
38 |
Correct |
198 ms |
58296 KB |
Output is correct |
39 |
Correct |
200 ms |
56928 KB |
Output is correct |
40 |
Correct |
270 ms |
70328 KB |
Output is correct |
41 |
Correct |
196 ms |
57540 KB |
Output is correct |
42 |
Correct |
201 ms |
57456 KB |
Output is correct |
43 |
Correct |
61 ms |
33988 KB |
Output is correct |
44 |
Correct |
664 ms |
100500 KB |
Output is correct |
45 |
Correct |
738 ms |
104372 KB |
Output is correct |
46 |
Correct |
748 ms |
106624 KB |
Output is correct |
47 |
Correct |
751 ms |
109232 KB |
Output is correct |
48 |
Correct |
665 ms |
103148 KB |
Output is correct |
49 |
Correct |
304 ms |
63936 KB |
Output is correct |
50 |
Correct |
339 ms |
66288 KB |
Output is correct |
51 |
Correct |
617 ms |
99764 KB |
Output is correct |
52 |
Correct |
237 ms |
64688 KB |
Output is correct |
53 |
Correct |
10 ms |
23388 KB |
Output is correct |
54 |
Correct |
764 ms |
103720 KB |
Output is correct |
55 |
Correct |
645 ms |
103592 KB |
Output is correct |
56 |
Correct |
736 ms |
108376 KB |
Output is correct |
57 |
Correct |
465 ms |
87540 KB |
Output is correct |
58 |
Correct |
457 ms |
88104 KB |
Output is correct |
59 |
Correct |
504 ms |
98976 KB |
Output is correct |
60 |
Correct |
473 ms |
86944 KB |
Output is correct |
61 |
Correct |
455 ms |
88260 KB |
Output is correct |
62 |
Correct |
322 ms |
62400 KB |
Output is correct |
63 |
Correct |
20 ms |
25688 KB |
Output is correct |
64 |
Correct |
465 ms |
86960 KB |
Output is correct |
65 |
Correct |
422 ms |
86188 KB |
Output is correct |
66 |
Correct |
313 ms |
70272 KB |
Output is correct |
67 |
Correct |
390 ms |
82004 KB |
Output is correct |
68 |
Correct |
241 ms |
62900 KB |
Output is correct |
69 |
Correct |
742 ms |
108112 KB |
Output is correct |
70 |
Correct |
640 ms |
102572 KB |
Output is correct |
71 |
Correct |
687 ms |
102444 KB |
Output is correct |
72 |
Correct |
324 ms |
63492 KB |
Output is correct |
73 |
Correct |
659 ms |
100272 KB |
Output is correct |
74 |
Correct |
219 ms |
60924 KB |
Output is correct |
75 |
Correct |
202 ms |
60168 KB |
Output is correct |
76 |
Correct |
240 ms |
66480 KB |
Output is correct |
77 |
Correct |
285 ms |
67752 KB |
Output is correct |
78 |
Correct |
258 ms |
62056 KB |
Output is correct |
79 |
Correct |
198 ms |
57648 KB |
Output is correct |
80 |
Correct |
204 ms |
56764 KB |
Output is correct |
81 |
Correct |
258 ms |
70324 KB |
Output is correct |
82 |
Correct |
204 ms |
57540 KB |
Output is correct |
83 |
Correct |
202 ms |
57264 KB |
Output is correct |
84 |
Correct |
52 ms |
31620 KB |
Output is correct |
85 |
Correct |
789 ms |
107000 KB |
Output is correct |
86 |
Correct |
902 ms |
124320 KB |
Output is correct |
87 |
Correct |
41 ms |
30448 KB |
Output is correct |
88 |
Correct |
41 ms |
30352 KB |
Output is correct |
89 |
Correct |
41 ms |
30284 KB |
Output is correct |
90 |
Correct |
19 ms |
25616 KB |
Output is correct |
91 |
Correct |
4 ms |
21852 KB |
Output is correct |
92 |
Correct |
18 ms |
24668 KB |
Output is correct |
93 |
Correct |
203 ms |
52296 KB |
Output is correct |
94 |
Correct |
22 ms |
25684 KB |
Output is correct |
95 |
Correct |
490 ms |
90240 KB |
Output is correct |
96 |
Correct |
429 ms |
85676 KB |
Output is correct |
97 |
Correct |
334 ms |
70840 KB |
Output is correct |
98 |
Correct |
396 ms |
81700 KB |
Output is correct |
99 |
Correct |
1081 ms |
144540 KB |
Output is correct |
100 |
Correct |
760 ms |
105668 KB |
Output is correct |
101 |
Correct |
794 ms |
115872 KB |
Output is correct |
102 |
Correct |
306 ms |
63540 KB |
Output is correct |
103 |
Correct |
206 ms |
61104 KB |
Output is correct |
104 |
Correct |
211 ms |
59940 KB |
Output is correct |
105 |
Correct |
245 ms |
66480 KB |
Output is correct |
106 |
Correct |
238 ms |
63276 KB |
Output is correct |
107 |
Correct |
262 ms |
66392 KB |
Output is correct |
108 |
Correct |
37 ms |
28180 KB |
Output is correct |
109 |
Correct |
542 ms |
88296 KB |
Output is correct |
110 |
Correct |
646 ms |
104884 KB |
Output is correct |
111 |
Correct |
623 ms |
103560 KB |
Output is correct |
112 |
Correct |
282 ms |
66744 KB |
Output is correct |
113 |
Correct |
279 ms |
64952 KB |
Output is correct |