Submission #497667

#TimeUsernameProblemLanguageResultExecution timeMemory
497667KarliverRainforest Jumps (APIO21_jumps)C++17
39 / 100
1197 ms92492 KiB
#include <bits/stdc++.h> #include "jumps.h" #define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x) #define all(v) (v).begin(), (v).end() using namespace std; #define forn(i,n) for (int i = 0; i < (n); ++i) #define rforn(i, n) for(int i = (n) - 1;i >= 0;--i) #define sz(x) (int)x.size() #define ff first #define se second #define mp make_pair using ll = long long; int mod = (ll)1e9 + 7; const int INF = 1e9 + 1; const double eps = 1e-7; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template<class T, size_t SZ> using AR = array<T, SZ>; template<class T> using PR = pair<T, T>; template <typename XPAX> bool ckma(XPAX &x, XPAX y) { return (x < y ? x = y, 1 : 0); } template <typename XPAX> bool ckmi(XPAX &x, XPAX y) { return (x > y ? x = y, 1 : 0); } V<int> g[200001]; int dis[200001]; const int L = 19; int pl[200001]; int pr[200001]; int F[200001][L]; int W[200001][L]; int H[200001]; bool firstcase = 1; int n; V<int> S[200001 * 4]; void build(V<int> &a, int v, int tl, int tr) { if (tl == tr) { S[v] = {a[tl]}; } else { int tm = (tl + tr) / 2; build(a, v*2, tl, tm); build(a, v*2+1, tm+1, tr); merge(S[v*2].begin(), S[v*2].end(), S[v*2+1].begin(), S[v*2+1].end(), back_inserter(S[v])); } } int query(int v, int tl, int tr, int l, int r, int x) { if (l > r) return -1; if (l == tl && r == tr) { vector<int>::iterator pos = lower_bound(S[v].begin(), S[v].end(), x); if(pos != S[v].begin()) { pos--; return *pos; } return -1; } int tm = (tl + tr) / 2; return max(query(v*2, tl, tm, l, min(r, tm), x), query(v*2+1, tm+1, tr, max(l, tm+1), r, x)); } void init(int N, V<int> a) { for(auto &c : a)--c; forn(i, N) H[i] = a[i]; n = N; build(a, 1, 0, N-1); H[N] = N; stack<int> st; forn(i, N) { while(sz(st) && H[st.top()] < H[i]) st.pop(); if(sz(st))g[i].push_back(st.top()); pl[H[i]] = sz(st) ? H[st.top()] : N; st.push(i); } while(sz(st))st.pop(); rforn(i, N) { while(sz(st) && H[st.top()] < H[i]) st.pop(); if(sz(st))g[i].push_back(st.top()); pr[H[i]] = sz(st) ? H[st.top()] : N; st.push(i); } forn(i, N) { dis[i] = -1; firstcase &= (H[i] == i); } pl[N] = pr[N] = N; for(int i = 0;i <= N;++i) { F[i][0] = max(pl[i], pr[i]); W[i][0] = pr[i]; } for(int j = 1;j < L;++j) { forn(i,N+1) { F[i][j] = F[F[i][j-1]][j-1]; W[i][j] = W[W[i][j-1]][j-1]; } } } int minimum_jumps(int A, int B, int C, int D) { if(firstcase) return C-B; if(A == B && C == D) { int ret =0 ; int f = H[A]; int t = H[C]; rforn(i, L) if(F[f][i] <= t) f = F[f][i], ret += 1 << i; rforn(i, L) if(W[f][i] <= t) f = W[f][i], ret += 1 << i; if(f != t)return -1; return ret; } if(C == D) { int t = H[C]; int f = query(1, 0, n-1, A, B, t); if(f == -1)return -1; int ret = 0; rforn(i, L) if(F[f][i] <= t) f = F[f][i], ret += 1 << i; rforn(i, L) if(W[f][i] <= t) f = W[f][i], ret += 1 << i; if(f != t)return -1; return ret; } queue<int> q; V<int> clr; auto reset = [&]() { for(auto x : clr) dis[x] = -1; }; for(int a = A;a <= B;++a) { q.push(a); dis[a] = 0; clr.push_back(a); } while(sz(q)) { int v = q.front(); q.pop(); if(C <= v && v <= D) { int rr = dis[v]; reset(); return rr; } for(auto c : g[v]) { if(dis[c] == -1) { dis[c] = dis[v]+1; q.push(c); clr.push_back(c); } } } reset(); return -1; } /* int main() { init(7, {3, 2, 1, 6, 4, 5, 7}); cout << minimum_jumps(3, 4, 6, 6) << '\n'; } */
#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...