Submission #716164

# Submission time Handle Problem Language Result Execution time Memory
716164 2023-03-29T07:02:52 Z schiftyfive4 Rainforest Jumps (APIO21_jumps) C++14
48 / 100
1823 ms 196736 KB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) "MJ >> LAMELO"
#endif

template <typename T, class F = function<T(const T&, const T&)>>
struct sparse_table {
  int n;
  vector<vector<T>> mat;
  F func;
  sparse_table() {}
  sparse_table(const vector<T>& a, const F& f) : func(f) {
    n = static_cast<int>(a.size());
    int max_log = 32 - __builtin_clz(n);
    mat.resize(max_log);
    mat[0] = a;
    for (int j = 1; j < max_log; j++) {
      mat[j].resize(n - (1 << j) + 1);
      for (int i = 0; i <= n - (1 << j); i++) {
        mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
      }
    }
  }
  T get(int L, int R) const {
    assert(0 <= L && L <= R && R <= n - 1);
    int lg = 32 - __builtin_clz(R - L + 1) - 1;
    return func(mat[lg][L], mat[lg][R - (1 << lg) + 1]);
  }
};

const int lg = 20;
vector<int> h;
vector<vector<int>> g;
vector<vector<int>> go_low;
vector<vector<int>> go_high;
vector<vector<int>> go_right;
sparse_table<int> st;
int n;

void init(int N, vector<int> H) {
  n = N;
  h = H;
  h.push_back(1e9);
  g.resize(n + 1);
  vector<int> t;
  for (int i = 0; i < n; i++) {
    while (!t.empty() && h[i] > h[t.back()]) {
      t.pop_back();
    }
    if (!t.empty()) {
      g[i].push_back(t.back());
    }
    t.push_back(i);
  }
  go_right = vector<vector<int>>(n + 1, vector<int>(lg));
  t.clear();
  for (int i = n - 1; i >= 0; i--) {
    go_right[i][0] = i;
    while (!t.empty() && h[i] > h[t.back()]) {
      t.pop_back();
    }
    if (!t.empty()) {
      g[i].push_back(t.back());
      go_right[i][0] = t.back();
    }
    t.push_back(i);
  }
  for (int i = 0; i < n; i++) {
    if (g[i].size() == 2 && h[g[i][0]] > h[g[i][1]]) {
      swap(g[i][0], g[i][1]);
    }
  }
  go_low = vector<vector<int>>(n + 1, vector<int>(lg));
  go_high = vector<vector<int>>(n + 1, vector<int>(lg));
  for (int i = 0; i <= n; i++) {
    go_low[i][0] = go_high[i][0] = n;
    if (g[i].size() > 0) {
      go_low[i][0] = g[i][0];
    }
    if (g[i].size() > 1){
      go_high[i][0] = g[i][1];
    }
  }
  for (int b = 1; b < lg; b++) {
    for (int i = 0; i <= n; i++) {
      go_low[i][b] = go_low[go_low[i][b - 1]][b - 1];
      go_high[i][b] = go_high[go_high[i][b - 1]][b - 1];
      go_right[i][b] = go_right[go_right[i][b - 1]][b - 1];
    }
  }
  vector<int> p(n);
  iota(p.begin(), p.end(), 0);
  st = sparse_table<int>(p, [&](int i, int j) { return h[i] > h[j] ? i : j; });
}

int minimum_jumps(int a, int b, int c, int d) {
  int x = st.get(c, d);
  if (h[st.get(b, c - 1)] > h[x]) {
    return -1;
  }
  int l = a;
  int r = b;
  while (l < r) {
    int mid = (l + r) >> 1;
    int pos = st.get(mid, b);
    if (h[pos] > h[x]) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  int s = st.get(l, b);
  l = c;
  r = d;
  while (l < r) {
    int mid = (l + r) >> 1;
    int pos = st.get(c, mid);
    if (h[pos] > h[s]) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  int t = l;
  int ans = 0;
  for (int b = lg - 1; b >= 0; b--) {
    int ns = go_high[s][b];
    if (h[ns] < h[t]) {
      s = ns;
      ans += (1 << b);
    }
  }
  if (c <= go_right[s][0] && go_right[s][0] <= d) {
    return ans + 1;
  }
  if (h[go_high[s][0]] < h[x]) {
    assert(go_high[s][0] < s);
    s = go_high[s][0];
    // assert(s < c);
    // s = go_low[s][0];
    // assert(c <= s && s <= d);
    return ans + 2;
  }
  assert(s < c);
  assert(h[s] < h[t]);
  assert(h[go_high[s][0]] > h[x]);
  for (int b = lg - 1; b >= 0; b--) {
    int ns = go_low[s][b];
    if (h[ns] <= h[t]) {
      s = ns;
      ans += (1 << b);
    }
  }
  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 330 ms 78336 KB Output is correct
4 Correct 1823 ms 98672 KB Output is correct
5 Correct 1025 ms 49380 KB Output is correct
6 Correct 1698 ms 98676 KB Output is correct
7 Correct 1049 ms 67148 KB Output is correct
8 Correct 1762 ms 98620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 2 ms 208 KB Output is correct
6 Runtime error 2 ms 592 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 2 ms 208 KB Output is correct
6 Runtime error 2 ms 592 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 207 ms 78552 KB Output is correct
6 Correct 253 ms 97792 KB Output is correct
7 Correct 118 ms 49820 KB Output is correct
8 Correct 254 ms 97940 KB Output is correct
9 Correct 34 ms 14544 KB Output is correct
10 Correct 237 ms 97900 KB Output is correct
11 Correct 212 ms 98664 KB Output is correct
12 Correct 214 ms 98452 KB Output is correct
13 Correct 219 ms 98336 KB Output is correct
14 Runtime error 318 ms 196736 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 323 ms 44440 KB Output is correct
5 Correct 1139 ms 97880 KB Output is correct
6 Correct 809 ms 15972 KB Output is correct
7 Correct 1008 ms 97880 KB Output is correct
8 Correct 605 ms 33416 KB Output is correct
9 Correct 1151 ms 97880 KB Output is correct
10 Correct 1523 ms 98684 KB Output is correct
11 Correct 1376 ms 98600 KB Output is correct
12 Correct 1298 ms 98484 KB Output is correct
13 Correct 1200 ms 97880 KB Output is correct
14 Correct 1396 ms 98276 KB Output is correct
15 Correct 1176 ms 98616 KB Output is correct
16 Correct 1240 ms 98588 KB Output is correct
17 Correct 0 ms 208 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 2 ms 208 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 23 ms 1204 KB Output is correct
28 Correct 19 ms 1224 KB Output is correct
29 Correct 23 ms 1152 KB Output is correct
30 Correct 20 ms 1216 KB Output is correct
31 Correct 20 ms 1104 KB Output is correct
32 Correct 1 ms 208 KB Output is correct
33 Correct 130 ms 56340 KB Output is correct
34 Correct 223 ms 97908 KB Output is correct
35 Correct 267 ms 98584 KB Output is correct
36 Correct 254 ms 97960 KB Output is correct
37 Correct 260 ms 98244 KB Output is correct
38 Correct 227 ms 98660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 323 ms 44440 KB Output is correct
5 Correct 1139 ms 97880 KB Output is correct
6 Correct 809 ms 15972 KB Output is correct
7 Correct 1008 ms 97880 KB Output is correct
8 Correct 605 ms 33416 KB Output is correct
9 Correct 1151 ms 97880 KB Output is correct
10 Correct 1523 ms 98684 KB Output is correct
11 Correct 1376 ms 98600 KB Output is correct
12 Correct 1298 ms 98484 KB Output is correct
13 Correct 1200 ms 97880 KB Output is correct
14 Correct 1396 ms 98276 KB Output is correct
15 Correct 1176 ms 98616 KB Output is correct
16 Correct 1240 ms 98588 KB Output is correct
17 Correct 0 ms 208 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 2 ms 208 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 23 ms 1204 KB Output is correct
28 Correct 19 ms 1224 KB Output is correct
29 Correct 23 ms 1152 KB Output is correct
30 Correct 20 ms 1216 KB Output is correct
31 Correct 20 ms 1104 KB Output is correct
32 Correct 1 ms 208 KB Output is correct
33 Correct 130 ms 56340 KB Output is correct
34 Correct 223 ms 97908 KB Output is correct
35 Correct 267 ms 98584 KB Output is correct
36 Correct 254 ms 97960 KB Output is correct
37 Correct 260 ms 98244 KB Output is correct
38 Correct 227 ms 98660 KB Output is correct
39 Correct 1 ms 208 KB Output is correct
40 Correct 0 ms 256 KB Output is correct
41 Correct 1 ms 256 KB Output is correct
42 Correct 322 ms 44448 KB Output is correct
43 Correct 1139 ms 97976 KB Output is correct
44 Correct 677 ms 16096 KB Output is correct
45 Correct 1153 ms 97888 KB Output is correct
46 Correct 659 ms 33384 KB Output is correct
47 Correct 1132 ms 97876 KB Output is correct
48 Correct 1394 ms 98672 KB Output is correct
49 Correct 1158 ms 98600 KB Output is correct
50 Correct 1337 ms 98544 KB Output is correct
51 Correct 1029 ms 97988 KB Output is correct
52 Correct 1224 ms 98188 KB Output is correct
53 Correct 1165 ms 98564 KB Output is correct
54 Correct 1234 ms 98576 KB Output is correct
55 Correct 0 ms 208 KB Output is correct
56 Correct 230 ms 97396 KB Output is correct
57 Correct 1064 ms 97880 KB Output is correct
58 Correct 386 ms 17056 KB Output is correct
59 Correct 806 ms 97876 KB Output is correct
60 Correct 396 ms 34616 KB Output is correct
61 Correct 1166 ms 97900 KB Output is correct
62 Correct 1450 ms 98692 KB Output is correct
63 Correct 1288 ms 98472 KB Output is correct
64 Correct 1254 ms 98312 KB Output is correct
65 Correct 966 ms 97848 KB Output is correct
66 Correct 1175 ms 98188 KB Output is correct
67 Correct 789 ms 98568 KB Output is correct
68 Correct 1028 ms 98668 KB Output is correct
69 Correct 0 ms 208 KB Output is correct
70 Correct 1 ms 208 KB Output is correct
71 Correct 2 ms 336 KB Output is correct
72 Correct 2 ms 336 KB Output is correct
73 Correct 2 ms 336 KB Output is correct
74 Correct 2 ms 336 KB Output is correct
75 Correct 2 ms 336 KB Output is correct
76 Correct 0 ms 208 KB Output is correct
77 Correct 0 ms 208 KB Output is correct
78 Correct 2 ms 208 KB Output is correct
79 Correct 2 ms 336 KB Output is correct
80 Correct 2 ms 336 KB Output is correct
81 Correct 2 ms 336 KB Output is correct
82 Correct 3 ms 336 KB Output is correct
83 Correct 3 ms 336 KB Output is correct
84 Correct 0 ms 336 KB Output is correct
85 Correct 3 ms 336 KB Output is correct
86 Correct 18 ms 1104 KB Output is correct
87 Correct 17 ms 1104 KB Output is correct
88 Correct 21 ms 1104 KB Output is correct
89 Correct 17 ms 1212 KB Output is correct
90 Correct 21 ms 1232 KB Output is correct
91 Correct 1 ms 336 KB Output is correct
92 Correct 1 ms 592 KB Output is correct
93 Correct 22 ms 1200 KB Output is correct
94 Correct 16 ms 1228 KB Output is correct
95 Correct 10 ms 1104 KB Output is correct
96 Correct 18 ms 1224 KB Output is correct
97 Correct 19 ms 1104 KB Output is correct
98 Correct 0 ms 208 KB Output is correct
99 Correct 216 ms 97684 KB Output is correct
100 Correct 208 ms 97884 KB Output is correct
101 Correct 201 ms 98440 KB Output is correct
102 Correct 211 ms 97820 KB Output is correct
103 Correct 237 ms 98176 KB Output is correct
104 Correct 208 ms 98604 KB Output is correct
105 Correct 0 ms 208 KB Output is correct
106 Correct 118 ms 56348 KB Output is correct
107 Correct 214 ms 97944 KB Output is correct
108 Correct 197 ms 98572 KB Output is correct
109 Correct 208 ms 97876 KB Output is correct
110 Correct 243 ms 98396 KB Output is correct
111 Correct 196 ms 98624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 330 ms 78336 KB Output is correct
4 Correct 1823 ms 98672 KB Output is correct
5 Correct 1025 ms 49380 KB Output is correct
6 Correct 1698 ms 98676 KB Output is correct
7 Correct 1049 ms 67148 KB Output is correct
8 Correct 1762 ms 98620 KB Output is correct
9 Correct 1 ms 216 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Runtime error 2 ms 592 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -