Submission #434152

#TimeUsernameProblemLanguageResultExecution timeMemory
434152dqhungdlRainforest Jumps (APIO21_jumps)C++17
100 / 100
1635 ms67008 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; typedef pair<int,int> ii; const int MAX=2e5+5; int Lg[MAX],J[MAX][20],JR[MAX][20]; ii f[MAX][20]; ii query(int l,int r) { int k=Lg[r-l+1]; return max(f[l][k],f[r-(1<<k)+1][k]); } void init(int N, std::vector<int> H) { // Init log for(int i=1;i<=N;i++) Lg[i]=log2(i); // Init RMQ for(int i=0;i<N;i++) f[i][0]=ii(H[i],i); for(int k=1;(1<<k)<=N;k++) for(int i=0;i+(1<<k)-1<N;i++) f[i][k]=max(f[i][k-1],f[i+(1<<(k-1))][k-1]); vector<int> L(N),R(N); // Init left for(int i=0; i<N;i++) { int tmp=i-1; while(tmp>=0&&H[tmp]<=H[i]) tmp=L[tmp]; L[i]=tmp; } // Init right for(int i=N-1; i>=0;i--) { int tmp=i+1; while(tmp<N&&H[tmp]<=H[i]) tmp=R[tmp]; R[i]=tmp; } // Init jumps RMQ for(int i=0;i<N;i++) { if(L[i]==-1&&R[i]==N) J[i][0]=-1; else if(L[i]==-1) J[i][0]=R[i]; else if(R[i]==N) J[i][0]=L[i]; else if(H[L[i]]>H[R[i]]) J[i][0]=L[i]; else J[i][0]=R[i]; } for(int k=1;k<20;k++) for(int i=0;i<N;i++) if(J[i][k-1]==-1) J[i][k]=-1; else J[i][k]=J[J[i][k-1]][k-1]; // Init jumps right RMQ for(int i=0;i<N;i++) JR[i][0]=R[i]; for(int k=1;k<20;k++) for(int i=0;i<N;i++) if(JR[i][k-1]==N) JR[i][k]=N; else JR[i][k]=JR[JR[i][k-1]][k-1]; } int minimum_jumps(int A, int B, int C, int D) { // Special cases ii maxCD=query(C,D); if(f[B][0].first>=maxCD.first) return -1; if(B+1==C) return 1; ii maxBC=query(B+1,C-1); if(maxBC.first>=maxCD.first) return -1; // Find the starting position int pos=B; for(int i=19;i>=0;i--) { if((1<<i)>pos-A||f[pos-(1<<i)][i].first>=maxCD.first) continue; pos-=(1<<i); } pos=query(pos,B).second; if(f[pos][0].first>=maxBC.first) return 1; // RMQ jumps int rs=0; for(int i=19;i>=0;i--) { if(J[pos][i]==-1||f[J[pos][i]][0].first>=maxBC.first) continue; pos=J[pos][i]; rs+=(1<<i); } if(J[pos][0]!=-1&&f[J[pos][0]][0].first<maxCD.first) return rs+2; // RMQ jumps right for(int i=19;i>=0;i--) if(JR[pos][i]<C) { rs+=(1<<i); pos=JR[pos][i]; } return rs+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...