Submission #857384

#TimeUsernameProblemLanguageResultExecution timeMemory
857384chanhchuong123Rainforest Jumps (APIO21_jumps)C++14
4 / 100
959 ms45880 KiB
#include <bits/stdc++.h> //#include "jumps.h" using namespace std; #define task "" const int INF = 1e9 + 7; const int MAX = 200020; stack<int> st; int nTree, nQuery, H[MAX]; int lf[18][MAX], rg[18][MAX], dp[18][MAX]; int combine(int x, int y) { return H[x] >= H[y] ? x : y; } int getMax(int l, int r) { int k = 31 - __builtin_clz(r - l + 1); return combine(dp[k][l], dp[k][r - (1 << k) + 1]); } int jumpRg(int u, int C, int D) { int res = 1; for (int j = 17; j >= 0; --j) if (rg[j][u] != -1) { if (rg[j][u] < C) { u = rg[j][u]; res += 1 << j; } } u = rg[0][u]; if (C <= u && u <= D) return res; return INF; } int jumpRg(int u, int f, int C, int D) { return jumpRg(u, f, f) + jumpRg(f, C, D); } int jumpLf(int u, int v) { int res = 1; for (int j = 17; j >= 0; --j) if (lf[j][u] != -1) { if (lf[j][u] > v) { u = lf[j][u]; res += 1 << j; } } u = lf[0][u]; if (u == v) return res; return INF; } int minimum_jumps(int A, int B, int C, int D) { if (B + 1 == C) { int ans = jumpRg(B, C, D); if (ans == INF) ans = -1; return ans; } int f = getMax(B + 1, C - 1), ans = jumpRg(B, C, D); { int l = A - 1, r = B; while (r - l > 1) { int mid = l + r >> 1; if (H[getMax(mid, B)] < H[f]) r = mid; else l = mid; } int u = getMax(r, B); if (H[u] < H[f]) ans = min(ans, jumpRg(u, f, C, D)); if (A <= r - 1) ans = min(ans, jumpRg(r - 1, C, D)); } if (A - 1 >= 1) { int l = 0, r = A - 1; while (r - l > 1) { int mid = l + r >> 1; if (H[getMax(mid, A - 1)] < H[f]) r = mid; else l = mid; } int g = getMax(r, A - 1); if (H[g] < H[f]) { int l = A, r = B + 1; while (r - l > 1) { int mid = l + r >> 1; if (H[getMax(A, mid)] < H[g]) l = mid; else r = mid; } int u = getMax(A, l); if (H[u] < H[g]) { int res = jumpLf(u, g) + jumpRg(g, C, D); ans = min(ans, res); } } if (g > 1) { --g; if (H[g] > H[f]) { int l = A, r = B + 1; while (r - l > 1) { int mid = l + r >> 1; if (H[getMax(A, mid)] < H[g]) l = mid; else r = mid; } int u = getMax(A, l); if (H[u] < H[g]) { ans = min(ans, jumpLf(u, g) + jumpRg(g, C, D)); } } } } if (ans == INF) ans = -1; return ans; } void init(int N, vector<int> _H) { nTree = N; for (int i = 0; i < nTree; ++i) { H[i] = _H[i]; dp[0][i] = H[i]; } st = stack<int> {}; for (int i = 0; i <= nTree - 1; ++i) { while (st.size() && H[st.top()] < H[i]) st.pop(); lf[0][i] = st.size() ? st.top() : -1; st.push(i); } st = stack<int> {}; for (int i = nTree - 1; i >= 0; --i) { while (st.size() && H[st.top()] < H[i]) st.pop(); rg[0][i] = st.size() ? st.top() : -1; st.push(i); } for (int j = 1; j <= 17; ++j) { for (int i = 0; i < nTree; ++i) { if (lf[j - 1][i] != -1) lf[j][i] = lf[j - 1][lf[j - 1][i]]; else lf[j][i] = -1; if (rg[j - 1][i] != -1) rg[j][i] = rg[j - 1][rg[j - 1][i]]; else rg[j][i] = -1; } } for (int j = 1; (1 << j) <= nTree; ++j) { for (int i = 0; i + (1 << j) <= nTree; ++i) dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]); } } // //int main() { // ios_base::sync_with_stdio(false); cin.tie(nullptr); // // if (fopen(task".inp", "r")) { // freopen(task".inp", "r", stdin); // freopen(task".out", "w", stdout); // } // // cin >> nTree >> nQuery; // for (int i = 0; i < nTree; ++i) { // cin >> H[i]; dp[0][i] = i; // } // // st = stack<int> {}; // for (int i = 0; i <= nTree - 1; ++i) { // while (st.size() && H[st.top()] < H[i]) st.pop(); // lf[0][i] = st.size() ? st.top() : -1; st.push(i); // } // st = stack<int> {}; // for (int i = nTree - 1; i >= 0; --i) { // while (st.size() && H[st.top()] < H[i]) st.pop(); // rg[0][i] = st.size() ? st.top() : -1; st.push(i); // } // // for (int j = 1; j <= 17; ++j) { // for (int i = 0; i < nTree; ++i) { // if (lf[j - 1][i] != -1) lf[j][i] = lf[j - 1][lf[j - 1][i]]; else lf[j][i] = -1; // if (rg[j - 1][i] != -1) rg[j][i] = rg[j - 1][rg[j - 1][i]]; else rg[j][i] = -1; // } // } // for (int j = 1; (1 << j) <= nTree; ++j) { // for (int i = 0; i + (1 << j) <= nTree; ++i) // dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]); // } // while (nQuery--) { // int A, B, C, D; cin >> A >> B >> C >> D; // cout << minimum_jumps(A, B, C, D) << '\n'; // } // return 0; //} /* 7 3 3 2 1 6 4 5 7 4 4 6 6 1 3 5 6 0 1 2 2 */

Compilation message (stderr)

jumps.cpp: In function 'int jumpRg(int, int, int)':
jumps.cpp:27:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   27 |     if (C <= u && u <= D) return res; return INF;
      |     ^~
jumps.cpp:27:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   27 |     if (C <= u && u <= D) return res; return INF;
      |                                       ^~~~~~
jumps.cpp: In function 'int jumpLf(int, int)':
jumps.cpp:43:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   43 |     if (u == v) return res; return INF;
      |     ^~
jumps.cpp:43:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   43 |     if (u == v) return res; return INF;
      |                             ^~~~~~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:56:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   56 |             int mid = l + r >> 1;
      |                       ~~^~~
jumps.cpp:66:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |             int mid = l + r >> 1;
      |                       ~~^~~
jumps.cpp:73:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |                 int mid = l + r >> 1;
      |                           ~~^~~
jumps.cpp:87:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |                     int mid = l + r >> 1;
      |                               ~~^~~
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:126:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  126 |             dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << j - 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...