#include "walk.h"
#include "bits/stdc++.h"
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) ((int)((x).size()))
#define pb push_back
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
const ll LINF=1e18;
const int N=1e5+10;
int n, m;
vector<pii> ins;
ll d[N];
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int str, int g) {
n=sz(x); m=sz(l);
FOR(i, 0, m) {
l[i]=x[l[i]];
r[i]=x[r[i]];
d[i]=LINF;
}
FOR(i, 0, m) {
ins.pb({l[i],i});
ins.pb({r[i],i});
if(l[i]==x[0]) d[i]=min(d[i], 1LL*y[i]+r[i]-l[i]);
}
sort(ins.begin(), ins.end());
set<pii> st;
ll ans=LINF;
FOR(i, 0, 2*m) {
vector<pii> now;
while(i+1<2*m && ins[i+1].f==ins[i].f) {
int pos=ins[i].f, in=ins[i].s;
if(pos==l[in]) st.insert({y[in],in});
else now.pb(ins[i]);
i++;
}
int poss=ins[i].f, inn=ins[i].s;
if(poss==l[inn]) st.insert({y[inn],inn});
else now.pb(ins[i]);
for(auto[pos, in] : now) {
assert(pos!=l[in]);
st.erase({y[in],in});
}
for(auto[pos, in] : now) {
// cout << pos << ": ";
assert(pos!=l[in]);
auto up=st.lower_bound({y[in],0});
// cout << pos << ": ";
if(up!=st.end()) {
ll new_up=d[in] + abs(up->f - y[in]) + r[up->s]-r[in];
d[up->s]=min(d[up->s], new_up);
// cout << "u: " << up->f << " ";
}
auto down=st.upper_bound({y[in],0});
if(down!=st.begin()) {
--down;
ll new_down=d[in] + abs(down->f - y[in]) + r[down->s]-r[in];
d[down->s]=min(d[down->s], new_down);
// cout << "d: " << up->f << " ";
}
if(r[in]==x[n-1]) {
ans=min(ans, d[in]+y[in]);
}
}
}
// FOR(i, 0, m) {
// cout << i << ": " << d[i] << endl;
// }
return ans;
}
/*
3 5
0 3
3 3
5 2
0 1 2
0 1 1
1 2 1
1 2 2
0 1 3
0 2
ans: 7
1:
6 6
0 4
1 4
2 4
4 4
7 4
9 4
0 2 3
0 1 1
1 2 2
1 4 0
2 3 1
2 5 4
0 5
ans: 17
2:
5 4
0 4
2 4
4 4
5 4
6 4
0 1 2
1 2 1
1 3 3
1 4 4
0 4
ans: 14
3:
7 5
0 4
2 4
3 4
4 4
6 4
8 4
9 4
0 1 1
1 3 2
2 4 0
3 4 1
3 6 3
0 6
in1: 28
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3280 KB |
Output is correct |
2 |
Correct |
72 ms |
5580 KB |
Output is correct |
3 |
Correct |
71 ms |
5840 KB |
Output is correct |
4 |
Correct |
84 ms |
7120 KB |
Output is correct |
5 |
Correct |
102 ms |
11396 KB |
Output is correct |
6 |
Correct |
95 ms |
9152 KB |
Output is correct |
7 |
Correct |
36 ms |
4560 KB |
Output is correct |
8 |
Correct |
44 ms |
7236 KB |
Output is correct |
9 |
Correct |
99 ms |
9920 KB |
Output is correct |
10 |
Correct |
57 ms |
9404 KB |
Output is correct |
11 |
Incorrect |
5 ms |
1116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3280 KB |
Output is correct |
2 |
Correct |
72 ms |
5580 KB |
Output is correct |
3 |
Correct |
71 ms |
5840 KB |
Output is correct |
4 |
Correct |
84 ms |
7120 KB |
Output is correct |
5 |
Correct |
102 ms |
11396 KB |
Output is correct |
6 |
Correct |
95 ms |
9152 KB |
Output is correct |
7 |
Correct |
36 ms |
4560 KB |
Output is correct |
8 |
Correct |
44 ms |
7236 KB |
Output is correct |
9 |
Correct |
99 ms |
9920 KB |
Output is correct |
10 |
Correct |
57 ms |
9404 KB |
Output is correct |
11 |
Incorrect |
5 ms |
1116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |