제출 #561328

#제출 시각아이디문제언어결과실행 시간메모리
561328piOOE밀림 점프 (APIO21_jumps)C++17
4 / 100
806 ms31800 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; #define all(x) begin(x), end(x) const int logn = 18, N = 200001; int h[N], jump[logn][N], R[N], L[N], spt[logn][N]; bool okk = true; int Min(int i, int j) { return h[i] < h[j] ? i : j; } int Max(int i, int j) { return h[i] > h[j] ? i : j; } int get_max(int l, int r) { if (r <= l) return -1; int sz = __lg(r - l); return Max(spt[sz][l], spt[sz][r - (1 << sz)]); } void init(int n, vector<int> H) { for (int i = 0; i < n; ++i) { h[i] = H[i]; okk &= (H[i] == i + 1); } vector<int> st; for (int i = 0; i < n; ++i) { while (!st.empty() && h[st.back()] < h[i]) { R[st.back()] = i; st.pop_back(); } if (st.empty()) { L[i] = -1; } else { L[i] = st.back(); } st.push_back(i); } for (int i : st) { R[i] = n; } st.clear(); for (int i = 0; i < n; ++i) { jump[0][i] = R[i]; spt[0][i] = i; } jump[0][n] = n; for (int j = 1; j < logn; ++j) { for (int i = 0; i <= n; ++i) { jump[j][i] = jump[j - 1][jump[j - 1][i]]; if (i + (1 << j) <= n) { spt[j][i] = Max(spt[j - 1][i], spt[j - 1][i + (1 << (j - 1))]); } } } } int minimum_jumps(int A, int B, int C, int D) { if (okk) { return C - B; } ++D; int mxcd = get_max(C, D); int lx = A - 1, rx = C; while (lx + 1 < rx) { int m = (lx + rx) >> 1; if (h[get_max(m, C)] < h[mxcd]) rx = m; else lx = m; } if (rx > B) { return -1; } ++B; int i = get_max(rx, B); auto get = [](int i, int lx, int rx) { int ans = 0; for (int j = logn - 1; j > -1; --j) { if (jump[j][i] < lx) { ans += (1 << j); i = jump[j][i]; } } if (jump[0][i] >= rx) { return -1; } return ans + 1; }; int ans = INT_MAX; int added = 0; while (i != -1) { int nw = get(i, C, D); if (nw != -1) { ans = min(ans, added + nw); } ++added; i = L[i]; } return (ans == INT_MAX ? -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...