Submission #979917

#TimeUsernameProblemLanguageResultExecution timeMemory
979917TsaganaRainforest Jumps (APIO21_jumps)C++14
48 / 100
783 ms42756 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 jump[200005][20]; int jump2[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_jump(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_jump() { 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); jump[i][0] = mx; jump2[i][0] = mx2; } jump[n][0] = jump2[n][0] = n; for (int j = 1; j < 20; j++) for (int i = 0; i <= n; i++) jump[i][j] = jump[jump[i][j-1]][j-1]; for (int j = 1; j < 20; j++) for (int i = 0; i <= n; i++) jump2[i][j] = jump2[jump2[i][j-1]][j-1]; } int count(int A, int B, int C, int D) { vector<int> mindis(n, 2e9); pq<pi> q; for (int i = A; i <= B; i++) {mindis[i] = 0;q.push({0, i});} while(!q.empty()) { int i = q.top().F, s = q.top().S; q.pop(); if (mindis[s] != i) continue; if (s >= C && s <= D) return i; if (lj[s] >= 0 && mindis[lj[s]] > i + 1) { mindis[lj[s]] = i + 1; q.push({i + 1, lj[s]}); } if (rj[s] < n && mindis[rj[s]] > i + 1) { mindis[rj[s]] = i + 1; q.push({i + 1, rj[s]}); } } return -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[jump[A][i]] < h[C]) {A = jump[A][i]; ans += (1 << i);} if (jump[A][0] == C) return ans + 1; for (int i = 19; i >= 0; i--) if (h[jump2[A][i]] < h[C]) {A = jump2[A][i]; ans += (1 << i);} if (jump2[A][0] == C) return ans + 1; return -1; } } T; void init(int N, std::vector<int> H) { T.n = N; T.h = H; T.take_jump(H); T.build(); T.h.pb(N + 1); T.set_jump(); 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...