Submission #916215

#TimeUsernameProblemLanguageResultExecution timeMemory
916215adhityamvRainforest Jumps (APIO21_jumps)C++17
0 / 100
4053 ms33232 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define mp make_pair #define pii pair<int,int> #define fi first #define se second const int N=200000; const int ln=20; int big[N][ln]; int small[N][ln]; int l[N]; int r[N]; int h[N]; void init(int n,vector<int> hh){ for(int i=0;i<n;i++) (h[i]=hh[i]); stack<int> fl; fl.push(-1); for(int i=0;i<n;i++){ while(fl.top()!=-1 && h[fl.top()]<h[i]) fl.pop(); l[i]=fl.top(); fl.push(i); } stack<int> fr; fr.push(-1); for(int i=n-1;i>=0;i--){ while(fr.top()!=-1 && h[fr.top()]<h[i]) fr.pop(); r[i]=fr.top(); fr.push(i); } for(int i=0;i<n;i++){ if(l[i]==-1){ big[i][0]=small[i][0]=r[i]; continue; } if(r[i]==-1){ big[i][0]=small[i][0]=l[i]; continue; } if(h[l[i]]>h[r[i]]){ big[i][0]=l[i]; small[i][0]=r[i]; } else{ big[i][0]=r[i]; small[i][0]=l[i]; } } for(int j=1;j<ln;j++){ for(int i=0;i<n;i++){ if(big[i][j-1]==-1) big[i][j]=-1; else big[i][j]=big[big[i][j-1]][j-1]; if(small[i][j-1]==-1) small[i][j]=-1; else small[i][j]=small[small[i][j-1]][j-1]; } } } int check(int s,int e){ if(h[s]>=h[e]) return -1; int ans=0; for(int j=ln-1;j>=0;j--){ if(big[s][j]!=-1 && h[big[s][j]]<h[e]){ s=big[s][j]; ans+=(1<<j); } } for(int j=ln-1;j>=0;j--){ if(small[s][j]!=-1 && h[small[s][j]]<h[e]){ s=small[s][j]; ans+=(1<<j); } } if(e==l[s] || e==r[s]) return ans+1; return -1; } int minimum_jumps(int a,int b,int c,int d){ int ans=N; for(int i=a;i<=b;i++){ for(int j=c;j<=d;j++){ int cans=check(i,j); if(cans!=-1) ans=min(cans,ans); } } if(ans==N) ans=-1; return ans; }
#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...