Submission #484113

#TimeUsernameProblemLanguageResultExecution timeMemory
484113MilosMilutinovicRainforest Jumps (APIO21_jumps)C++14
100 / 100
1422 ms161692 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; const int N=200050; const int LOG=25; int a[N],l[N],r[N],L[N][LOG],st[N][LOG]; tuple<int,int,int> mn[N][LOG],mx[N][LOG]; int getmx(int l,int r){ int res=0; for(int i=LOG-1;~i;i--)if(l+(1<<i)-1<=r)res=max(res,st[l][i]),l+=(1<<i); return res; } void init(int n,vector<int> h){ for(int i=1;i<=n;i++)a[i]=h[i-1],st[i][0]=a[i]; for(int j=1;j<LOG;j++)for(int i=1;i+(1<<j)<=n+1;i++)st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]); stack<int> stk; for(int i=1;i<=n;i++){ while(!stk.empty()&&a[stk.top()]<a[i])stk.pop(); if(!stk.empty())l[i]=stk.top(); else l[i]=0; stk.push(i); } while(stk.size())stk.pop(); for(int i=n;i>=1;i--){ while(!stk.empty()&&a[stk.top()]<a[i])stk.pop(); if(!stk.empty())r[i]=stk.top(); else r[i]=0; stk.push(i); } for(int i=1;i<=n;i++){ if(r[i]!=0&&(l[i]==0||a[l[i]]>a[r[i]]))mn[i][0]={r[i],r[i],r[i]}; else mn[i][0]={l[i],r[i],r[i]}; if(r[i]!=0&&(l[i]==0||a[l[i]]<a[r[i]]))mx[i][0]={r[i],r[i],r[i]}; else mx[i][0]={l[i],r[i],r[i]}; L[i][0]=l[i]; } for(int j=1;j<LOG;j++){ for(int i=1;i<=n;i++){ mn[i][j]=mn[get<0>(mn[i][j-1])][j-1]; mx[i][j]=mx[get<0>(mx[i][j-1])][j-1]; L[i][j]=L[L[i][j-1]][j-1]; get<1>(mn[i][j])=max(get<1>(mn[i][j]),get<1>(mn[i][j-1])); get<1>(mx[i][j])=max(get<1>(mx[i][j]),get<1>(mx[i][j-1])); get<2>(mn[i][j])=max(get<2>(mn[i][j]),get<2>(mn[i][j-1])); get<2>(mx[i][j])=max(get<2>(mx[i][j]),get<2>(mx[i][j-1])); } } } //bool check(int A,int B){ // for(int i=LOG-1;~i;i--)if(mn[A][i]!=0&&a[mn[A][i]]<=a[B])A=mn[A][i]; // return A==B; //} int minimum_jumps(int A,int B,int C,int D){ A++;B++;C++;D++; int x=getmx(C,D),pos=B; for(int i=LOG-1;~i;i--)if(L[pos][i]>=A&&r[L[pos][i]]!=0&&r[L[pos][i]]<=D)pos=L[pos][i]; int ans=0; for(int i=LOG-1;~i;i--){ if(get<0>(mx[pos][i])>0&&get<1>(mx[pos][i])<C&&r[get<0>(mx[pos][i])]!=0&&r[get<0>(mx[pos][i])]<=D&&get<2>(mx[pos][i])<=D)pos=get<0>(mx[pos][i]),ans+=(1<<i); } for(int i=LOG-1;~i;i--){ if(get<0>(mn[pos][i])>0&&get<1>(mn[pos][i])<C&&r[get<0>(mn[pos][i])]!=0&&r[get<0>(mn[pos][i])]<=D&&get<2>(mn[pos][i])<=D)pos=get<0>(mn[pos][i]),ans+=(1<<i); assert(pos<=D); } if(r[pos]>=C&&r[pos]<=D)ans++,pos=r[pos]; if(pos>=C&&pos<=D)return ans; return -1; } //int main(){ // int d; // scanf("%d",&d); // vector<int> A(d); // for(int i=0;i<d;i++)scanf("%d",&A[i]); // init(d,A); // printf("%d\n",minimum_jumps(4,4,6,6)); // printf("%d\n",minimum_jumps(1,3,5,6)); // printf("%d\n",minimum_jumps(0,1,2,2)); // return 0; //} /* 7 3 2 1 6 4 5 7 */

Compilation message (stderr)

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:55:6: warning: unused variable 'x' [-Wunused-variable]
   55 |  int x=getmx(C,D),pos=B;
      |      ^
#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...