제출 #598838

#제출 시각아이디문제언어결과실행 시간메모리
598838Jomnoi밀림 점프 (APIO21_jumps)C++17
0 / 100
287 ms53872 KiB
#include <bits/stdc++.h> #include "jumps.h" using namespace std; const int MAX_N = 2e5 + 5; int N; int H[MAX_N]; int L[MAX_N], R[MAX_N], J1[MAX_N][20], J2[MAX_N][20], mx[MAX_N][20]; int table[MAX_N][20]; void init(int n, vector <int> h) { N = n; for(int i = 1; i <= N; i++) { H[i] = h[i - 1]; table[i][0] = H[i]; } H[0] = H[N + 1] = N + 1; for(int j = 1; j < 20; j++) { for(int i = 1; i + (1<<j) - 1 <= N; i++) { table[i][j] = max(table[i][j - 1], table[i + (1<<(j - 1))][j - 1]); } } stack <int> stk; stk.push(0); for(int i = 1; i <= N; i++) { while(!stk.empty() and H[stk.top()] < H[i]) { stk.pop(); } L[i] = stk.top(); stk.push(i); } while(!stk.empty()) { stk.pop(); } stk.push(N + 1); for(int i = N; i >= 1; i--) { while(!stk.empty() and H[stk.top()] < H[i]) { stk.pop(); } R[i] = stk.top(); stk.push(i); } L[N + 1] = 0; R[0] = N + 1; for(int i = 0; i <= N + 1; i++) { if(H[L[i]] > H[R[i]]) { J1[i][0] = L[i]; J2[i][0] = R[i]; } else { J1[i][0] = R[i]; J2[i][0] = L[i]; } mx[i][0] = max(L[i], R[i]); } for(int j = 1; j < 20; j++) { for(int i = 0; i <= N + 1; i++) { J1[i][j] = J1[J1[i][j - 1]][j - 1]; J2[i][j] = J2[J2[i][j - 1]][j - 1]; mx[i][j] = mx[mx[i][j - 1]][j - 1]; } } } int get_max(int l, int r) { if(l > r) { return 0; } int j = log2(r - l + 1); return max(table[l][j], table[r - (1<<j) + 1][j]); } int minimum_jumps(int A, int B, int C, int D) { A++, B++, C++, D++; int lft = get_max(A, B), mid = get_max(B + 1, C - 1), rgh = get_max(C, D); if(max(lft, mid) > rgh) { return -1; } if(lft > mid) { return 1; } int l = A, r = B, now = B; while(l <= r) { int m = (l + r) / 2; if(H[m] < mid) { r = m - 1; now = m; } else { l = m + 1; } } int ans = 0; for(int i = 19; i >= 0; i--) { if(H[J1[now][i]] < rgh and mx[now][i] < C) { now = J1[now][i]; ans += (1<<i); } } if(mx[now][0] >= C) { ans++; now = mx[now][0]; } else { for(int i = 19; i >= 0; i--) { if(H[J1[now][i]] < rgh and J2[now][i] < C) { now = J2[now][i]; ans += (1<<i); } } ans++; now = J1[now][0]; } if(C <= now and now <= D) { return ans; } return -1; }
#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...