Submission #1054045

#TimeUsernameProblemLanguageResultExecution timeMemory
1054045KerimRainforest Jumps (APIO21_jumps)C++17
Compilation error
0 ms0 KiB
// #include "jumps.h"

#include <bits/stdc++.h>
using namespace std;

constexpr int kMaxLogN = 20;

int N;
vector<int> H;
vector<vector<int>> L, R;
vector<vector<int>> higher;

void init(int _N, std::vector<int> _H) {
  N = _N;
  H = _H;

  H.insert(H.begin(), INT_MAX);
  H.insert(H.end(), INT_MAX);
  N += 2;
  L = R = higher = vector<vector<int>>(kMaxLogN, vector<int>(N));

  stack<int> decreasing_height;
  for (int i = 0; i < N; ++i) {
    while (!decreasing_height.empty() && H[decreasing_height.top()] <= H[i]) {
      decreasing_height.pop();
    }
    L[0][i] = decreasing_height.empty() ? i : decreasing_height.top();
    decreasing_height.push(i);
  }

  while (!decreasing_height.empty()) {
    decreasing_height.pop();
  }
  for (int i = N - 1; i >= 0; --i) {
    while (!decreasing_height.empty() && H[decreasing_height.top()] <= H[i]) {
      decreasing_height.pop();
    }
    R[0][i] = decreasing_height.empty() ? i : decreasing_height.top();
    decreasing_height.push(i);
  }

  for (int i = 0; i < N; ++i) {
    higher[0][i] = H[L[0][i]] > H[R[0][i]] ? L[0][i] : R[0][i];
  }

  for (int j = 1; j < kMaxLogN; ++j) {
    for (int i = 0; i < N; ++i) {
      L[j][i] = L[j - 1][L[j - 1][i]];
      R[j][i] = R[j - 1][R[j - 1][i]];
      higher[j][i] = higher[j - 1][higher[j - 1][i]];
    }
  }
}

int minimum_jumps(int s, int e) {
  int jumps = 0;
  for (int j = kMaxLogN - 1; j >= 0; --j) {
    if (H[higher[j][s]] <= H[e]) {
      s = higher[j][s];
      jumps |= (1 << j);
    }
  }
  if (H[L[0][s]] > H[e]) {
    for (int j = kMaxLogN - 1; j >= 0; --j) {
      if (H[R[j][s]] <= H[e]) {
        s = R[j][s];
        jumps += (1 << j);
      }
    }
  } else {
    for (int j = kMaxLogN - 1; j >= 0; --j) {
      if (H[L[j][s]] <= H[e]) {
        s = L[j][s];
        jumps += (1 << j);
      }
    }
  }
  return s == e ? jumps : -1;
}

int minimum_jumps(int A, int B, int C, int D) {
  ++A; ++B; ++C; ++D;

  int s = B;
  int e = C;
  for (int j = kMaxLogN - 1; j >= 0; --j) {
    if (A <= L[j][s] && H[L[j][s]] < H[e]) {
      s = L[j][s];
    }
  }
  return minimum_jumps(s, e);
}

int main(){#include "jumps.h"

#include <bits/stdc++.h>
using namespace std;

constexpr int kMaxLogN = 20;

int N;
vector<int> H;
vector<vector<int>> L, R;
vector<vector<int>> higher;

void init(int _N, std::vector<int> _H) {
  N = _N;
  H = _H;

  H.insert(H.begin(), INT_MAX);
  H.insert(H.end(), INT_MAX);
  N += 2;
  L = R = higher = vector<vector<int>>(kMaxLogN, vector<int>(N));

  stack<int> decreasing_height;
  for (int i = 0; i < N; ++i) {
    while (!decreasing_height.empty() && H[decreasing_height.top()] <= H[i]) {
      decreasing_height.pop();
    }
    L[0][i] = decreasing_height.empty() ? i : decreasing_height.top();
    decreasing_height.push(i);
  }

  while (!decreasing_height.empty()) {
    decreasing_height.pop();
  }
  for (int i = N - 1; i >= 0; --i) {
    while (!decreasing_height.empty() && H[decreasing_height.top()] <= H[i]) {
      decreasing_height.pop();
    }
    R[0][i] = decreasing_height.empty() ? i : decreasing_height.top();
    decreasing_height.push(i);
  }

  for (int i = 0; i < N; ++i) {
    higher[0][i] = H[L[0][i]] > H[R[0][i]] ? L[0][i] : R[0][i];
  }

  for (int j = 1; j < kMaxLogN; ++j) {
    for (int i = 0; i < N; ++i) {
      L[j][i] = L[j - 1][L[j - 1][i]];
      R[j][i] = R[j - 1][R[j - 1][i]];
      higher[j][i] = higher[j - 1][higher[j - 1][i]];
    }
  }
}

int minimum_jumps(int s, int e) {
  int jumps = 0;
  for (int j = kMaxLogN - 1; j >= 0; --j) {
    if (H[higher[j][s]] <= H[e]) {
      s = higher[j][s];
      jumps |= (1 << j);
    }
  }
  if (H[L[0][s]] > H[e]) {
    for (int j = kMaxLogN - 1; j >= 0; --j) {
      if (H[R[j][s]] <= H[e]) {
        s = R[j][s];
        jumps += (1 << j);
      }
    }
  } else {
    for (int j = kMaxLogN - 1; j >= 0; --j) {
      if (H[L[j][s]] <= H[e]) {
        s = L[j][s];
        jumps += (1 << j);
      }
    }
  }
  return s == e ? jumps : -1;
}

int minimum_jumps(int A, int B, int C, int D) {
  ++A; ++B; ++C; ++D;

  int s = B;
  int e = C;
  for (int j = kMaxLogN - 1; j >= 0; --j) {
    if (A <= L[j][s] && H[L[j][s]] < H[e]) {
      s = L[j][s];
    }
  }
  return minimum_jumps(s, e);
}

// int main(){
//     init(3, {2, 1, 3});
//     printf("%d\n", minimum_jumps(0, 1, 2, 2));
// }
    init(3, {2, 1, 3});
    printf("%d\n", minimum_jumps(0, 1, 2, 2));
}

Compilation message (stderr)

jumps.cpp:94:12: error: stray '#' in program
   94 | int main(){#include "jumps.h"
      |            ^
jumps.cpp: In function 'int main()':
jumps.cpp:94:13: error: 'include' was not declared in this scope
   94 | int main(){#include "jumps.h"
      |             ^~~~~~~
jumps.cpp:106:40: error: a function-definition is not allowed here before '{' token
  106 | void init(int _N, std::vector<int> _H) {
      |                                        ^
jumps.cpp:148:33: error: a function-definition is not allowed here before '{' token
  148 | int minimum_jumps(int s, int e) {
      |                                 ^
jumps.cpp:174:47: error: a function-definition is not allowed here before '{' token
  174 | int minimum_jumps(int A, int B, int C, int D) {
      |                                               ^
jumps.cpp:99:15: warning: unused variable 'kMaxLogN' [-Wunused-variable]
   99 | constexpr int kMaxLogN = 20;
      |               ^~~~~~~~
jumps.cpp:101:5: warning: unused variable 'N' [-Wunused-variable]
  101 | int N;
      |     ^