Submission #922314

#TimeUsernameProblemLanguageResultExecution timeMemory
922314ArpRainforest Jumps (APIO21_jumps)C++17
4 / 100
855 ms63640 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5+10; const int K = 18; const int INF = 1e9; int up[K][N], mvr[K][N], mvl[K][N], h[N], idx[N], lh[N], rh[N], n; int sp[K][N], lg[N],arr[N],ml[N],mr[N],best[N],MAXN; int que(int l, int r) { int i=lg[r-l+1]; return max(sp[i][l], sp[i][r-(1<<i)+1]); } void init(int N, std::vector<int> H) { for(int i = 1;i<=n;i++){ ml[i] = 0; mr[i] = n + 1; arr[i] = H[i - 1]; // one &= (arr[i] == i); } MAXN = n; arr[0] = arr[n + 1] = INF; stack<int> lgg; for(int i = 1;i<=n;i++){ while(lgg.size() > 0 && arr[lgg.top()] < arr[i]) lgg.pop(); if(lgg.size() > 0){ ml[i] = lgg.top(); } lgg.push(i); } stack<int> rg; for(int i = n;i >= 1;i--){ while(rg.size() > 0 && arr[rg.top()] < arr[i]) rg.pop(); if(rg.size() > 0){ mr[i] = rg.top(); } rg.push(i); } for(int i = 1;i<=n;i++){ best[i] = (arr[ml[i]] > arr[mr[i]] ? ml[i] : mr[i]); } n=N; for (int i=0; i<n; ++i) h[i+1]=H[i], idx[h[i+1]]=i+1; h[0]=INF-1, h[n+1]=INF; stack<int> st; st.push(0); for (int i=1; i<=n; ++i) { while (h[st.top()] <= h[i]) st.pop(); assert(!st.empty()); lh[i]=st.top(), st.push(i); } st=stack<int>(); for (int i=1; i<=n; ++i) sp[0][i]=h[i]; for (int j=1; j<K; ++j) { for (int i=1; i+(1<<(j-1))<=n; ++i) sp[j][i]=max(sp[j-1][i], sp[j-1][i+(1<<(j-1))]); } lg[1]=0; for (int i=2; i<N; ++i) lg[i]=lg[i/2]+1; st.push(n+1); for (int i=n; i>=1; --i) { while (h[st.top()] <= h[i]) st.pop(); assert(!st.empty()); rh[i]=st.top(), st.push(i); } // assume A=B, C=D for testing reasons for (int i=n; i>=1; --i) { int j=idx[i]; int r=rh[j], l=lh[j]; assert(h[l] > h[j] && h[j] < h[r]); // if (h[l] > h[r] && l > 0) up[0][j]=l; // else up[0][j]=r; up[0][j] = best[j]; for (int k=1; k<K; ++k) up[k][j]=up[k-1][up[k-1][j]]; } for (int k=0; k<K; ++k) mvr[k][n+1]=n+1; for (int i=n; i>=1; --i) { int j=idx[i]; mvr[0][j]=rh[j]; for (int k=1; k<K; ++k) mvr[k][j]=mvr[k-1][mvr[k-1][j]]; } for (int i=n; i>=1; --i) { int j=idx[i]; mvl[0][j]=lh[j]; for (int k=1; k<K; ++k) mvl[k][j]=mvl[k-1][mvl[k-1][j]]; } } int minimum_jumps(int a, int b, int c, int d) { ++a, ++b, ++c, ++d; int x=b, mxv=que(c, d), ans=0; for (int j=K-1; j>=0; --j) { int u=mvl[j][x]; if (u >= a && h[u] < mxv) x=u; } // find greatest A such that A < c and rh[A] >= c and h[A] < h[C] for (int j=K-1; j>=0; --j) { int u=up[j][x]; if (u < c && rh[u] < c && h[u] < mxv) x=u, ans+=(1<<j); } if (up[0][x] < c && h[up[0][x]] < mxv && rh[x] < c) x=up[0][x], ++ans; for (int j=K-1; j>=0; --j) { int u=mvr[j][x]; if (u < c) x=u, ans+=(1<<j); } if (x >= c && x <= d) return ans; if (rh[x] >= c && rh[x] <= d) return ans+1; return -1; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...