Problem p03771 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define MAX 200200
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = true, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
void cmax(int &x, int y) {
  if (x < y)
    x = y;
}
void cmin(int &x, int y) {
  if (x > y)
    x = y;
}
int n, V[25], x[MAX], t;
int L[20][MAX], R[20][MAX], mr[MAX];
int sl[1 << 19], sr[1 << 19];
int main() {
  n = read();
  V[0] = read();
  for (int i = 1; i <= n; ++i)
    x[i] = read();
  while (V[t])
    V[t + 1] = V[t] >> 1, ++t;
  int S = 1 << t;
  for (int i = 0; i <= t; ++i) {
    L[i][1] = 1;
    R[i][n] = n;
    for (int j = 2; j <= n; ++j)
      if (x[j] - x[j - 1] <= V[i])
        L[i][j] = L[i][j - 1];
      else
        L[i][j] = j;
    for (int j = n - 1; ~j; --j)
      if (x[j + 1] - x[j] <= V[i])
        R[i][j] = R[i][j + 1];
      else
        R[i][j] = j;
  }
  for (int i = 1; i < S; ++i)
    sl[i] = 1, sr[i] = n;
  sl[0] = 0;
  sr[0] = n + 1;
  for (int i = 0; i < S; ++i)
    for (int j = 1; j <= t; ++j)
      if (!(i & (1 << (j - 1)))) {
        cmax(sl[i | (1 << (j - 1))], R[j][sl[i] + 1]);
        cmin(sr[i | (1 << (j - 1))], L[j][sr[i] - 1]);
      }
  memset(mr, 63, sizeof(mr));
  for (int i = 0; i <= n + 1; ++i)
    for (int j = 0; j < S; ++j)
      cmin(mr[sl[j]], sr[(S - 1) ^ j]);
  for (int i = n; ~i; --i)
    mr[i] = min(mr[i], mr[i + 1]);
  for (int i = 1; i <= n; ++i)
    puts(mr[L[0][i] - 1] <= R[0][i] + 1 ? "Possible" : "Impossible");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define MAX 200200
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = true, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
void cmax(int &x, int y) {
  if (x < y)
    x = y;
}
void cmin(int &x, int y) {
  if (x > y)
    x = y;
}
int n, V[25], x[MAX], t;
int L[20][MAX], R[20][MAX], mr[MAX];
int sl[1 << 19], sr[1 << 19];
int main() {
  n = read();
  V[0] = read();
  for (int i = 1; i <= n; ++i)
    x[i] = read();
  while (V[t])
    V[t + 1] = V[t] >> 1, ++t;
  int S = 1 << t;
  for (int i = 0; i <= t; ++i) {
    L[i][1] = 1;
    R[i][n] = n;
    for (int j = 2; j <= n; ++j)
      if (x[j] - x[j - 1] <= V[i])
        L[i][j] = L[i][j - 1];
      else
        L[i][j] = j;
    for (int j = n - 1; ~j; --j)
      if (x[j + 1] - x[j] <= V[i])
        R[i][j] = R[i][j + 1];
      else
        R[i][j] = j;
  }
  for (int i = 1; i < S; ++i)
    sl[i] = 1, sr[i] = n;
  sl[0] = 0;
  sr[0] = n + 1;
  for (int i = 0; i < S; ++i)
    for (int j = 1; j <= t; ++j)
      if (!(i & (1 << (j - 1)))) {
        cmax(sl[i | (1 << (j - 1))], R[j][sl[i] + 1]);
        cmin(sr[i | (1 << (j - 1))], L[j][sr[i] - 1]);
      }
  memset(mr, 63, sizeof(mr));
  for (int j = 0; j < S; ++j)
    cmin(mr[sl[j]], sr[(S - 1) ^ j]);
  for (int i = n; ~i; --i)
    mr[i] = min(mr[i], mr[i + 1]);
  for (int i = 1; i <= n; ++i)
    puts(mr[L[0][i] - 1] <= R[0][i] + 1 ? "Possible" : "Impossible");
  return 0;
}