#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define ln "\n"
using namespace std;
const ll INF = 2e9;
void solve(){
ll n; cin >> n;
ll sr, sc; cin >> sr >> sc;
ll er, ec; cin >> er >> ec;
vector<ll> a(n);
for (ll i=0; i<n; i++){
cin >> a[i]; a[i]++;
}
vector<array<ll, 3>> e;
vector<ll> st, con;
for (ll i=n-1; i>=0; i--){
con.clear(); con.push_back(1); con.push_back(a[i]);
if (a[i]>=sc) con.push_back(sc);
if (a[i]>=ec) con.push_back(ec);
if (i+1<n) con.push_back(a[i+1]);
if (i) con.push_back(a[i-1]);
sort(con.begin(), con.end());
con.erase(unique(con.begin(), con.end()), con.end());
for (ll j=1; j<(ll)con.size(); j++){
e.push_back({(i+1)*(ll)1e9+con[j-1], (i+1)*(ll)1e9 + con[j], con[j]-con[j-1]});
e.push_back({(i+1)*(ll)1e9+con[j], (i+1)*(ll)1e9+con[j-1], con[j]-con[j-1]});
}
if (i){
for (ll j=0; j<(ll)con.size(); j++){
if (con[j]<=a[i-1]) {
e.push_back({(i)*(ll)1e9+con[j], (i+1)*(ll)1e9+con[j], 1});
e.push_back({(i+1)*(ll)1e9+con[j], (i)*(ll)1e9+con[j], 1});
}
}
e.push_back({(i)*(ll)1e9+a[i-1], (i+1)*(ll)1e9+1, 1});
e.push_back({(i+1)*(ll)1e9+1, (i)*(ll)1e9+a[i-1], 1});
}
if (i+1<n){
for (ll j=0; j<(ll)con.size(); j++){
if (con[j]<=a[i+1]) {
e.push_back({(i+1)*(ll)1e9+con[j], (i+2)*(ll)1e9+con[j], 1});
e.push_back({(i+2)*(ll)1e9+con[j], (i+1)*(ll)1e9+con[j], 1});
}
}
}
while(!st.empty() and a[st.back()]>=a[i]) st.pop_back();
if (!st.empty()){
ll lim=a[st.back()];
for (ll j=con.size()-1; j>=0 and con[j]>lim; j--){
e.push_back({(i+1)*(ll)1e9+con[j], (st.back()+1)*(ll)1e9+a[st.back()], st.back()-i});
}
}
st.push_back(i);
}
st.clear();
for (ll i=0; i<n; i++){
con.clear(); con.push_back(1); con.push_back(a[i]);
if (a[i]>=sc) con.push_back(sc);
if (a[i]>=ec) con.push_back(ec);
if (i+1<n) con.push_back(a[i+1]);
if (i) con.push_back(a[i-1]);
sort(con.begin(), con.end());
con.erase(unique(con.begin(), con.end()), con.end());
while(!st.empty() and a[st.back()]>=a[i]) st.pop_back();
if (!st.empty()){
ll lim=a[st.back()];
for (ll j=con.size()-1; j>=0 and con[j]>lim; j--){
e.push_back({(i+1)*(ll)1e9+con[j], (st.back()+1)*(ll)1e9+a[st.back()], i-st.back()});
}
}
st.push_back(i);
}
vector<ll> pts;
pts.push_back(er*1e9+ec);
pts.push_back(sr*1e9+sc);
for (auto [from, to, w]:e){
// cout << fr << " " << fc << " - " << tr << " " << tc << ": " << w << ln;
pts.push_back(from); pts.push_back(to);
}
sort(pts.begin(), pts.end());
pts.erase(unique(pts.begin(), pts.end()), pts.end());
vector<vector<pair<ll, ll>>> A(pts.size());
auto getid = [&](ll r){
return lower_bound(pts.begin(), pts.end(), r)-pts.begin();
};
for (auto [from, to, w]:e){
assert(w>0);
ll fid = getid(from), tid = getid(to);
A[fid].push_back({tid, w});
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> que;
vector<ll> dist(pts.size(), INF), vis(pts.size()); ll root=getid(sr*1e9+sc);
que.push({0, root}); dist[root]=0;
while (!que.empty()){
auto cur = que.top(); que.pop();
ll u = cur.ss, sw = cur.ff;
if (vis[u]) continue;
vis[u]=1;
for (auto [v, w]:A[u]){
if (dist[v]>w+sw){
que.push({w+sw, v}); dist[v]=w+sw;
}
}
}
cout << dist[getid(er*1e9+ec)] << ln;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll t=1;
// cin >> t;
while (t--){
solve();
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |