Problem p04017 (C++)

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

using VS = vector<string>;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VL = vector<LL>;
using VVL = vector<VL>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
// #pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9;
const LL LINF = 1e16;
const LL MOD = 1000000007;
const double PI = acos(-1.0);
int DX[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int DY[8] = {1, -1, 0, 0, 1, -1, 1, -1};

/* -----  __MAKE_TIME__  Problem: __PROBLEM__ / Link: __CONTEST_URL__  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

LL ans = 0LL;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  LL N;
  cin >> N;
  VL x(N);
  FOR(i, 0, N) { cin >> x[i]; }
  LL L, Q;
  cin >> L >> Q;

  // ダブリング
  // d[x][i]:=xから2^i回移動して移動可能な最長の頂点
  VVL d(N, VL(31, -1));
  FOR(i, 0, N) {
    int id = lower_bound(ALL(x), x[i] + L) - x.begin();
    if (x[id] - x[i] > L)
      id--;
    d[i][0] = id;
  }
  FOR(i, 0, 30) {
    FOR(j, 0, N) { d[j][i + 1] = d[d[j][i]][i]; }
  }

  FOR(_, 0, Q) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    ans = 0;
    DD(de(a, "to", b));
    // 上から二分探索
    FORR(i, 30, 0 - 1) {
      if (b > d[a][i]) {
        ans += 1 << i;
        a = d[a][i];
        DD(de("step to", a));
      }
    }
    cout << ans + 1 << "\n";
  }

  return 0;
}

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

using VS = vector<string>;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VL = vector<LL>;
using VVL = vector<VL>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
// #pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9;
const LL LINF = 1e16;
const LL MOD = 1000000007;
const double PI = acos(-1.0);
int DX[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int DY[8] = {1, -1, 0, 0, 1, -1, 1, -1};

/* -----  __MAKE_TIME__  Problem: __PROBLEM__ / Link: __CONTEST_URL__  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

LL ans = 0LL;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  LL N;
  cin >> N;
  VL x(N);
  FOR(i, 0, N) { cin >> x[i]; }
  LL L, Q;
  cin >> L >> Q;

  // ダブリング
  // d[x][i]:=xから2^i回移動して移動可能な最長の頂点
  VVL d(N, VL(31, -1));
  FOR(i, 0, N) {
    int id = lower_bound(ALL(x), x[i] + L) - x.begin();
    if (id == N || x[id] - x[i] > L)
      id--;
    d[i][0] = id;
  }
  FOR(i, 0, 30) {
    FOR(j, 0, N) { d[j][i + 1] = d[d[j][i]][i]; }
  }

  FOR(_, 0, Q) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    ans = 0;
    DD(de(a, "to", b));
    // 上から二分探索
    FORR(i, 30, 0 - 1) {
      if (b > d[a][i]) {
        ans += 1 << i;
        a = d[a][i];
        DD(de("step to", a));
      }
    }
    cout << ans + 1 << "\n";
  }

  return 0;
}