Submission #964621

#TimeUsernameProblemLanguageResultExecution timeMemory
964621kilkuwuRainforest Jumps (APIO21_jumps)C++17
27 / 100
956 ms105720 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, LOG = 20; int L[LOG][mxN]; int cntL[LOG][mxN]; int R[LOG][mxN]; int cntR[LOG][mxN]; int N; int prefer[LOG][mxN]; int cnt[LOG][mxN]; 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[0][i] = st.empty() ? i : st.top(); cntL[0][i] = L[0][i] != i; 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[0][i] = st.empty() ? i : st.top(); cntR[0][i] = R[0][i] != i; st.push(i); } tree = IterativeSegmentTree<Info>(N); for (int i = 0; i < N; i++) { tree.update(i, {{H[i], i}, {H[i], i}}); } for (int i = 0; i < N; i++) { if (L[0][i] == i && R[0][i] == i) { prefer[0][i] = i; cnt[0][i] = 0; continue; } cnt[0][i] = 1; if (L[0][i] == i) { prefer[0][i] = R[0][i]; } else if (R[0][i] == i) { prefer[0][i] = L[0][i]; } else { prefer[0][i] = H[L[0][i]] > H[R[0][i]] ? L[0][i] : R[0][i]; } } for (int k = 0; k + 1 < LOG; k++) { for (int i = 0; i < N; i++) { prefer[k + 1][i] = prefer[k][prefer[k][i]]; cnt[k + 1][i] = cnt[k][i] + cnt[k][prefer[k][i]]; L[k + 1][i] = L[k][L[k][i]]; R[k + 1][i] = R[k][R[k][i]]; cntL[k + 1][i] = cntL[k][i] + cntL[k][L[k][i]]; cntR[k + 1][i] = cntR[k][i] + cntR[k][R[k][i]]; } } } 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; int jumps = 0; int u = B; for (int k = LOG - 1; k >= 0; k--) { int to = prefer[k][u]; if (H[to] >= H[C]) continue; jumps += cnt[k][u]; u = to; } for (int k = LOG - 1; k >= 0; k--) { int to = R[k][u]; if (to >= C) continue; jumps += cntR[k][u]; u = to; } jumps++; return jumps; }

Compilation message (stderr)

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:134:8: warning: variable 'bl' set but not used [-Wunused-but-set-variable]
  134 |   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...