Submission #979922

#TimeUsernameProblemLanguageResultExecution timeMemory
979922TsaganaRainforest Jumps (APIO21_jumps)C++14
81 / 100
4035 ms42772 KiB
#include "jumps.h" //OP #include<bits/stdc++.h> #define all(x) x.begin(), x.end() #define pi pair<int, int > #define pq priority_queue #define lb lower_bound #define ub upper_bound #define pb push_back #define eb emplace_back #define mset multiset #define F first #define S second #define meta int M = (L + R) / 2, x = 2 * id + 1, y = x + 1 using namespace std; bool one; struct tree { int n; int lj[200005]; int rj[200005]; int J[200005][20]; int J2[200005][20]; vector<int> h; pi t[800005]; void build(int id, int L, int R) { if (L == R) {t[id] = {h[L], L}; return ;} meta; build(x, L, M); build(y, M + 1, R); t[id] = max(t[x], t[y]); } void build() {build(0, 0, n-1); return ;} pi query(int id, int L, int R, int l, int r) { if (R < l || r < L) return {-2e9, -1}; if (l <= L && r >= R) return t[id]; meta; return max(query(x, L, M, l, r), query(y, M + 1, R, l, r)); } pi query(int L, int R) {return query(0, 0, n-1, L, R);} void take_J(vector<int> H) { stack<int> st; for (int i = 0; i < n; i++) { while (!st.empty() && H[st.top()] < H[i]) st.pop(); if (st.empty()) lj[i] = -1; else lj[i] = 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(); if (st.empty()) rj[i] = n; else rj[i] = st.top(); st.push(i); } } void set_J() { for (int i = 0; i < n; i++) { int mx = lj[i], mx2 = rj[i]; if (mx == -1) mx = n; if (h[mx] < h[mx2]) swap(mx, mx2); J[i][0] = mx; J2[i][0] = mx2; } J[n][0] = J2[n][0] = n; for (int j = 1; j < 20; j++) for (int i = 0; i <= n; i++) J[i][j] = J[J[i][j-1]][j-1]; for (int j = 1; j < 20; j++) for (int i = 0; i <= n; i++) J2[i][j] = J2[J2[i][j-1]][j-1]; } int count(int A, int B, int C) { A = query(max(A, lj[C] + 1), min(B, rj[C] - 1)).S; if (A == -1) return -1; int ans = 0; for (int i = 19; i >= 0; i--) if (h[J[A][i]] < h[C]) {A = J[A][i]; ans += (1 << i);} if (J[A][0] == C) return ans + 1; for (int i = 19; i >= 0; i--) if (h[J2[A][i]] < h[C]) {A = J2[A][i]; ans += (1 << i);} if (J2[A][0] == C) return ans + 1; return -1; } int count(int A, int B, int C, int D) { int mn = 2e9; for (int i = C; i <= D; i++) { int ans = count(A, B, i); if (ans == -1) continue ; mn = min(mn, ans); } return (mn == 2e9 ? -1 : mn); } } T; void init(int N, std::vector<int> H) { T.n = N; T.h = H; T.take_J(H); T.build(); T.h.pb(N + 1); T.set_J(); one = 1; for (int i = 0; i < N; i++) if (H[i] != i+1) {one = 0; break;} } int minimum_jumps(int A, int B, int C, int D) { if (C == D) return T.count(A, B, C); if (one) return C-B; return T.count(A, B, C, D); } //ED
#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...