Submission #984478

#TimeUsernameProblemLanguageResultExecution timeMemory
984478shjeongRainforest Jumps (APIO21_jumps)C++17
100 / 100
1078 ms109244 KiB
#include "jumps.h" #include <iostream> #include <cmath> #include <algorithm> #include <numeric> #include <cstring> #include <vector> #include <string> #include <climits> #include <map> #include <set> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <list> using namespace std; typedef long long ll; vector<ll> v; ll n; ll L[202020][22]; ll R[202020][22]; ll M[202020][22]; void init(int N, std::vector<int> H) { n=N; v.resize(n+1,0); for(int i = 1 ; i <= n ; i++)v[i] = H[i-1]; stack<ll> st; for(int i = 1 ; i <= n ; i++){ while(st.size() and v[st.top()] < v[i]){ M[st.top()][0] = R[st.top()][0] = i; st.pop(); } st.push(i); } while(st.size())st.pop(); for(int i = n ; i >= 1 ; i--){ while(st.size() and v[st.top()] < v[i]){ L[st.top()][0] = i; M[st.top()][0] = (v[i] > v[R[st.top()][0]] ? i : R[st.top()][0]); st.pop(); } st.push(i); } for(int j = 1 ; j <= 20 ; j++){ for(int i = 1 ; i <= n ; i++){ L[i][j] = L[L[i][j-1]][j-1]; R[i][j] = R[R[i][j-1]][j-1]; M[i][j] = M[M[i][j-1]][j-1]; } } } ll RMQ(ll l, ll r){ ll ret = l; for(int i = 20 ; i >= 0 ; i--){ if(R[ret][i]>0 and R[ret][i]<=r)ret = R[ret][i]; } return v[ret]; } int minimum_jumps(int A, int B, int C, int D) { A++; B++; C++; D++; ll H = RMQ(B,C-1); ll mx = RMQ(C,D); ll cur = B; for(int i = 20 ; i >= 0 ; i--){ if(L[cur][i]>0 and L[cur][i]>=A and v[L[cur][i]]<=mx)cur = L[cur][i]; } ll ret = 0; for(int i = 20 ; i >= 0 ; i--){ if(M[cur][i]>0 and v[M[cur][i]]<=H)cur = M[cur][i], ret += (1<<i); } if(C <= R[cur][0] and R[cur][0] <= D)return ret+1; if(0<M[cur][0] and v[M[cur][0]]<mx){ cur = M[cur][0], ret++; } for(int i = 20 ; i >= 0 ; i--){ if(R[cur][i]>0 and R[cur][i]<C)cur = R[cur][i], ret += (1<<i); } if(cur<C)cur = R[cur][0], ret++; return C <= cur and cur <= D ? ret : -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...