Submission #497656

#TimeUsernameProblemLanguageResultExecution timeMemory
497656Karliver밀림 점프 (APIO21_jumps)C++17
56 / 100
4062 ms47520 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; void init(int N, V<int> a) { for(auto &c : a)--c; forn(i, N) H[i] = a[i]; 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+1); } 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; } 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(1, 1, 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...