Submission #671350

#TimeUsernameProblemLanguageResultExecution timeMemory
671350YENGOYANRainforest Jumps (APIO21_jumps)C++17
60 / 100
4035 ms58024 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; vector<int> l, r, h; vector<vector<int>> upmec, uppoqr; vector<bool> used; bool f = 1; int n; void calcDp(int u, vector<int> &dp, int c, int d){ if(dp[u] != -1) return; if(u >= c && u <= d) { dp[u] = 0; return; } if(l[u] != -1 && r[u] != -1) { calcDp(l[u], dp, c, d); calcDp(r[u], dp, c, d); dp[u] = min(dp[l[u]], dp[r[u]]) + 1; } else if(l[u] != -1) { calcDp(l[u], dp, c, d); dp[u] = dp[l[u]] + 1; } else if(r[u] != -1){ calcDp(r[u], dp, c, d); dp[u] = dp[r[u]] + 1; } else dp[u] = 1e9; } void calcUp(int u){ if(used[u]) return; used[u] = 1; if(r[u] != -1 && l[u] != -1){ if(h[r[u]] > h[l[u]]){ calcUp(r[u]), calcUp(l[u]); upmec[u][0] = r[u]; uppoqr[u][0] = l[u]; for(int i = 1; i < 20; i++){ if(upmec[u][i - 1] == -1) upmec[u][i] = -1; else upmec[u][i] = upmec[upmec[u][i - 1]][i - 1]; if(uppoqr[u][i - 1] == -1) uppoqr[u][i] = -1; else uppoqr[u][i] = uppoqr[uppoqr[u][i - 1]][i - 1]; } } else{ calcUp(r[u]), calcUp(l[u]); upmec[u][0] = l[u]; uppoqr[u][0] = r[u]; for(int i = 1; i < 20; i++){ if(upmec[u][i - 1] == -1) upmec[u][i] = -1; else upmec[u][i] = upmec[upmec[u][i - 1]][i - 1]; if(uppoqr[u][i - 1] == -1) uppoqr[u][i] = -1; else uppoqr[u][i] = uppoqr[uppoqr[u][i - 1]][i - 1]; } } } else if(r[u] != -1){ calcUp(r[u]); upmec[u][0] = uppoqr[u][0] = r[u]; for(int i = 1; i < 20; i++){ if(upmec[u][i - 1] == -1) upmec[u][i] = uppoqr[u][i] = -1; else upmec[u][i] = uppoqr[u][i] = upmec[upmec[u][i - 1]][i - 1]; } } else if(l[u] != -1){ calcUp(l[u]); upmec[u][0] = uppoqr[u][0] = l[u]; for(int i = 1; i < 20; i++){ if(upmec[u][i - 1] == -1) upmec[u][i] = uppoqr[u][i] = -1; else upmec[u][i] = uppoqr[u][i] = upmec[upmec[u][i - 1]][i - 1]; } } else { for(int i = 0; i < 20; i++) uppoqr[u][i] = upmec[u][i] = -1; } } void init(int N, vector<int> H) { n = N, h = H; l = r = vector<int> (n, -1); upmec = uppoqr = vector<vector<int>> (n, vector<int> (20)); used = vector<bool> (n); for(int i = 0; i < n; i++) if(h[i] != i + 1) f = 0; if(f) return; stack<pair<int, int>> st; st.push({1e9, -1}); for(int i = n - 1; i >= 0; i--){ while(st.top().first < h[i]){ st.pop(); } r[i] = st.top().second; st.push({h[i], i}); } while(st.size())st.pop(); st.push({1e9, -1}); for(int i = 0; i < n; i++){ while(st.top().first < h[i]){ st.pop(); } l[i] = st.top().second; st.push({h[i], i}); } for(int i = 0; i < n; i++) { if(!used[i]) calcUp(i); } // up[i][j] } int minimum_jumps(int A, int B, int C, int D) { if(f) return C - B; int ans = 0; if(A == B && C == D){ int node = A; for(int i = 19; i >= 0; i--){ if(upmec[node][i] == -1) continue; if(h[upmec[node][i]] <= h[C]){ ans += (1 << i); node = upmec[node][i]; } } for(int i = 19; i >= 0; i--){ if(uppoqr[node][i] == -1) continue; if(h[uppoqr[node][i]] <= h[C]){ ans += (1 << i); node = uppoqr[node][i]; } } if(node == C) return ans; else return -1; } vector<int> dp(n, -1); for(int i = A; i <= B; i++) calcDp(i, dp, C, D); ans = 2e9; for(int i = A; i <= B; i++){ ans = min(ans, dp[i]); } if (ans >= 1e9) return -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...