Submission #949778

#TimeUsernameProblemLanguageResultExecution timeMemory
949778AiperiiiRainforest Jumps (APIO21_jumps)C++14
27 / 100
1148 ms42760 KiB
#include <bits/stdc++.h> #include "jumps.h" //#define int long long #define ff first #define ss second #define pb push_back #define all(x) x.begin(),x.end() using namespace std; const int N=2e5+5; int jmp_high[N][20],jmp_low[N][20],t[N*4],ind[N*4]; vector <int> h; int n; void build(int v,int tl,int tr){ if(tl==tr){ t[v]=h[tl]; ind[v]=tl; } else{ int tm=(tl+tr)/2; build(v*2,tl,tm); build(v*2+1,tm+1,tr); if(t[v*2]<t[v*2+1]){ t[v]=t[v*2+1]; ind[v]=ind[v*2+1]; } else{ t[v]=t[v*2]; ind[v]=ind[v*2]; } } } pair <int,int> get(int v,int tl,int tr,int l,int r){ if(r<tl or tr<l)return {-1e9,-1e9}; if(l<=tl && tr<=r)return {t[v],ind[v]}; pair <int,int> v1,v2; int tm=(tl+tr)/2; v1=get(v*2,tl,tm,l,r); v2=get(v*2+1,tm+1,tr,l,r); if(v1.ff<v2.ff)return v2; else return v1; } void init(int N,vector<int> H) { stack <int> st; n=N; for(int i=0;i<N;i++)h.pb(H[i]); h.pb(1e9);H.pb(1e9); vector <int> l(N+1,N),r(N+1,N); for(int i=0;i<N;i++){ while(!st.empty() && H[st.top()]<H[i]){ r[st.top()]=i; st.pop(); } st.push(i); } while(!st.empty())st.pop(); for(int i=N-1;i>=0;i--){ while(!st.empty() && H[st.top()]<H[i]){ l[st.top()]=i; st.pop(); } st.push(i); } for(int i=0;i<=N;i++){ if(H[l[i]]>H[r[i]]){ jmp_high[i][0]=l[i]; jmp_low[i][0]=r[i]; } else{ jmp_high[i][0]=r[i]; jmp_low[i][0]=l[i]; } } for(int k=1;k<20;k++){ for(int i=0;i<=N;i++){ jmp_high[i][k]=jmp_high[jmp_high[i][k-1]][k-1]; jmp_low[i][k]=jmp_low[jmp_low[i][k-1]][k-1]; } } build(1,0,N-1); } int minimum_jumps(int A, int B, int C, int D) { int l=A,r=B; int pos=A; while(l<=r){ int md=(l+r)/2; pair <int,int> p=get(1,0,n-1,md,B); if(p.ff<=h[C])r=md-1; else{ pos=md; l=md+1; } } A=get(1,0,n-1,pos,B).ss; int ans=0; for(int k=19;k>=0;k--){ if(h[jmp_high[A][k]]<=h[C]){ A=jmp_high[A][k]; ans+=(1<<k); } } for(int k=19;k>=0;k--){ if(h[jmp_low[A][k]]<=h[C]){ A=jmp_low[A][k]; ans+=(1<<k); } } if(A==C)return ans; return -1; } /*signed main(){ ios_base::sync_with_stdio(); cin.tie(0);cout.tie(0); int n,q; cin>>n>>q; vector <int> a(n); for(int i=0;i<n;i++)cin>>a[i]; init(n,a); for(int i=0;i<q;i++){ int a,b,c,d; cin>>a>>b>>c>>d; cout<<minimum_jumps(a,b,c,d)<<"\n"; } }*/ /* 7 10 3 2 1 6 4 5 7 */
#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...