Submission #1237360

#TimeUsernameProblemLanguageResultExecution timeMemory
1237360GrayText editor (CEOI24_editor)C++20
45 / 100
4051 ms1074144 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<array<ll, 5>> e; vector<ll> st; vector<vector<ll>> ptsc(n); vector<ll> 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()); ptsc[i]=con; for (ll j=1; j<(ll)con.size(); j++){ e.push_back({i+1, con[j-1], i+1, con[j], con[j]-con[j-1]}); e.push_back({i+1, con[j], i+1, 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, con[j], i+1, con[j], 1}); e.push_back({i+1, con[j], i, con[j], 1}); } } e.push_back({i, a[i-1], i+1, 1, 1}); e.push_back({i+1, 1, i, 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, con[j], i+2, con[j], 1}); e.push_back({i+2, con[j], i+1, 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, con[j], st.back()+1, a[st.back()], st.back()-i}); } } st.push_back(i); } st.clear(); for (ll i=0; i<n; i++){ while(!st.empty() and a[st.back()]>=a[i]) st.pop_back(); if (!st.empty()){ ll lim=a[st.back()]; for (ll j=ptsc[i].size()-1; j>=0 and ptsc[i][j]>lim; j--){ e.push_back({i+1, ptsc[i][j], st.back()+1, a[st.back()], i-st.back()}); } } st.push_back(i); } vector<pair<ll, ll>> pts; pts.push_back({er, ec}); pts.push_back({sr, sc}); for (auto [fr, fc, tr, tc, w]:e){ // cout << fr << " " << fc << " - " << tr << " " << tc << ": " << w << ln; pts.push_back({fr, fc}); pts.push_back({tr, tc}); } 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, ll c){ return lower_bound(pts.begin(), pts.end(), make_pair(r, c))-pts.begin(); }; for (auto [fr, fc, tr, tc, w]:e){ assert(w>0); ll fid = getid(fr, fc), tid = getid(tr, tc); 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, 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...