Submission #1237412

#TimeUsernameProblemLanguageResultExecution timeMemory
1237412GrayText editor (CEOI24_editor)C++20
56 / 100
4101 ms521120 KiB
#pragma GCC optimize("Ofast,unroll-loops") #include<bits/stdc++.h> #define ll int #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<pair<ll, ll>> pts; pts.push_back({er, ec}); pts.push_back({sr, sc}); for (ll i=0; i<n; i++){ pts.push_back({i+1, 1}); pts.push_back({i+1, a[i]}); if (a[i]>sc and sc>1) pts.push_back({i+1, sc}); if (a[i]>ec and ec>1) pts.push_back({i+1, ec}); if (i+1<n and a[i+1]<=a[i]) pts.push_back({i+1, a[i+1]}); if (i and a[i-1]<=a[i]) pts.push_back({i+1, a[i-1]}); } sort(pts.begin(), pts.end()); pts.erase(unique(pts.begin(), pts.end()), pts.end()); auto getid = [&](ll r, ll c){ ll res=lower_bound(pts.begin(), pts.end(), make_pair(r, c))-pts.begin(); if(res>=(ll)pts.size() or pts[res]!=make_pair(r, c)){ return -1; } return res; }; vector<vector<pair<ll, ll>>> A(pts.size()); 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 and sc>1) con.push_back(sc); if (a[i]>ec and ec>1) con.push_back(ec); if (i+1<n and a[i+1]<=a[i]) con.push_back(a[i+1]); if (i and a[i-1]<=a[i]) con.push_back(a[i-1]); sort(con.begin(), con.end()); con.erase(unique(con.begin(), con.end()), con.end()); // cout << i << ": "; // for (auto x: con) cout << x << " "; // cout << ln; for (ll j=1; j<(ll)con.size(); j++){ ll from = getid(i+1, con[j-1]), to = getid(i+1, con[j]); if (from!=-1 and to!=-1){ A[from].push_back({to, con[j]-con[j-1]}); A[to].push_back({from, con[j]-con[j-1]}); } } if (i){ for (ll j=0; j<(ll)con.size(); j++){ if (con[j]<=a[i-1]) { ll from = getid(i, con[j]), to = getid(i+1, con[j]); if (from!=-1 and to!=-1){ A[from].push_back({to, 1}); A[to].push_back({from, 1}); } } } ll from = getid(i, a[i-1]), to = getid(i+1, 1); if (from!=-1 and to!=-1){ A[from].push_back({to, 1}); A[to].push_back({from, 1}); } } if (i+1<n){ for (ll j=0; j<(ll)con.size(); j++){ if (con[j]<=a[i+1]) { ll from = getid(i+1, con[j]), to = getid(i+2, con[j]); if (from!=-1 and to!=-1){ A[from].push_back({to, 1}); A[to].push_back({from, 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--){ ll from = getid(i+1, con[j]), to = getid(st.back()+1, a[st.back()]); A[from].push_back({to, 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 and sc>1) con.push_back(sc); if (a[i]>ec and ec>1) con.push_back(ec); if (i+1<n and a[i+1]<=a[i]) con.push_back(a[i+1]); if (i and a[i-1]<=a[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--){ ll from = getid(i+1, con[j]), to = getid(st.back()+1, a[st.back()]); A[from].push_back({to, i-st.back()}); } } st.push_back(i); } 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, 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, ec)] << ln; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); ll t=1; // cin >> t; while (t--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...