Problem p03771 (C++)

Buggy submission (status: Runtime Error):
// AT 2365
// DeP
#include <algorithm>
#include <cctype>
#include <cstdio>
using namespace std;

namespace IO {
const int MAXSIZE = 1 << 18 | 1;
char buf[MAXSIZE], *p1, *p2;

inline int Gc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <class T> void read(T &x) {
  x = 0;
  int f = 0, ch = Gc();
  while (!isdigit(ch))
    f |= ch == '-', ch = Gc();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = Gc();
  if (f)
    x = -x;
}
} // namespace IO
using IO::read;

typedef pair<int, int> Pii;
const int MAXN = 2e5 + 5, LOG = 21, INF = 0x3f3f3f3f;

int n, V;
int X[MAXN], f[MAXN], g[MAXN];
int L[LOG][MAXN], R[LOG][MAXN];
bool Ans[MAXN];
Pii segs[MAXN];

int main() {
  // input
  read(n);
  read(V);
  for (int i = 1; i <= n; ++i)
    read(X[i]);
  X[0] = -INF;
  X[n + 1] = INF;
  // solve
  int log = 0;
  for (int v = V; v >= 0; v /= 2, ++log) {
    R[log][n + 1] = n + 1;
    for (int i = 1; i <= n; ++i)
      L[log][i] = X[i] - X[i - 1] <= v ? L[log][i - 1] : i;
    for (int i = n; i >= 1; --i)
      R[log][i] = X[i + 1] - X[i] <= v ? R[log][i + 1] : i;
    if (!v) {
      ++log;
      break;
    }
  }
  for (int i = 1; i <= n; ++i)
    segs[i] = make_pair(L[0][i], R[0][i]);
  int tot = unique(segs + 1, segs + 1 + n) - segs - 1;
  if (tot > log) {
    for (int i = 1; i <= n; ++i)
      puts("Impossible");
    return 0;
  }
  for (int S = 0; S < (1 << log); ++S)
    f[S] = n + 1;
  for (int S = 0; S < (1 << log); S += 2) {
    for (int i = 0; i < log; ++i)
      if ((S >> i) & 1) {
        f[S] = min(f[S], L[i][f[S ^ (1 << i)] - 1]);
        g[S] = max(g[S], R[i][g[S ^ (1 << i)] + 1]);
      }
  }
  for (int i = 1; i <= tot; ++i)
    for (int S = 0; S < (1 << log); S += 2)
      if (g[S] >= segs[i].first - 1 &&
          segs[i].second + 1 >= f[((1 << log) - 1) - (S + 1)]) {
        for (int k = segs[i].first; k <= segs[i].second; ++k)
          Ans[k] = true;
        break;
      }
  // output
  for (int i = 1; i <= n; ++i)
    puts(Ans[i] ? "Possible" : "Impossible");
  return 0;
}


Fixed submission:
// AT 2365
// DeP
#include <algorithm>
#include <cctype>
#include <cstdio>
using namespace std;

namespace IO {
const int MAXSIZE = 1 << 18 | 1;
char buf[MAXSIZE], *p1, *p2;

inline int Gc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <class T> void read(T &x) {
  x = 0;
  int f = 0, ch = Gc();
  while (!isdigit(ch))
    f |= ch == '-', ch = Gc();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = Gc();
  if (f)
    x = -x;
}
} // namespace IO
using IO::read;

typedef pair<int, int> Pii;
const int MAXN = 1e6 + 5, LOG = 21, INF = 0x3f3f3f3f;

int n, V;
int X[MAXN], f[MAXN], g[MAXN];
int L[LOG][MAXN], R[LOG][MAXN];
bool Ans[MAXN];
Pii segs[MAXN];

int main() {
  // input
  read(n);
  read(V);
  for (int i = 1; i <= n; ++i)
    read(X[i]);
  X[0] = -INF;
  X[n + 1] = INF;
  // solve
  int log = 0;
  for (int v = V; v >= 0; v /= 2, ++log) {
    R[log][n + 1] = n + 1;
    for (int i = 1; i <= n; ++i)
      L[log][i] = X[i] - X[i - 1] <= v ? L[log][i - 1] : i;
    for (int i = n; i >= 1; --i)
      R[log][i] = X[i + 1] - X[i] <= v ? R[log][i + 1] : i;
    if (!v) {
      ++log;
      break;
    }
  }
  for (int i = 1; i <= n; ++i)
    segs[i] = make_pair(L[0][i], R[0][i]);
  int tot = unique(segs + 1, segs + 1 + n) - segs - 1;
  if (tot > log) {
    for (int i = 1; i <= n; ++i)
      puts("Impossible");
    return 0;
  }
  for (int S = 0; S < (1 << log); ++S)
    f[S] = n + 1;
  for (int S = 0; S < (1 << log); S += 2) {
    for (int i = 0; i < log; ++i)
      if ((S >> i) & 1) {
        f[S] = min(f[S], L[i][f[S ^ (1 << i)] - 1]);
        g[S] = max(g[S], R[i][g[S ^ (1 << i)] + 1]);
      }
  }
  for (int i = 1; i <= tot; ++i)
    for (int S = 0; S < (1 << log); S += 2)
      if (g[S] >= segs[i].first - 1 &&
          segs[i].second + 1 >= f[((1 << log) - 1) - (S + 1)]) {
        for (int k = segs[i].first; k <= segs[i].second; ++k)
          Ans[k] = true;
        break;
      }
  // output
  for (int i = 1; i <= n; ++i)
    puts(Ans[i] ? "Possible" : "Impossible");
  return 0;
}