Problem p03771 (C++)

Buggy submission (status: Runtime Error):
// Indomie, Mie dari Indonesia
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define pb push_back
const int LogN = 18;
const int MAXN = 200000;
int n, air, jarak[(1 << (LogN)) + 5], terkanan[LogN + 5][MAXN + 5],
    terkiri[LogN + 5][MAXN + 5];
int pos[MAXN + 5], l, r, komponen;
bool ans[MAXN + 5];
vector<int> daftarair;
void isiair(int air) {
  while (air > 0)
    daftarair.pb(air), air /= 2;
  daftarair.pb(0);
  assert(daftarair.size() <= LogN);
}
void isiterkanan(int jenis) {
  int last = 1;
  for (int i = 2; i <= n; i++) {
    if (pos[i] - pos[i - 1] > daftarair[jenis]) {
      while (last != i) {
        terkanan[jenis][last] = i - 1;
        last++;
      }
    }
  }
  while (last != n + 1) {
    terkanan[jenis][last] = n;
    last++;
  }
}
void isiterkiri(int jenis) {
  int last = n;
  for (int i = n - 1; i >= 1; i--) {
    if (pos[i + 1] - pos[i] > daftarair[jenis]) {
      while (last != i) {
        terkiri[jenis][last] = i + 1;
        last--;
      }
    }
  }
  while (last != 0) {
    terkiri[jenis][last] = 1;
    last--;
  }
}
void isiterkanan() {
  for (int i = 0; i < daftarair.size(); i++)
    isiterkanan(i);
}
void isiterkiri() {
  for (int i = 0; i < daftarair.size(); i++)
    isiterkiri(i);
}
int totalkomponen() {
  int ret = 0;
  int now = 1;
  while (now <= n) {
    ret++;
    now = terkanan[0][now];
    now++;
  }
  return ret;
}
bool bfs() {
  queue<int> cari;
  memset(jarak, -1, sizeof(jarak));
  jarak[0] = 1;
  if (l <= jarak[0] && jarak[0] <= r)
    jarak[0] = r + 1;
  cari.push(0);
  while (!cari.empty()) {
    // cout<<"bfs"<<endl;
    int u = cari.front();
    if (jarak[u] > n) {
      // cout<<"terkanannya "<<jarak[u]<<endl;
      return true;
    }
    cari.pop();
    // cout<<u<<endl;
    for (int i = 1; i < daftarair.size(); i++) {
      if (u & (1 << (i - 1)))
        continue;
      int v = (u | (1 << (i - 1)));
      int nextpos = terkanan[i][jarak[u]] + 1;
      if (l <= nextpos && nextpos <= r)
        nextpos = r + 1;
      if (jarak[v] == -1) {
        jarak[v] = nextpos;
        cari.push(v);
      } else
        jarak[v] = max(jarak[v], nextpos);
    }
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &air);
  isiair(air);
  for (int i = 1; i <= n; i++)
    scanf("%d", &pos[i]);
  isiterkanan();
  isiterkiri();
  komponen = totalkomponen();
  for (int i = 1; i <= n; i++) {
    // cout<<"loop "<<i<<endl;
    if (komponen > daftarair.size()) {
      ans[i] = false;
    } else if (i != 1 &&
               terkanan[0][i] == terkanan[0][i - 1]) // in the same komponen
    {
      ans[i] = ans[i - 1];
    } else {
      l = terkiri[0][i];
      r = terkanan[0][i];
      if (bfs())
        ans[i] = true;
      else
        ans[i] = false;
    }
    if (ans[i])
      printf("Possible\n");
    else
      printf("Impossible\n");
  }
}

Fixed submission:
// Indomie, Mie dari Indonesia
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define pb push_back
const int LogN = 18;
const int MAXN = 200000;
int n, air, jarak[(1 << (LogN)) + 5], terkanan[LogN + 5][MAXN + 5],
    terkiri[LogN + 5][MAXN + 5];
int pos[MAXN + 5], l, r, komponen;
bool ans[MAXN + 5];
vector<int> daftarair;
void isiair(int air) {
  while (air > 0)
    daftarair.pb(air), air /= 2;
  daftarair.pb(0);
  assert(daftarair.size() - 1 <= LogN);
}
void isiterkanan(int jenis) {
  int last = 1;
  for (int i = 2; i <= n; i++) {
    if (pos[i] - pos[i - 1] > daftarair[jenis]) {
      while (last != i) {
        terkanan[jenis][last] = i - 1;
        last++;
      }
    }
  }
  while (last != n + 1) {
    terkanan[jenis][last] = n;
    last++;
  }
}
void isiterkiri(int jenis) {
  int last = n;
  for (int i = n - 1; i >= 1; i--) {
    if (pos[i + 1] - pos[i] > daftarair[jenis]) {
      while (last != i) {
        terkiri[jenis][last] = i + 1;
        last--;
      }
    }
  }
  while (last != 0) {
    terkiri[jenis][last] = 1;
    last--;
  }
}
void isiterkanan() {
  for (int i = 0; i < daftarair.size(); i++)
    isiterkanan(i);
}
void isiterkiri() {
  for (int i = 0; i < daftarair.size(); i++)
    isiterkiri(i);
}
int totalkomponen() {
  int ret = 0;
  int now = 1;
  while (now <= n) {
    ret++;
    now = terkanan[0][now];
    now++;
  }
  return ret;
}
bool bfs() {
  queue<int> cari;
  memset(jarak, -1, sizeof(jarak));
  jarak[0] = 1;
  if (l <= jarak[0] && jarak[0] <= r)
    jarak[0] = r + 1;
  cari.push(0);
  while (!cari.empty()) {
    // cout<<"bfs"<<endl;
    int u = cari.front();
    if (jarak[u] > n) {
      // cout<<"terkanannya "<<jarak[u]<<endl;
      return true;
    }
    cari.pop();
    // cout<<u<<endl;
    for (int i = 1; i < daftarair.size(); i++) {
      if (u & (1 << (i - 1)))
        continue;
      int v = (u | (1 << (i - 1)));
      int nextpos = terkanan[i][jarak[u]] + 1;
      if (l <= nextpos && nextpos <= r)
        nextpos = r + 1;
      if (jarak[v] == -1) {
        jarak[v] = nextpos;
        cari.push(v);
      } else
        jarak[v] = max(jarak[v], nextpos);
    }
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &air);
  isiair(air);
  for (int i = 1; i <= n; i++)
    scanf("%d", &pos[i]);
  isiterkanan();
  isiterkiri();
  komponen = totalkomponen();
  for (int i = 1; i <= n; i++) {
    // cout<<"loop "<<i<<endl;
    if (komponen > daftarair.size()) {
      ans[i] = false;
    } else if (i != 1 &&
               terkanan[0][i] == terkanan[0][i - 1]) // in the same komponen
    {
      ans[i] = ans[i - 1];
    } else {
      l = terkiri[0][i];
      r = terkanan[0][i];
      if (bfs())
        ans[i] = true;
      else
        ans[i] = false;
    }
    if (ans[i])
      printf("Possible\n");
    else
      printf("Impossible\n");
  }
}