#include "walk.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF=1e18;
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
vector <int> a[100001],b[100001],V;
vector <pair <int, int>> ke[12000001],ve,nodes;
int n,m,d[12000001],dp[200001],ft[200001][2];
set <int> S;
void add(int i, int j){
a[i].push_back(j);
nodes.push_back({i,j});
}
int f(int i, int j){
return lower_bound(nodes.begin(),nodes.end(),make_pair(i,j))-nodes.begin();
}
void update(int i, int j, int val){
for (;i<=V.size();i+=i&(-i))
ft[i][j]=min(ft[i][j],val);
}
int get(int i, int j){
int res=INF;
for (;i;i-=i&(-i))
res=min(res,ft[i][j]);
return res;
}
int min_distance(vector<int32_t> x, vector<int32_t> h, vector<int32_t> l, vector<int32_t> r, vector<int32_t> y, int32_t s, int32_t g){
n=x.size(),m=l.size();
int ch=1;
for (int i=1;i<n;i++)
ch&=(h[i]==h[0]);
if (s>g)
swap(s,g);
if (s==0&&g==n-1&&ch){
for (int i=0;i<m;i++){
ve.push_back({l[i],y[i]});
if (r[i]==g)
ve.push_back({r[i],y[i]});
}
sort(ve.begin(),ve.end());
for (auto [i,j]:ve)
V.push_back(j);
sort(V.begin(),V.end());
V.resize(unique(V.begin(),V.end())-V.begin());
for (int i=0;i<=V.size();i++)
ft[i][0]=ft[i][1]=INF;
for (int i=ve.size()-1;i>=0;i--){
int Y=ve[i].second,j=lower_bound(V.begin(),V.end(),Y)-V.begin()+1;
if (ve[i].first==g)
dp[i]=Y;
else
dp[i]=min(get(V.size()-j+1,0)-Y,get(j,1)+Y);
update(V.size()-j+1,0,dp[i]+Y);
update(j,1,dp[i]-Y);
}
return dp[0]+ve[0].second+x[g]-x[s];
}
for (int i=0;i<n;i++)
ve.push_back({-h[i],-i});
for (int i=0;i<m;i++)
ve.push_back({-y[i],i+1});
sort(ve.begin(),ve.end());
for (auto [i,j]:ve){
i=-i,j=-j;
if (j>=0){
S.insert(j);
continue;
}
j=-j-1;
for (auto it=S.lower_bound(l[j]);it!=S.end()&&*it<=r[j];it++){
add(*it,j);
b[j].push_back(*it);
}
}
add(s,-1);
add(g,-1);
sort(nodes.begin(),nodes.end());
nodes.resize(unique(nodes.begin(),nodes.end())-nodes.begin());
for (int i=0;i<n;i++)
for (int j=0;j<(int)a[i].size()-1;j++){
int u=f(i,a[i][j]),v=f(i,a[i][j+1]),w=abs((a[i][j]>=0?y[a[i][j]]:0)-(a[i][j+1]>=0?y[a[i][j+1]]:0));
ke[u].push_back({v,w});
ke[v].push_back({u,w});
}
for (int i=0;i<m;i++)
for (int j=0;j<(int)b[i].size()-1;j++){
int u=f(b[i][j],i),v=f(b[i][j+1],i),w=x[b[i][j+1]]-x[b[i][j]];
ke[u].push_back({v,w});
ke[v].push_back({u,w});
}
fill(d,d+nodes.size(),INF);
d[f(s,-1)]=0;
q.push({0,f(s,-1)});
while (!q.empty()){
auto [du,u]=q.top();
q.pop();
if (du!=d[u])
continue;
for (auto [v,w]:ke[u])
if (d[v]>d[u]+w){
d[v]=d[u]+w;
q.push({d[v],v});
}
}
return (d[f(g,-1)]==INF?-1:d[f(g,-1)]);
}
Compilation message
walk.cpp: In function 'void update(long long int, long long int, long long int)':
walk.cpp:19:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (;i<=V.size();i+=i&(-i))
| ~^~~~~~~~~~
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int32_t, int32_t)':
walk.cpp:46:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i=0;i<=V.size();i++)
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
290900 KB |
Output is correct |
2 |
Correct |
64 ms |
291164 KB |
Output is correct |
3 |
Correct |
61 ms |
290896 KB |
Output is correct |
4 |
Correct |
61 ms |
290900 KB |
Output is correct |
5 |
Correct |
62 ms |
290900 KB |
Output is correct |
6 |
Correct |
62 ms |
290896 KB |
Output is correct |
7 |
Correct |
64 ms |
290900 KB |
Output is correct |
8 |
Correct |
61 ms |
290912 KB |
Output is correct |
9 |
Correct |
61 ms |
290896 KB |
Output is correct |
10 |
Correct |
63 ms |
290896 KB |
Output is correct |
11 |
Correct |
61 ms |
291004 KB |
Output is correct |
12 |
Correct |
65 ms |
290900 KB |
Output is correct |
13 |
Correct |
63 ms |
290896 KB |
Output is correct |
14 |
Correct |
61 ms |
290896 KB |
Output is correct |
15 |
Correct |
62 ms |
290896 KB |
Output is correct |
16 |
Correct |
61 ms |
290900 KB |
Output is correct |
17 |
Correct |
62 ms |
290992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
291156 KB |
Output is correct |
2 |
Correct |
65 ms |
290864 KB |
Output is correct |
3 |
Correct |
1040 ms |
392700 KB |
Output is correct |
4 |
Correct |
975 ms |
397692 KB |
Output is correct |
5 |
Correct |
726 ms |
382868 KB |
Output is correct |
6 |
Correct |
588 ms |
372256 KB |
Output is correct |
7 |
Correct |
722 ms |
383060 KB |
Output is correct |
8 |
Correct |
1327 ms |
419856 KB |
Output is correct |
9 |
Correct |
879 ms |
383124 KB |
Output is correct |
10 |
Correct |
1569 ms |
441388 KB |
Output is correct |
11 |
Correct |
580 ms |
346448 KB |
Output is correct |
12 |
Correct |
362 ms |
322960 KB |
Output is correct |
13 |
Correct |
1295 ms |
422484 KB |
Output is correct |
14 |
Correct |
284 ms |
322596 KB |
Output is correct |
15 |
Correct |
321 ms |
323972 KB |
Output is correct |
16 |
Correct |
329 ms |
326868 KB |
Output is correct |
17 |
Correct |
301 ms |
323520 KB |
Output is correct |
18 |
Correct |
353 ms |
332732 KB |
Output is correct |
19 |
Correct |
68 ms |
292300 KB |
Output is correct |
20 |
Correct |
130 ms |
305664 KB |
Output is correct |
21 |
Correct |
344 ms |
322776 KB |
Output is correct |
22 |
Correct |
308 ms |
322284 KB |
Output is correct |
23 |
Correct |
385 ms |
333780 KB |
Output is correct |
24 |
Correct |
314 ms |
323832 KB |
Output is correct |
25 |
Correct |
312 ms |
324024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
293728 KB |
Output is correct |
2 |
Correct |
116 ms |
298944 KB |
Output is correct |
3 |
Correct |
121 ms |
300996 KB |
Output is correct |
4 |
Correct |
137 ms |
304576 KB |
Output is correct |
5 |
Correct |
136 ms |
304392 KB |
Output is correct |
6 |
Correct |
144 ms |
305216 KB |
Output is correct |
7 |
Correct |
107 ms |
300704 KB |
Output is correct |
8 |
Incorrect |
145 ms |
304556 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
293728 KB |
Output is correct |
2 |
Correct |
116 ms |
298944 KB |
Output is correct |
3 |
Correct |
121 ms |
300996 KB |
Output is correct |
4 |
Correct |
137 ms |
304576 KB |
Output is correct |
5 |
Correct |
136 ms |
304392 KB |
Output is correct |
6 |
Correct |
144 ms |
305216 KB |
Output is correct |
7 |
Correct |
107 ms |
300704 KB |
Output is correct |
8 |
Incorrect |
145 ms |
304556 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
290900 KB |
Output is correct |
2 |
Correct |
64 ms |
291164 KB |
Output is correct |
3 |
Correct |
61 ms |
290896 KB |
Output is correct |
4 |
Correct |
61 ms |
290900 KB |
Output is correct |
5 |
Correct |
62 ms |
290900 KB |
Output is correct |
6 |
Correct |
62 ms |
290896 KB |
Output is correct |
7 |
Correct |
64 ms |
290900 KB |
Output is correct |
8 |
Correct |
61 ms |
290912 KB |
Output is correct |
9 |
Correct |
61 ms |
290896 KB |
Output is correct |
10 |
Correct |
63 ms |
290896 KB |
Output is correct |
11 |
Correct |
61 ms |
291004 KB |
Output is correct |
12 |
Correct |
65 ms |
290900 KB |
Output is correct |
13 |
Correct |
63 ms |
290896 KB |
Output is correct |
14 |
Correct |
61 ms |
290896 KB |
Output is correct |
15 |
Correct |
62 ms |
290896 KB |
Output is correct |
16 |
Correct |
61 ms |
290900 KB |
Output is correct |
17 |
Correct |
62 ms |
290992 KB |
Output is correct |
18 |
Correct |
63 ms |
291156 KB |
Output is correct |
19 |
Correct |
65 ms |
290864 KB |
Output is correct |
20 |
Correct |
1040 ms |
392700 KB |
Output is correct |
21 |
Correct |
975 ms |
397692 KB |
Output is correct |
22 |
Correct |
726 ms |
382868 KB |
Output is correct |
23 |
Correct |
588 ms |
372256 KB |
Output is correct |
24 |
Correct |
722 ms |
383060 KB |
Output is correct |
25 |
Correct |
1327 ms |
419856 KB |
Output is correct |
26 |
Correct |
879 ms |
383124 KB |
Output is correct |
27 |
Correct |
1569 ms |
441388 KB |
Output is correct |
28 |
Correct |
580 ms |
346448 KB |
Output is correct |
29 |
Correct |
362 ms |
322960 KB |
Output is correct |
30 |
Correct |
1295 ms |
422484 KB |
Output is correct |
31 |
Correct |
284 ms |
322596 KB |
Output is correct |
32 |
Correct |
321 ms |
323972 KB |
Output is correct |
33 |
Correct |
329 ms |
326868 KB |
Output is correct |
34 |
Correct |
301 ms |
323520 KB |
Output is correct |
35 |
Correct |
353 ms |
332732 KB |
Output is correct |
36 |
Correct |
68 ms |
292300 KB |
Output is correct |
37 |
Correct |
130 ms |
305664 KB |
Output is correct |
38 |
Correct |
344 ms |
322776 KB |
Output is correct |
39 |
Correct |
308 ms |
322284 KB |
Output is correct |
40 |
Correct |
385 ms |
333780 KB |
Output is correct |
41 |
Correct |
314 ms |
323832 KB |
Output is correct |
42 |
Correct |
312 ms |
324024 KB |
Output is correct |
43 |
Correct |
78 ms |
293728 KB |
Output is correct |
44 |
Correct |
116 ms |
298944 KB |
Output is correct |
45 |
Correct |
121 ms |
300996 KB |
Output is correct |
46 |
Correct |
137 ms |
304576 KB |
Output is correct |
47 |
Correct |
136 ms |
304392 KB |
Output is correct |
48 |
Correct |
144 ms |
305216 KB |
Output is correct |
49 |
Correct |
107 ms |
300704 KB |
Output is correct |
50 |
Incorrect |
145 ms |
304556 KB |
Output isn't correct |
51 |
Halted |
0 ms |
0 KB |
- |