Submission #1101863

#TimeUsernameProblemLanguageResultExecution timeMemory
1101863blackslexRainforest Jumps (APIO21_jumps)C++17
27 / 100
953 ms57540 KiB
#include "jumps.h" #include <vector> #include<bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int MxN = 2e5 + 5, K = 19; int n, rt, t, h[MxN], l[MxN], r[MxN], tin[MxN], tout[MxN], dep[MxN], dp[K][MxN]; pii dp2[K][MxN]; void dfs (int cur, int par, int parl, int parr) { tin[cur] = ++t; dep[cur] = dep[par] + 1; dp[0][cur] = (dep[parl] < dep[parr] ? parl : parr); if (l[cur]) dfs(l[cur], cur, parl, cur); if (r[cur]) dfs(r[cur], cur, cur, parr); tout[cur] = t; } pii get (int l, int r) { int lg = 31 - __builtin_clz(r - l + 1); return max(dp2[lg][l], dp2[lg][r - (1 << lg) + 1]); } void init(int N, std::vector<int> H) { n = N; for (int i = 1; i <= n; i++) h[i] = H[i - 1]; stack<int> st; for (int i = 1; i <= n; i++) { while (!st.empty() && h[i] > h[st.top()]) l[i] = st.top(), st.pop(); if (!st.empty()) r[st.top()] = i; else rt = i; st.emplace(i); } dfs(rt, 0, 0, 0); for (int i = 1; i <= n; i++) dp2[0][i] = {h[i], i}; for (int i = 1; i < K; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = dp[i - 1][dp[i - 1][j]]; dp2[i][j] = max(dp2[i - 1][j], dp2[i - 1][j + (1 << (i - 1))]); } } } int minimum_jumps(int A, int B, int C, int D) { int ans = 0; int st = get(A + 1, B + 1).second, ed = C + 1; if (tin[ed] > tin[st] || tout[st] > tout[ed]) return -1; for (int i = K - 1; i >= 0; i--) { if (dep[dp[i][st]] >= dep[ed]) st = dp[i][st], ans += (1 << i); } ans += dep[st] - dep[ed]; 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...