Submission #555973

#TimeUsernameProblemLanguageResultExecution timeMemory
555973joelauRainforest Jumps (APIO21_jumps)C++14
100 / 100
1170 ms66104 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; int n, p1[200005][20], p2[200005][20], p3[200005][20], p4[200005][20], X[200005]; stack<int> s; void init(int N, std::vector<int> H) { n = N; for (int i = 0; i < N; ++i) X[i] = H[i]; memset(p1,-1,sizeof(p1)); memset(p2,-1,sizeof(p2)); memset(p3,-1,sizeof(p3)); memset(p4,-1,sizeof(p4)); for (int i = 0; i < N; ++i) { while (!s.empty() && H[i] >= H[s.top()]) s.pop(); if (!s.empty()) p1[i][0] = p3[i][0] = s.top(); s.push(i); } while (!s.empty()) s.pop(); for (int i = N-1; i >= 0; --i) { while (!s.empty() && H[i] >= H[s.top()]) s.pop(); if (!s.empty()) p2[i][0] = p4[i][0] = s.top(); s.push(i); } for (int i = 0; i < N; ++i) if (p1[i][0] == -1 || (p1[i][0] != -1 && p2[i][0] != -1 && H[p1[i][0]] < H[p2[i][0]])) swap(p1[i][0], p2[i][0]); for (int k = 1; k < 20; ++k) for (int i = 0; i < N; ++i) { if (p1[i][k-1] == -1) p1[i][k] = -1; else p1[i][k] = p1[p1[i][k-1]][k-1]; if (p2[i][k-1] == -1) p2[i][k] = -1; else p2[i][k] = p2[p2[i][k-1]][k-1]; if (p3[i][k-1] == -1) p3[i][k] = -1; else p3[i][k] = p3[p3[i][k-1]][k-1]; if (p4[i][k-1] == -1) p4[i][k] = -1; else p4[i][k] = p4[p4[i][k-1]][k-1]; } } int minimum_jumps(int A, int B, int C, int D) { int x = C; for (int k = 19; k >= 0; --k) if (p3[x][k] != -1 && p3[x][k] > B) x = p3[x][k]; x = p3[x][0]; if (x >= A && x <= B && p4[x][0] >= C && p4[x][0] <= D) return 1; int y = B; for (int k = 19; k >= 0; --k) if (p4[y][k] != -1 && p4[y][k] < C) y = p4[y][k]; y = p4[y][0]; if (y < C || y > D) return -1; x = B; for (int k = 19; k >= 0; --k) if (p3[x][k] != -1 && X[p3[x][k]] <= X[y] && p3[x][k] >= A) x = p3[x][k]; int ans = 0; for (int k = 19; k >= 0; --k) if (p1[x][k] != -1 && X[p1[x][k]] <= X[y]) ans += (1<<k), x = p1[x][k]; int ans1 = 0, ans2 = 1, x1 = x, x2 = p1[x][0]; for (int k = 19; k >= 0; --k) if (p2[x1][k] != -1 && X[p2[x1][k]] <= X[y]) ans1 += (1<<k), x1 = p2[x1][k]; for (int k = 19; k >= 0; --k) if (p4[x2][k] != -1 && p4[x2][k] < C) ans2 += (1<<k), x2 = p4[x2][k]; ans2++, x2 = p4[x2][0]; if ((x1 < C || x1 > D) && (x2 < C || x2 > D)) return -1; else if (x1 < C || x1 > D) return ans+ans2; else if (x2 < C || x2 > D) return ans+ans1; else return ans+min(ans1,ans2); }
#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...