제출 #467756

#제출 시각아이디문제언어결과실행 시간메모리
467756spike1236밀림 점프 (APIO21_jumps)C++14
81 / 100
4091 ms56768 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define f first #define s second #define ll long long #define ld long double #define all(_v) _v.begin(), _v.end() #define sz(_v) (int)_v.size() #define pii pair <int, int> #define pll pair <ll, ll> #define veci vector <int> #define vecll vector <ll> const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const double PI = 3.1415926535897932384626433832795; const double eps = 1e-9; const int MOD1 = 1e9 + 7; const int MOD2 = 998244353; const int MAXN = 2e5 + 10; int a[MAXN]; int L[MAXN], R[MAXN]; int n; bool issorted; int up_L[MAXN][22], up_R[MAXN][22], up_h[MAXN][22]; int dist[MAXN]; void init(int _n, veci _a) { n = _n; for(int i = 0; i < _n; ++i) a[i + 1] = _a[i]; issorted = 1; for(int i = 1; i <= n; ++i) issorted &= (a[i] == 1 + a[i - 1]); a[0] = a[n + 1] = n + 1; stack <int> st; for(int i = 1; i <= n; ++i) { while(!st.empty() && a[st.top()] < a[i]) st.pop(); if(!st.empty()) L[i] = st.top(); else L[i] = 0; st.push(i); } while(!st.empty()) st.pop(); for(int i = n; i > 0; --i) { while(!st.empty() && a[st.top()] < a[i]) st.pop(); if(!st.empty()) R[i] = st.top(); else R[i] = n + 1; st.push(i); } for(int i = 1; i <= n; ++i) { up_L[i][0] = L[i]; up_R[i][0] = R[i]; up_h[i][0] = (a[L[i]] > a[R[i]] ? L[i] : R[i]); } for(int j = 1; j < 20; ++j) { for(int i = 1; i <= n; ++i) { up_L[i][j] = up_L[up_L[i][j - 1]][j - 1]; up_R[i][j] = up_R[up_R[i][j - 1]][j - 1]; up_h[i][j] = up_h[up_h[i][j - 1]][j - 1]; } } } int minimum_jumps_eq(int l, int x) { int ans = 0; for(int i = 19; i >= 0; --i) if(a[up_h[l][i]] <= a[x]) l = up_h[l][i], ans += (1 << i); if(a[L[l]] > a[x]) { for(int i = 19; i >= 0; --i) if(a[up_R[l][i]] <= a[x]) l = up_R[l][i], ans += (1 << i); } else { for(int i = 19; i <= 0; --i) if(a[up_L[l][i]] <= a[x]) l = up_L[l][i], ans += (1 << i); } return (l == x ? ans : -1); } int minimum_jumps(int l, int r, int x, int y) { ++l, ++r, ++x, ++y; if(issorted) return x - r; if(x == y) { for(int i = 19; i >= 0; --i) if(up_L[r][i] >= l && a[up_L[r][i]] < a[x]) r = up_L[r][i]; return minimum_jumps_eq(r, x); } queue <int> q; for(int i = 1; i <= n; ++i) dist[i] = 1e9; for(int i = l; i <= r; ++i) { q.push(i); dist[i] = 0; } while(!q.empty()) { int v = q.front(); q.pop(); if(dist[L[v]] > dist[v] + 1) { dist[L[v]] = dist[v] + 1; q.push(L[v]); } if(dist[R[v]] > dist[v] + 1) { dist[R[v]] = dist[v] + 1; q.push(R[v]); } } int ans = 1e9; for(int i = x; i <= y; ++i) ans = min(ans, dist[i]); return (ans == 1e9 ? -1 : ans); }
#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...