#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define gg exit(0);
const long long INF=1e18+10;
const int N=100010;
long long f[N*12];
vector<int> val;
multiset<long long> up[N],dw[N];
vector<int> has[N];
void upd_u(int i,long long j,int t){
i=lower_bound(all(val),i)-val.begin()+1;
for(;i;i-=i&-i){
if(t) up[i].insert(j); else up[i].erase(up[i].find(j));
}
}
void upd_d(int i,long long j,int t){
i=lower_bound(all(val),i)-val.begin()+1;
for(;i<=val.size();i+=i&-i){
if(t) dw[i].insert(j); else dw[i].erase(dw[i].find(j));
}
}
long long que_u(int i){
i=lower_bound(all(val),i)-val.begin()+1;
long long ret=INF;
for(;i<=val.size();i+=i&-i) if(up[i].size())
ret=min(ret,*up[i].begin());
return ret;
}
long long que_d(int i){
i=lower_bound(all(val),i)-val.begin()+1;
long long ret=INF;
for(;i;i-=i&-i) if(dw[i].size())
ret=min(ret,*dw[i].begin());
return ret;
}
map<ii,int> mp;
vector<ii> ad[N*12];
int mask(int i,int h){
if(mp.count({i,h}))
return mp[{i,h}];
return mp[{i,h}]=mp.size();
}
void add(int i,int j,int c){
//cerr<<i<<" "<<j<<" "<<c<<"\n";
ad[i].pb({j,c});
ad[j].pb({i,c});
}
long long min_distance(vector<int> x,vector<int> h,vector<int> l,vector<int> r,vector<int> y,int s,int g){
if(s==0 && g==x.size()-1){
val=y, val.pb(0), sort(all(val)), val.erase(unique(all(val)),val.end());
vector<ii> q;
forinc(i,1,l.size()){
q.push_back({l[i-1],-i});
q.push_back({r[i-1],i});
}
q.push_back({0,l.size()+1});
q.push_back({0,-l.size()-1});
sort(all(q));
y.emplace_back();
x.push_back(x[0]);
l.push_back(0);
r.push_back(0);
long long ret=INF;
forv(i,q){
if(i.se<0){ i.se=-i.se;
i.se--;
f[i.se]=min(x[l[i.se]]+y[i.se]+que_d(y[i.se]),x[l[i.se]]-y[i.se]+que_u(y[i.se]));
if(i.se==l.size()-1) f[i.se]=0;
upd_d(y[i.se],f[i.se]-x[l[i.se]]-y[i.se],1);
upd_u(y[i.se],f[i.se]-x[l[i.se]]+y[i.se],1);
if(r[i.se]==x.size()-2)
ret=min(ret,f[i.se]+x[r[i.se]]-x[l[i.se]]+y[i.se]);
} else{
i.se--;
upd_d(y[i.se],f[i.se]-x[l[i.se]]-y[i.se],0);
upd_u(y[i.se],f[i.se]-x[l[i.se]]+y[i.se],0);
}
}
return ret==INF ? -1 : ret;
} else{
has[s].push_back(0);
has[g].push_back(0);
set<int> st;
vector<ii> el, q;
int n=x.size();
forinc(i,1,n){
st.insert(i-1);
el.push_back({h[i-1],i-1});
}
sort(all(el));
forinc(i,1,l.size()){
q.push_back({y[i-1],i-1});
}
sort(all(q));
int it=0;
forv(i,q){
while(el[it].fi<i.fi){
st.erase(el[it++].se);
}
auto j=st.lower_bound(l[i.se]);
int pre=-1;
for(;;){
has[*j].push_back(y[i.se]);
if(pre>=0)
add(mask(*j,y[i.se]),mask(pre,y[i.se]),x[*j]-x[pre]);
pre=*j;
if(*j==r[i.se]) break;
j++;
}
}
forinc(i,0,n-1){
sort(all(has[i]));
has[i].erase(unique(all(has[i])),has[i].end());
forinc(j,1,has[i].size()-1){
add(mask(i,has[i][j-1]),mask(i,has[i][j]),has[i][j]-has[i][j-1]);
}
}
//gg
using li=pair<long long,int>;
priority_queue<li,vector<li>,greater<li>> qq;
int ss=mask(s,0), ed=mask(g,0);
qq.push({0,ss});
reset(f,127);
f[ss]=0;
while(qq.size()){
long long c=qq.top().fi; int u=qq.top().se; qq.pop();
if(u==ed) return c;
forv(v,ad[u]){
if(f[v.fi]>c+v.se){
//cerr<<f[v.fi]<<" "<<c+v.se<<"\n";
f[v.fi]=c+v.se;
qq.push({c+v.se,v.fi});
}
}
}
return -1;
}
}
Compilation message
walk.cpp: In function 'void upd_d(int, long long int, int)':
walk.cpp:38:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(;i<=val.size();i+=i&-i){
~^~~~~~~~~~~~
walk.cpp: In function 'long long int que_u(int)':
walk.cpp:46:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(;i<=val.size();i+=i&-i) if(up[i].size())
~^~~~~~~~~~~~
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:75:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(s==0 && g==x.size()-1){
~^~~~~~~~~~~~
walk.cpp:97:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i.se==l.size()-1) f[i.se]=0;
~~~~^~~~~~~~~~~~
walk.cpp:100:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(r[i.se]==x.size()-2)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
49656 KB |
Output is correct |
2 |
Correct |
22 ms |
40192 KB |
Output is correct |
3 |
Correct |
29 ms |
49656 KB |
Output is correct |
4 |
Correct |
27 ms |
49664 KB |
Output is correct |
5 |
Correct |
28 ms |
49792 KB |
Output is correct |
6 |
Correct |
39 ms |
49656 KB |
Output is correct |
7 |
Correct |
27 ms |
49664 KB |
Output is correct |
8 |
Correct |
27 ms |
49664 KB |
Output is correct |
9 |
Correct |
28 ms |
49656 KB |
Output is correct |
10 |
Correct |
28 ms |
49792 KB |
Output is correct |
11 |
Correct |
27 ms |
49656 KB |
Output is correct |
12 |
Correct |
27 ms |
49656 KB |
Output is correct |
13 |
Correct |
27 ms |
49664 KB |
Output is correct |
14 |
Correct |
27 ms |
49664 KB |
Output is correct |
15 |
Correct |
28 ms |
49656 KB |
Output is correct |
16 |
Correct |
25 ms |
40192 KB |
Output is correct |
17 |
Correct |
23 ms |
40200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
49664 KB |
Output is correct |
2 |
Correct |
23 ms |
40320 KB |
Output is correct |
3 |
Correct |
1485 ms |
151048 KB |
Output is correct |
4 |
Correct |
1550 ms |
149228 KB |
Output is correct |
5 |
Correct |
1070 ms |
135788 KB |
Output is correct |
6 |
Correct |
931 ms |
125164 KB |
Output is correct |
7 |
Correct |
1072 ms |
135900 KB |
Output is correct |
8 |
Correct |
1889 ms |
181440 KB |
Output is correct |
9 |
Correct |
1312 ms |
138012 KB |
Output is correct |
10 |
Correct |
2174 ms |
195436 KB |
Output is correct |
11 |
Correct |
793 ms |
101740 KB |
Output is correct |
12 |
Correct |
350 ms |
51192 KB |
Output is correct |
13 |
Correct |
399 ms |
51184 KB |
Output is correct |
14 |
Correct |
379 ms |
80516 KB |
Output is correct |
15 |
Correct |
410 ms |
80492 KB |
Output is correct |
16 |
Correct |
467 ms |
82668 KB |
Output is correct |
17 |
Correct |
522 ms |
81284 KB |
Output is correct |
18 |
Correct |
1688 ms |
129148 KB |
Output is correct |
19 |
Correct |
39 ms |
51064 KB |
Output is correct |
20 |
Correct |
163 ms |
64752 KB |
Output is correct |
21 |
Correct |
130 ms |
49516 KB |
Output is correct |
22 |
Correct |
136 ms |
50412 KB |
Output is correct |
23 |
Correct |
703 ms |
87496 KB |
Output is correct |
24 |
Correct |
203 ms |
52964 KB |
Output is correct |
25 |
Correct |
143 ms |
49524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
43504 KB |
Output is correct |
2 |
Correct |
1037 ms |
50424 KB |
Output is correct |
3 |
Correct |
1079 ms |
50640 KB |
Output is correct |
4 |
Correct |
994 ms |
51912 KB |
Output is correct |
5 |
Correct |
1641 ms |
127328 KB |
Output is correct |
6 |
Correct |
1900 ms |
83528 KB |
Output is correct |
7 |
Correct |
267 ms |
45512 KB |
Output is correct |
8 |
Correct |
341 ms |
47824 KB |
Output is correct |
9 |
Correct |
1534 ms |
87624 KB |
Output is correct |
10 |
Correct |
682 ms |
85168 KB |
Output is correct |
11 |
Correct |
36 ms |
41600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
43504 KB |
Output is correct |
2 |
Correct |
1037 ms |
50424 KB |
Output is correct |
3 |
Correct |
1079 ms |
50640 KB |
Output is correct |
4 |
Correct |
994 ms |
51912 KB |
Output is correct |
5 |
Correct |
1641 ms |
127328 KB |
Output is correct |
6 |
Correct |
1900 ms |
83528 KB |
Output is correct |
7 |
Correct |
267 ms |
45512 KB |
Output is correct |
8 |
Correct |
341 ms |
47824 KB |
Output is correct |
9 |
Correct |
1534 ms |
87624 KB |
Output is correct |
10 |
Correct |
682 ms |
85168 KB |
Output is correct |
11 |
Correct |
36 ms |
41600 KB |
Output is correct |
12 |
Correct |
1103 ms |
50768 KB |
Output is correct |
13 |
Correct |
972 ms |
51996 KB |
Output is correct |
14 |
Correct |
1712 ms |
127176 KB |
Output is correct |
15 |
Correct |
649 ms |
59608 KB |
Output is correct |
16 |
Correct |
755 ms |
59592 KB |
Output is correct |
17 |
Correct |
771 ms |
59464 KB |
Output is correct |
18 |
Correct |
603 ms |
59464 KB |
Output is correct |
19 |
Correct |
707 ms |
59632 KB |
Output is correct |
20 |
Correct |
375 ms |
46400 KB |
Output is correct |
21 |
Correct |
58 ms |
42232 KB |
Output is correct |
22 |
Correct |
446 ms |
47288 KB |
Output is correct |
23 |
Correct |
371 ms |
48120 KB |
Output is correct |
24 |
Correct |
408 ms |
61744 KB |
Output is correct |
25 |
Correct |
336 ms |
46760 KB |
Output is correct |
26 |
Correct |
445 ms |
85100 KB |
Output is correct |
27 |
Correct |
1783 ms |
113768 KB |
Output is correct |
28 |
Correct |
548 ms |
47432 KB |
Output is correct |
29 |
Correct |
1902 ms |
83016 KB |
Output is correct |
30 |
Correct |
281 ms |
45128 KB |
Output is correct |
31 |
Correct |
1565 ms |
86856 KB |
Output is correct |
32 |
Correct |
369 ms |
64504 KB |
Output is correct |
33 |
Correct |
358 ms |
64584 KB |
Output is correct |
34 |
Correct |
601 ms |
66132 KB |
Output is correct |
35 |
Correct |
395 ms |
50524 KB |
Output is correct |
36 |
Correct |
227 ms |
46960 KB |
Output is correct |
37 |
Correct |
128 ms |
46700 KB |
Output is correct |
38 |
Correct |
136 ms |
47208 KB |
Output is correct |
39 |
Correct |
648 ms |
84484 KB |
Output is correct |
40 |
Correct |
210 ms |
50004 KB |
Output is correct |
41 |
Correct |
157 ms |
46840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
49656 KB |
Output is correct |
2 |
Correct |
22 ms |
40192 KB |
Output is correct |
3 |
Correct |
29 ms |
49656 KB |
Output is correct |
4 |
Correct |
27 ms |
49664 KB |
Output is correct |
5 |
Correct |
28 ms |
49792 KB |
Output is correct |
6 |
Correct |
39 ms |
49656 KB |
Output is correct |
7 |
Correct |
27 ms |
49664 KB |
Output is correct |
8 |
Correct |
27 ms |
49664 KB |
Output is correct |
9 |
Correct |
28 ms |
49656 KB |
Output is correct |
10 |
Correct |
28 ms |
49792 KB |
Output is correct |
11 |
Correct |
27 ms |
49656 KB |
Output is correct |
12 |
Correct |
27 ms |
49656 KB |
Output is correct |
13 |
Correct |
27 ms |
49664 KB |
Output is correct |
14 |
Correct |
27 ms |
49664 KB |
Output is correct |
15 |
Correct |
28 ms |
49656 KB |
Output is correct |
16 |
Correct |
25 ms |
40192 KB |
Output is correct |
17 |
Correct |
23 ms |
40200 KB |
Output is correct |
18 |
Correct |
29 ms |
49664 KB |
Output is correct |
19 |
Correct |
23 ms |
40320 KB |
Output is correct |
20 |
Correct |
1485 ms |
151048 KB |
Output is correct |
21 |
Correct |
1550 ms |
149228 KB |
Output is correct |
22 |
Correct |
1070 ms |
135788 KB |
Output is correct |
23 |
Correct |
931 ms |
125164 KB |
Output is correct |
24 |
Correct |
1072 ms |
135900 KB |
Output is correct |
25 |
Correct |
1889 ms |
181440 KB |
Output is correct |
26 |
Correct |
1312 ms |
138012 KB |
Output is correct |
27 |
Correct |
2174 ms |
195436 KB |
Output is correct |
28 |
Correct |
793 ms |
101740 KB |
Output is correct |
29 |
Correct |
350 ms |
51192 KB |
Output is correct |
30 |
Correct |
399 ms |
51184 KB |
Output is correct |
31 |
Correct |
379 ms |
80516 KB |
Output is correct |
32 |
Correct |
410 ms |
80492 KB |
Output is correct |
33 |
Correct |
467 ms |
82668 KB |
Output is correct |
34 |
Correct |
522 ms |
81284 KB |
Output is correct |
35 |
Correct |
1688 ms |
129148 KB |
Output is correct |
36 |
Correct |
39 ms |
51064 KB |
Output is correct |
37 |
Correct |
163 ms |
64752 KB |
Output is correct |
38 |
Correct |
130 ms |
49516 KB |
Output is correct |
39 |
Correct |
136 ms |
50412 KB |
Output is correct |
40 |
Correct |
703 ms |
87496 KB |
Output is correct |
41 |
Correct |
203 ms |
52964 KB |
Output is correct |
42 |
Correct |
143 ms |
49524 KB |
Output is correct |
43 |
Correct |
70 ms |
43504 KB |
Output is correct |
44 |
Correct |
1037 ms |
50424 KB |
Output is correct |
45 |
Correct |
1079 ms |
50640 KB |
Output is correct |
46 |
Correct |
994 ms |
51912 KB |
Output is correct |
47 |
Correct |
1641 ms |
127328 KB |
Output is correct |
48 |
Correct |
1900 ms |
83528 KB |
Output is correct |
49 |
Correct |
267 ms |
45512 KB |
Output is correct |
50 |
Correct |
341 ms |
47824 KB |
Output is correct |
51 |
Correct |
1534 ms |
87624 KB |
Output is correct |
52 |
Correct |
682 ms |
85168 KB |
Output is correct |
53 |
Correct |
36 ms |
41600 KB |
Output is correct |
54 |
Correct |
1103 ms |
50768 KB |
Output is correct |
55 |
Correct |
972 ms |
51996 KB |
Output is correct |
56 |
Correct |
1712 ms |
127176 KB |
Output is correct |
57 |
Correct |
649 ms |
59608 KB |
Output is correct |
58 |
Correct |
755 ms |
59592 KB |
Output is correct |
59 |
Correct |
771 ms |
59464 KB |
Output is correct |
60 |
Correct |
603 ms |
59464 KB |
Output is correct |
61 |
Correct |
707 ms |
59632 KB |
Output is correct |
62 |
Correct |
375 ms |
46400 KB |
Output is correct |
63 |
Correct |
58 ms |
42232 KB |
Output is correct |
64 |
Correct |
446 ms |
47288 KB |
Output is correct |
65 |
Correct |
371 ms |
48120 KB |
Output is correct |
66 |
Correct |
408 ms |
61744 KB |
Output is correct |
67 |
Correct |
336 ms |
46760 KB |
Output is correct |
68 |
Correct |
445 ms |
85100 KB |
Output is correct |
69 |
Correct |
1783 ms |
113768 KB |
Output is correct |
70 |
Correct |
548 ms |
47432 KB |
Output is correct |
71 |
Correct |
1902 ms |
83016 KB |
Output is correct |
72 |
Correct |
281 ms |
45128 KB |
Output is correct |
73 |
Correct |
1565 ms |
86856 KB |
Output is correct |
74 |
Correct |
369 ms |
64504 KB |
Output is correct |
75 |
Correct |
358 ms |
64584 KB |
Output is correct |
76 |
Correct |
601 ms |
66132 KB |
Output is correct |
77 |
Correct |
395 ms |
50524 KB |
Output is correct |
78 |
Correct |
227 ms |
46960 KB |
Output is correct |
79 |
Correct |
128 ms |
46700 KB |
Output is correct |
80 |
Correct |
136 ms |
47208 KB |
Output is correct |
81 |
Correct |
648 ms |
84484 KB |
Output is correct |
82 |
Correct |
210 ms |
50004 KB |
Output is correct |
83 |
Correct |
157 ms |
46840 KB |
Output is correct |
84 |
Correct |
135 ms |
59048 KB |
Output is correct |
85 |
Runtime error |
1227 ms |
334548 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
86 |
Halted |
0 ms |
0 KB |
- |