Problem p03771 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, m, n) for (i = m; i <= n; i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int, int>
#define sys system("pause")
const int maxn = 2e5 + 10;
const int N = 2e5 + 10;
using namespace std;
int id(int l, int r) { return l + r | l != r; }
ll gcd(ll p, ll q) { return q == 0 ? p : gcd(q, p % q); }
ll qpow(ll p, ll q) {
  ll f = 1;
  while (q) {
    if (q & 1)
      f = f * p % mod;
    p = p * p % mod;
    q >>= 1;
  }
  return f;
}
int n, m, k, t, v, x[maxn], d[20], ret[maxn], l[20][maxn], r[20][maxn],
    dpl[1 << 20], dpr[1 << 20], tot;
int main() {
  int i, j;
  scanf("%d%d", &n, &v);
  rep(i, 1, n) scanf("%d", &x[i]);
  while (v) {
    d[tot++] = v;
    v /= 2;
  }
  d[tot++] = 0;
  rep(i, 0, tot - 1) {
    rep(j, 1, n) {
      if (j > 1 && x[j] - x[j - 1] <= d[i])
        l[i][j] = l[i][j - 1];
      else
        l[i][j] = j;
    }
    for (j = n; j >= 1; j--) {
      if (j < n && x[j + 1] - x[j] <= d[i])
        r[i][j] = r[i][j + 1];
      else
        r[i][j] = j;
    }
  }
  memset(dpr, inf, sizeof(dpr));
  dpl[0] = 0, dpr[0] = n + 1;
  rep(i, 0, (1 << tot) - 1) {
    rep(j, 0, tot - 1) {
      if (~i & (1 << j)) {
        if (dpl[i] + 1 <= n)
          dpl[i ^ (1 << j)] = max(dpl[i ^ (1 << j)], r[j][dpl[i] + 1]);
        if (dpr[i] - 1 >= 1)
          dpr[i ^ (1 << j)] = min(dpr[i ^ (1 << j)], l[j][dpr[i] - 1]);
      }
    }
  }
  rep(i, 0, (1 << tot) - 1) {
    if (i & 1)
      continue;
    if (dpl[i] >= dpr[(i ^ ((1 << tot) - 1)) - 1]) {
      ret[1]++;
      ret[n + 1]--;
    } else {
      if (r[0][dpl[i] + 1] >= dpr[(i ^ ((1 << tot) - 1)) - 1] - 1) {
        int x = dpl[i] + 1;
        ret[l[0][x]]++;
        ret[r[0][x] + 1]--;
      }
    }
  }
  rep(i, 1, n) {
    ret[i] += ret[i - 1];
    puts(ret[i] ? "Possible" : "Impossible");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, m, n) for (i = m; i <= n; i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int, int>
#define sys system("pause")
const int maxn = 2e5 + 10;
const int N = 2e5 + 10;
using namespace std;
int id(int l, int r) { return l + r | l != r; }
ll gcd(ll p, ll q) { return q == 0 ? p : gcd(q, p % q); }
ll qpow(ll p, ll q) {
  ll f = 1;
  while (q) {
    if (q & 1)
      f = f * p % mod;
    p = p * p % mod;
    q >>= 1;
  }
  return f;
}
int n, m, k, t, v, x[maxn], d[20], ret[maxn], l[20][maxn], r[20][maxn],
    dpl[1 << 20], dpr[1 << 20], tot;
int main() {
  int i, j;
  scanf("%d%d", &n, &v);
  rep(i, 1, n) scanf("%d", &x[i]);
  while (v) {
    d[tot++] = v;
    v /= 2;
  }
  d[tot++] = 0;
  rep(i, 0, tot - 1) {
    rep(j, 1, n) {
      if (j > 1 && x[j] - x[j - 1] <= d[i])
        l[i][j] = l[i][j - 1];
      else
        l[i][j] = j;
    }
    for (j = n; j >= 1; j--) {
      if (j < n && x[j + 1] - x[j] <= d[i])
        r[i][j] = r[i][j + 1];
      else
        r[i][j] = j;
    }
  }
  rep(i, 0, (1 << tot) - 1) dpr[i] = n + 1;
  rep(i, 0, (1 << tot) - 1) {
    rep(j, 0, tot - 1) {
      if (~i & (1 << j)) {
        if (dpl[i] + 1 <= n)
          dpl[i ^ (1 << j)] = max(dpl[i ^ (1 << j)], r[j][dpl[i] + 1]);
        if (dpr[i] - 1 >= 1)
          dpr[i ^ (1 << j)] = min(dpr[i ^ (1 << j)], l[j][dpr[i] - 1]);
      }
    }
  }
  rep(i, 0, (1 << tot) - 1) {
    if (i & 1)
      continue;
    if (dpl[i] >= dpr[(i ^ ((1 << tot) - 1)) - 1]) {
      ret[1]++;
      ret[n + 1]--;
    } else {
      if (r[0][dpl[i] + 1] >= dpr[(i ^ ((1 << tot) - 1)) - 1] - 1) {
        int x = dpl[i] + 1;
        ret[l[0][x]]++;
        ret[r[0][x] + 1]--;
      }
    }
  }
  rep(i, 1, n) {
    ret[i] += ret[i - 1];
    puts(ret[i] ? "Possible" : "Impossible");
  }
  return 0;
}