Submission #814767

#TimeUsernameProblemLanguageResultExecution timeMemory
814767senthetaRainforest Jumps (APIO21_jumps)C++17
100 / 100
1225 ms50568 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; constexpr int kMaxLogN = 20; int N; vector<int> H; vector<vector<int>> L, R; vector<vector<int>> higher; void init(int _N, std::vector<int> _H) { N = _N; H = _H; H.insert(H.begin(), INT_MAX); H.insert(H.end(), INT_MAX); N += 2; L = R = higher = vector<vector<int>>(kMaxLogN, vector<int>(N)); // leftward edges stack<int> stak; for (int i = 0; i < N; ++i) { while (!stak.empty() && H[stak.top()] <= H[i]) { stak.pop(); } L[0][i] = stak.empty() ? i : stak.top(); stak.push(i); } // rightward edges while (!stak.empty()) { stak.pop(); } for (int i = N - 1; i >= 0; --i) { while (!stak.empty() && H[stak.top()] <= H[i]) { stak.pop(); } R[0][i] = stak.empty() ? i : stak.top(); stak.push(i); } for (int i = 0; i < N; ++i) { higher[0][i] = H[L[0][i]] > H[R[0][i]] ? L[0][i] : R[0][i]; } for (int j = 1; j < kMaxLogN; ++j) { for (int i = 0; i < N; ++i) { L[j][i] = L[j - 1][L[j - 1][i]]; R[j][i] = R[j - 1][R[j - 1][i]]; higher[j][i] = higher[j - 1][higher[j - 1][i]]; } } } int maximum_height_index(int l, int r) { for (int j = kMaxLogN - 1; j >= 0; --j) { if (R[j][l] <= r) { l = R[j][l]; } } return l; } int minimum_jumps(int A, int B, int C, int D) { ++A; ++B; ++C; ++D; // touching if (B == C - 1) { return R[0][B] <= D ? 1 : -1; } int m = maximum_height_index(B + 1, C - 1); // jump over if (H[B] > H[m]) { return R[0][B] <= D ? 1 : -1; } // starting point int s = B; for (int j = kMaxLogN - 1; j >= 0; --j) { if (A <= L[j][s] && H[L[j][s]] < H[m]) { s = L[j][s]; } } int jumps = 0; if (A <= L[0][s]) { // next maximum can jump over if (R[0][L[0][s]] <= D) { return 1; } } else { // limit height by a[m] for (int j = kMaxLogN - 1; j >= 0; --j) { if (H[higher[j][s]] <= H[m]) { jumps |= (1 << j); s = higher[j][s]; } } // stopped at m if (s == m) { return R[0][s]<=D ? jumps+1 : -1; } // if jump over if (0 < L[0][s] && R[0][L[0][s]] <= D) { return jumps+2; } } for (int j = kMaxLogN - 1; j >= 0; --j) { if (R[j][s] < C) { jumps += (1 << j); s = R[j][s]; } } return C<=R[0][s]&&R[0][s]<=D ? jumps+1 : -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...