This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |