Problem p03771 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 2e5 + 10, inf = 0x3f3f3f3f;
int tmp, Max[20][maxn], Min[20][maxn], a[maxn], f[maxn], g[maxn], R[maxn];
int main() {
  int n = read(), V = read();
  REP(i, 1, n) a[i] = read();
  tmp = -1;
  memset(Min, inf, sizeof(Min));
  while (1) {
    ++tmp;
    int lst = 1;
    REP(i, 2, n) if (a[i] - a[i - 1] > V) {
      chkmax(Max[tmp][lst], i - 1);
      chkmin(Min[tmp][i - 1], lst);
      lst = i;
    }
    chkmax(Max[tmp][lst], n);
    chkmin(Min[tmp][n], lst);
    REP(i, 1, n) chkmax(Max[tmp][i], Max[tmp][i - 1]);
    Max[tmp][n + 1] = n;
    DREP(i, n, 1) chkmin(Min[tmp][i], Min[tmp][i + 1]);
    Min[tmp][0] = 1;
    if (V == 0)
      break;
    V /= 2;
  }
  memset(g, inf, sizeof(g));
  f[0] = 0, g[0] = n + 1;
  REP(i, 0, (1 << tmp) - 1)
  REP(j, 1, tmp) if ((i & (1 << (j - 1))) == 0) {
    chkmax(f[i | (1 << (j - 1))], Max[j][f[i] + 1]);
    chkmin(g[i | (1 << (j - 1))], Min[j][g[i] - 1]);
  }
  memset(R, inf, sizeof(R));
  REP(i, 0, (1 << tmp) - 1) {
    if (f[i] >= g[i ^ iend] - 1) {
      REP(j, 1, n) printf("Possible\n");
      return 0;
    }
    chkmin(R[f[i] + 1], g[i ^ iend] - 1);
  }
  DREP(i, n, 1) chkmin(R[i], R[i + 1]);
  REP(i, 1, n)
  printf("%s\n", (R[Min[0][i]] <= Max[0][i]) ? "Possible" : "Impossible");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 2e5 + 10, inf = 0x3f3f3f3f;
int tmp, Max[20][maxn], Min[20][maxn], a[maxn], f[1 << 20], g[1 << 20], R[maxn];
int main() {
  int n = read(), V = read();
  REP(i, 1, n) a[i] = read();
  tmp = -1;
  memset(Min, inf, sizeof(Min));
  while (1) {
    ++tmp;
    int lst = 1;
    REP(i, 2, n) if (a[i] - a[i - 1] > V) {
      chkmax(Max[tmp][lst], i - 1);
      chkmin(Min[tmp][i - 1], lst);
      lst = i;
    }
    chkmax(Max[tmp][lst], n);
    chkmin(Min[tmp][n], lst);
    REP(i, 1, n) chkmax(Max[tmp][i], Max[tmp][i - 1]);
    Max[tmp][n + 1] = n;
    DREP(i, n, 1) chkmin(Min[tmp][i], Min[tmp][i + 1]);
    Min[tmp][0] = 1;
    if (V == 0)
      break;
    V /= 2;
  }
  memset(g, inf, sizeof(g));
  f[0] = 0, g[0] = n + 1;
  REP(i, 0, (1 << tmp) - 1)
  REP(j, 1, tmp) if ((i & (1 << (j - 1))) == 0) {
    chkmax(f[i | (1 << (j - 1))], Max[j][f[i] + 1]);
    chkmin(g[i | (1 << (j - 1))], Min[j][g[i] - 1]);
  }
  memset(R, inf, sizeof(R));
  REP(i, 0, (1 << tmp) - 1) {
    if (f[i] >= g[i ^ iend] - 1) {
      REP(j, 1, n) printf("Possible\n");
      return 0;
    }
    chkmin(R[f[i] + 1], g[i ^ iend] - 1);
  }
  DREP(i, n, 1) chkmin(R[i], R[i + 1]);
  REP(i, 1, n)
  printf("%s\n", (R[Min[0][i]] <= Max[0][i]) ? "Possible" : "Impossible");
  return 0;
}