Problem p03771 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <vector>
static const int MAXN = 2e5 + 4;
static const int LOGN = 19;
template <typename T> inline void upd_max(T &var, const T val) {
  if (var < val)
    var = val;
}
template <typename T> inline void upd_min(T &var, const T val) {
  if (var > val)
    var = val;
}

int n, v_max, x[MAXN];
int k, v[LOGN];
int f_lf[1 << LOGN], f_rg[1 << LOGN];
int rg_with_lf[MAXN];

void calc_dp(int *f) {
  static int next[LOGN][MAXN];
  for (int i = 0; i < k; ++i) {
    for (int p = 0, q; p < n;) {
      for (q = p + 1; q < n && x[q] - x[q - 1] <= v[i]; ++q)
        ;
      for (; p < q; ++p)
        next[i][p] = q;
    }
  }

  for (int i = 0; i < (1 << k); ++i)
    f[i] = 0;
  std::vector<int> pc[LOGN];
  for (int i = 0; i < (1 << k); ++i)
    pc[__builtin_popcount(i)].push_back(i);
  for (int i = 0; i < k; ++i)
    for (int mask : pc[i]) {
      for (int j = 0; j < k; ++j)
        if (!(mask & (1 << j))) {
          upd_max(f[mask | (1 << j)], next[j][f[mask]]);
        }
    }
}

int main() {
  scanf("%d%d", &n, &v_max);
  for (int i = 0; i < n; ++i)
    scanf("%d", &x[i]);
  for (k = 0; v_max > 0; ++k, v_max /= 2)
    v[k] = v_max;
  v[k++] = 0;

  calc_dp(f_lf);
  std::reverse(x, x + n);
  for (int i = 0; i < n; ++i)
    x[i] = -x[i];
  calc_dp(f_rg);
  std::reverse(x, x + n);
  for (int i = 0; i < n; ++i)
    x[i] = -x[i];
  for (int i = 0; i < (1 << k); ++i)
    f_rg[i] = n - f_rg[i];

  for (int i = 0; i <= n; ++i)
    rg_with_lf[i] = MAXN;
  for (int i = 0; i < (1 << (k - 1)); ++i)
    upd_min(rg_with_lf[f_lf[i << 1]], f_rg[(((1 << (k - 1)) - 1) ^ i) << 1]);
  for (int i = n - 1; i >= 0; --i)
    upd_min(rg_with_lf[i], rg_with_lf[i + 1]);

  for (int i = 0, j; i < n;) {
    for (j = i + 1; j < n && x[j] - x[j - 1] <= v[0]; ++j)
      ;
    bool possible = (rg_with_lf[i] <= j);
    for (; i < j; ++i)
      puts(possible ? "Possible" : "Impossible");
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
static const int MAXN = 2e5 + 4;
static const int LOGN = 20;
template <typename T> inline void upd_max(T &var, const T val) {
  if (var < val)
    var = val;
}
template <typename T> inline void upd_min(T &var, const T val) {
  if (var > val)
    var = val;
}

int n, v_max, x[MAXN];
int k, v[LOGN];
int f_lf[1 << LOGN], f_rg[1 << LOGN];
int rg_with_lf[MAXN];

void calc_dp(int *f) {
  static int next[LOGN][MAXN];
  for (int i = 0; i < k; ++i) {
    for (int p = 0, q; p < n;) {
      for (q = p + 1; q < n && x[q] - x[q - 1] <= v[i]; ++q)
        ;
      for (; p < q; ++p)
        next[i][p] = q;
    }
  }

  for (int i = 0; i < (1 << k); ++i)
    f[i] = 0;
  std::vector<int> pc[LOGN];
  for (int i = 0; i < (1 << k); ++i)
    pc[__builtin_popcount(i)].push_back(i);
  for (int i = 0; i < k; ++i)
    for (int mask : pc[i]) {
      for (int j = 0; j < k; ++j)
        if (!(mask & (1 << j))) {
          upd_max(f[mask | (1 << j)], next[j][f[mask]]);
        }
    }
}

int main() {
  scanf("%d%d", &n, &v_max);
  for (int i = 0; i < n; ++i)
    scanf("%d", &x[i]);
  for (k = 0; v_max > 0; ++k, v_max /= 2)
    v[k] = v_max;
  v[k++] = 0;

  calc_dp(f_lf);
  std::reverse(x, x + n);
  for (int i = 0; i < n; ++i)
    x[i] = -x[i];
  calc_dp(f_rg);
  std::reverse(x, x + n);
  for (int i = 0; i < n; ++i)
    x[i] = -x[i];
  for (int i = 0; i < (1 << k); ++i)
    f_rg[i] = n - f_rg[i];

  for (int i = 0; i <= n; ++i)
    rg_with_lf[i] = MAXN;
  for (int i = 0; i < (1 << (k - 1)); ++i)
    upd_min(rg_with_lf[f_lf[i << 1]], f_rg[(((1 << (k - 1)) - 1) ^ i) << 1]);
  for (int i = n - 1; i >= 0; --i)
    upd_min(rg_with_lf[i], rg_with_lf[i + 1]);

  for (int i = 0, j; i < n;) {
    for (j = i + 1; j < n && x[j] - x[j - 1] <= v[0]; ++j)
      ;
    bool possible = (rg_with_lf[i] <= j);
    for (; i < j; ++i)
      puts(possible ? "Possible" : "Impossible");
  }

  return 0;
}