Submission #478223

#TimeUsernameProblemLanguageResultExecution timeMemory
478223khoabrightRainforest Jumps (APIO21_jumps)C++17
37 / 100
4013 ms50448 KiB
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define pii pair<int, int> #define all(x) x.begin(), x.end() #define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i) #define rep1(i, a, b) for (int i = (int)a; i >= (int)b; --i) #define mp make_pair const int N = 2e5 + 5; const int k = 19; int L[N][20], R[N][20], higher[N][20], h[N]; int my_size; bool sub1 = 1; void cal_LR() { rep(i, 1, my_size) if (h[i] != i) { sub1 = 0; break; } if (sub1) return; stack<int> st; rep(i, 1, my_size) { while (!st.empty() && h[st.top()] < h[i]) st.pop(); if (!st.empty()) L[i][0] = st.top(); st.push(i); } while (!st.empty()) st.pop(); rep1(i, my_size, 1) { while (!st.empty() && h[st.top()] < h[i]) st.pop(); if (!st.empty()) R[i][0] = st.top(); st.push(i); } rep(i, 1, my_size) { if (h[L[i][0]] > h[R[i][0]]) higher[i][0] = L[i][0]; else higher[i][0] = R[i][0]; } rep(u, 1, k) { rep(i, 1, my_size) { L[i][u] = L[L[i][u - 1]][u - 1]; R[i][u] = R[R[i][u - 1]][u - 1]; higher[i][u] = higher[higher[i][u - 1]][u - 1]; } } } int minimum_jumps_sub1234(int a, int b, int c, int d) { if (sub1) return c - b; ++a, ++b, ++c, ++d; queue<pii> q; vector<bool> vst(my_size + 1); vst[0] = 1; rep(i, a, b) q.push({i, 0}), vst[i] = 1; while (!q.empty()) { auto [u, w] = q.front(); q.pop(); if (c <= u && u <= d) return w; int v = L[u][0]; if (!vst[v]) { q.push({v, w + 1}); vst[v] = 1; } v = R[u][0]; if (!vst[v]) { q.push({v, w + 1}); vst[v] = 1; } } return -1; } int minimum_jumps_sub5(int s, int t) { ++s, ++t; int ans = 0; //cout << "s,H,L,R="<<s<<' '<<higher[s][0] << ' ' << L[s][0] << ' ' << R[s][0] << '\n'; rep1(u, k, 0) { if (higher[s][u] != 0 && h[higher[s][u]] <= h[t]) { ans += (1 << u); s = higher[s][u]; } } if (s != t) { //cout << "s,ans,R0="<<s<<' '<<ans<<' '<<R[s][0]<<'\n'; rep1(u, k, 0) { if (R[s][u] != 0 && h[R[s][u]] <= h[t]) { ans += (1 << u); s = R[s][u]; } } } if (s != t) { return -1; } else return ans; } int minimum_jumps(int a, int b, int c, int d) { //if (a == b && c == d) return minimum_jumps_sub5(a, c); return minimum_jumps_sub1234(a, b, c, d); } void init(int sz, vector<int> array) { my_size = sz; rep(i, 1, my_size) h[i] = array[i - 1]; cal_LR(); }
#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...