Submission #769308

#TimeUsernameProblemLanguageResultExecution timeMemory
769308dxz05Rainforest Jumps (APIO21_jumps)C++17
23 / 100
927 ms33304 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; const int MaxN = 200000; const int Log = 18; int N; int low[MaxN][Log], high[MaxN][Log]; int lf[MaxN], rg[MaxN]; vector<int> H; void init(int _N, vector<int> _H) { N = _N, H = _H; stack<int> st; for (int i = 0; i < N; i++){ while (!st.empty() && H[st.top()] < H[i]) st.pop(); lf[i] = (st.empty() ? -1 : st.top()); st.push(i); } while (!st.empty()) st.pop(); for (int i = N - 1; i >= 0; i--){ while (!st.empty() && H[st.top()] < H[i]) st.pop(); rg[i] = (st.empty() ? -1 : st.top()); st.push(i); } memset(low, -1, sizeof(low)); memset(high, -1, sizeof(high)); for (int i = 0; i < N; i++){ int l = lf[i], r = rg[i]; if (l == -1 && r == -1) continue; if (l != -1 && r != -1){ low[i][0] = (H[l] < H[r] ? l : r); high[i][0] = (H[l] > H[r] ? l : r); } else { low[i][0] = high[i][0] = (l != -1 ? l : r); } } for (int j = 1; j < Log; j++){ for (int i = 0; i < N; i++){ if (low[i][j - 1] != -1){ low[i][j] = low[low[i][j - 1]][j - 1]; } if (high[i][j - 1] != -1){ high[i][j] = high[high[i][j - 1]][j - 1]; } } } } int minimum_jumps(int A, int B, int C, int D) { if (A != B || C != D) return 0; if (H[A] > H[C]) return -1; int ans = 0; int v = A; for (int i = Log - 1; i >= 0; i--){ int u = high[v][i]; if (u != -1 && H[u] <= H[C]){ ans += 1 << i; v = u; } } for (int i = Log - 1; i >= 0; i--){ int u = low[v][i]; if (u != -1 && H[u] <= H[C]){ ans += 1 << i; v = u; } } return (C <= v && v <= D ? ans : -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...