Submission #964611

#TimeUsernameProblemLanguageResultExecution timeMemory
964611kilkuwuRainforest Jumps (APIO21_jumps)C++17
33 / 100
4082 ms13500 KiB
#include "jumps.h" #include <bits/stdc++.h> template <typename Info, typename Merge = std::plus<Info>> struct IterativeSegmentTree { int n; std::vector<Info> T; IterativeSegmentTree() {} IterativeSegmentTree(int _n) : n(2 << std::__lg(_n)), T(2 * n, Info()){}; template <typename Type> IterativeSegmentTree(const std::vector<Type>& a) : n(2 << std::__lg(a.size())), T(2 * n, Info()) { for (int i = n; i < n + (int) a.size(); i++) { T[i] = a[i - n]; } for (int i = n - 1; i > 0; i--) { T[i] = Merge()(T[i << 1], T[i << 1 | 1]); } } template <typename Iterator> IterativeSegmentTree(Iterator first, Iterator last) : n(2 << std::__lg(distance(first, last))), T(2 * n, Info()) { int id = n; for (Iterator it = first; it != last; it++) { T[id++] = *it; } for (int i = n - 1; i > 0; i--) { T[i] = Merge()(T[i << 1], T[i << 1 | 1]); } } void update(int p, const Info& v) { for (T[p += n] = v; p >>= 1;) { T[p] = Merge()(T[p << 1], T[p << 1 | 1]); } } Info query(int l, int r) { Info resl = Info(), resr = Info(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = Merge()(resl, T[l++]); if (r & 1) resr = Merge()(T[--r], resr); } return Merge()(resl, resr); } }; struct Info { std::pair<int, int> mn = {1e9, -1}, mx = {-1e9, -1}; }; Info operator+(const Info& a, const Info& b) { return {std::min(a.mn, b.mn), std::max(a.mx, b.mx)}; } constexpr int mxN = 200002; int L[mxN]; int R[mxN]; int N; std::vector<int> H; IterativeSegmentTree<Info> tree; void init(int _N, std::vector<int> _H) { N = _N; H = _H; std::stack<int> st; for (int i = 0; i < N; i++) { while (st.size() && H[st.top()] <= H[i]) { st.pop(); } L[i] = st.empty() ? -1 : st.top(); st.push(i); } while (st.size()) st.pop(); for (int i = N - 1; i >= 0; i--) { while (st.size() && H[st.top()] <= H[i]) { st.pop(); } R[i] = st.empty() ? -1 : st.top(); st.push(i); } tree = IterativeSegmentTree<Info>(N); for (int i = 0; i < N; i++) { tree.update(i, {{H[i], i}, {H[i], i}}); } } // we answer them in O(n) int minimum_jumps(int A, int B, int C, int D) { int sz_mid = C - B - 1; auto bl = tree.query(A, B + 1); auto br = tree.query(C, D + 1); if (sz_mid > 0) { auto bmid = tree.query(B + 1, C); if (bmid.mx.first >= br.mx.first) { return -1; } } if (H[B] >= br.mx.first) return -1; std::vector<int> dp(N, -1); std::queue<int> q; for (int i = A; i <= B; i++) { dp[i] = 0; q.push(i); } while (q.size()) { int u = q.front(); q.pop(); for (int v : {L[u], R[u]}) { if (v == -1) continue; if (dp[v] != -1) continue; dp[v] = dp[u] + 1; q.emplace(v); } } int ans = 1e9; for (int i = C; i <= D; i++) { if (dp[i] != -1) ans = std::min(ans, dp[i]); } return ans; }

Compilation message (stderr)

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:100:8: warning: variable 'bl' set but not used [-Wunused-but-set-variable]
  100 |   auto bl = tree.query(A, B + 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...