Submission #980357

#TimeUsernameProblemLanguageResultExecution timeMemory
980357TsaganaRainforest Jumps (APIO21_jumps)C++14
48 / 100
789 ms49104 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 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]; pi g[800005]; void build(int id, int L, int R) { if (L == R) {t[id] = {h[L], L}; g[id] = {h[L], L}; return ;} meta; build(x, L, M); build(y, M + 1, R); t[id] = max(t[x], t[y]); g[id] = min(g[x], g[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);} pi ub(int id, int L, int R, int i, int k) { if (t[id].F < k) return {-2e9, -1}; if (L == R) return t[id]; meta; if (i <= M) return ub(x, L, M, i, k); if (i < R) { pi ans = ub(y, M + 1, R, i, k); if (ans.S != -1) return ans; return ub(x, L, M, M, k); } if (t[y].F > k) return ub(y, M + 1, R, i, k); return ub(x, L, M, M, k); } pi ub(int R, int k) {return ub(0, 0, n-1, R, k);} pi lb(int id, int L, int R, int l, int r, int k) { if (g[id].F > k) return {-2e9, -1}; if (L == R) return t[id]; meta; if (r <= M) return lb(x, L, M, l, r, k); if (l > M) return lb(y, M + 1, R, l, r, k); if (g[y].F > k) return lb(x, L, M, l, M, k); if (g[x].F > k) return lb(y, M + 1, R, M + 1, r, k); return max(lb(x, L, M, l, M, k), lb(y, M + 1, R, M + 1, r, k)); } pi lb(int L, int R, int k) {return lb(0, 0, n-1, L, R, k);} 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 C) { 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) { if (C > B) return count(query(max(A, lj[C] + 1), B).S, C); return count(query(A, min(rj[C] - 1, B)).S, C); } int count(int A, int B, int C, int D) { if (B + 1 == C) return (query(C, D).F > h[B] ? 1 : -1); if (A == 0) { pi b = query(B + 1, C - 1); pi c = query(C, D); pi l = ub(C-1, c.F); if (b > c) return -1; if (l.S >= B) return -1; if (lb(l.S+1, C-1, c.F).S <= B) return 1; int ans = count(query(l.S+1, B).S, b.S); return (ans == -1 ? -1 : ans + 1); } pi b = query(B + 1, C - 1); pi c = query(C, D); pi l = ub(C-1, c.F); pi a = ub(A-1, b.F); if (b > c) return -1; if (l.S >= B) return -1; if (lb(max(A, l.S+1), C-1, c.F).S <= B) return 1; int ans = count(query(l.S+1, B).S, b.S); if (ans != -1) ans++; if (a.S != -1) { int x = count(A, B, a.S); if (x != -1) ans = min(ans, x + 1); } return ans; } } 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...