Problem p03771 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#define MAX 200002

int n;
int v;

vector<int> vv;

int dp[MAX][20];
int val[MAX];

map<int, vector<int>> mp;

vector<vector<int>> V;

vector<int> VV;

int mx = 0;

int D[1 << 17];
bool ok() {
  if (VV.size() == 0) {
    return true;
  }
  int N = VV.size();
  for (int i = 1; i <= mx; i++) {
    dp[N - 1][i] = N - 1;
  }
  for (int i = N - 2; i >= 0; i--) {
    long long int d = VV[i + 1] - VV[i];
    for (int j = 1; j <= mx; j++) {
      if (d <= val[j]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = i;
      }
    }
  }
  memset(D, 0, sizeof(D));
  for (int i = 0; i < (1 << mx); i++) {
    if (D[i] >= N) {
      return true;
    }
    for (int j = 1; j <= mx; j++) {
      if ((i >> (j - 1)) & 1)
        continue;
      int go = i | (1 << (j - 1));
      D[go] = max(D[go], dp[D[i]][j] + 1);
    }
  }
  return false;
}
bool ans[MAX];

int main() {
  cin >> n >> v;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    vv.push_back(x);
  }

  val[0] = v;
  for (mx = 1; v / 2 >= 0; mx++) {
    v /= 2;
    val[mx] = v;
    if (v == 0)
      break;
  }
  for (int i = 0; i <= mx; i++) {
    dp[n - 1][i] = n - 1;
  }
  for (int i = n - 2; i >= 0; i--) {
    long long int d = vv[i + 1] - vv[i];
    for (int j = 0; j <= mx; j++) {
      if (d <= val[j]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = i;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    mp[dp[i][0]].push_back(i);
  }
  if (mp.size() > mx + 1) {

  } else {
    for (auto it = mp.begin(); it != mp.end(); it++) {
      VV.clear();
      for (int i = 0; i < n; i++) {
        if (i < (*it).second[0] || (*it).second.back() < i) {
          VV.push_back(vv[i]);
        }
      }
      if (ok()) {
        vector<int> &vvv = (*it).second;
        for (int j = 0; j < vvv.size(); j++) {
          ans[vvv[j]] = true;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == true) {
      puts("Possible");
    } else {
      puts("Impossible");
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#define MAX 200002

int n;
int v;

vector<int> vv;

int dp[MAX][20];
int val[MAX];

map<int, vector<int>> mp;

vector<vector<int>> V;

vector<int> VV;

int mx = 0;

int D[1 << 18];
bool ok() {
  if (VV.size() == 0) {
    return true;
  }
  int N = VV.size();
  for (int i = 1; i <= mx; i++) {
    dp[N - 1][i] = N - 1;
  }
  for (int i = N - 2; i >= 0; i--) {
    long long int d = VV[i + 1] - VV[i];
    for (int j = 1; j <= mx; j++) {
      if (d <= val[j]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = i;
      }
    }
  }
  memset(D, 0, sizeof(D));
  for (int i = 0; i < (1 << mx); i++) {
    if (D[i] >= N) {
      return true;
    }
    for (int j = 1; j <= mx; j++) {
      if ((i >> (j - 1)) & 1)
        continue;
      int go = i | (1 << (j - 1));
      D[go] = max(D[go], dp[D[i]][j] + 1);
    }
  }
  return false;
}
bool ans[MAX];

int main() {
  cin >> n >> v;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    vv.push_back(x);
  }

  val[0] = v;
  for (mx = 1; v / 2 >= 0; mx++) {
    v /= 2;
    val[mx] = v;
    if (v == 0)
      break;
  }
  for (int i = 0; i <= mx; i++) {
    dp[n - 1][i] = n - 1;
  }
  for (int i = n - 2; i >= 0; i--) {
    long long int d = vv[i + 1] - vv[i];
    for (int j = 0; j <= mx; j++) {
      if (d <= val[j]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = i;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    mp[dp[i][0]].push_back(i);
  }
  if (mp.size() > mx + 1) {

  } else {
    for (auto it = mp.begin(); it != mp.end(); it++) {
      VV.clear();
      for (int i = 0; i < n; i++) {
        if (i < (*it).second[0] || (*it).second.back() < i) {
          VV.push_back(vv[i]);
        }
      }
      if (ok()) {
        vector<int> &vvv = (*it).second;
        for (int j = 0; j < vvv.size(); j++) {
          ans[vvv[j]] = true;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == true) {
      puts("Possible");
    } else {
      puts("Impossible");
    }
  }
  return 0;
}