Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for (int i = j; i <= k; i++)
#define FFOR(i, j, k) for (int i = j; i < k; i++)
#define DFOR(i, j, k) for (int i = j; i >= k; i--)
#define bug(x) cerr << #x << " = " << x << '\n'
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
template <typename T> inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar()))
    x = x * 10 + c - 48;
  if (nega)
    x = -x;
}
template <typename T> inline void writep(T x) {
  if (x > 9)
    writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\n');
}
#define taskname "E"
int x[100001], l;
int f[100001][21];
int ans[100001];
int n, q, a, b;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    query[100001];
int main() {
#ifdef Megumin
  if (fopen(taskname ".inp", "r"))
    freopen(taskname ".inp", "r", stdin);
#endif // Megumin
  read(n);
  FOR(i, 1, n) read(x[i]);
  read(l);
  read(q);
  FOR(i, 1, q) {
    read(a);
    read(b);
    if (a > b)
      swap(a, b);
    query[a].push(mp(b, i));
  }
  f[n][0] = n;
  DFOR(i, n - 1, 1) {
    f[i][0] = f[i + 1][0];
    while (x[f[i][0]] - x[i] > l)
      f[i][0]--;
  }
  for (int j = 1, r = 2; r <= n; j++, r <<= 1) {
    for (int i = 1; f[i][j - 1] < n; i++)
      f[i][j] = f[f[i][j - 1]][j - 1];
  }
  pair<int, int> p;
  FOR(i, 1, n) {
    int j = 0, r = 1;
    while (!query[i].empty()) {
      p = query[i].top();
      query[i].pop();
      while (f[i][j] < p.first) {
        r <<= 1;
        j++;
      }
      if (j == 0)
        ans[p.second]++;
      else {
        ans[p.second] += r >> 1;
        query[f[i][j - 1]].push(p);
      }
    }
  }
  FOR(i, 1, q) writeln(ans[i]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for (int i = j; i <= k; i++)
#define FFOR(i, j, k) for (int i = j; i < k; i++)
#define DFOR(i, j, k) for (int i = j; i >= k; i--)
#define bug(x) cerr << #x << " = " << x << '\n'
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
template <typename T> inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar()))
    x = x * 10 + c - 48;
  if (nega)
    x = -x;
}
template <typename T> inline void writep(T x) {
  if (x > 9)
    writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\n');
}
#define taskname "E"
int x[100001], l;
int f[100001][21];
int ans[100001];
int n, q, a, b;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    query[100001];
int main() {
#ifdef Megumin
  if (fopen(taskname ".inp", "r"))
    freopen(taskname ".inp", "r", stdin);
#endif // Megumin
  read(n);
  FOR(i, 1, n) read(x[i]);
  read(l);
  read(q);
  FOR(i, 1, q) {
    read(a);
    read(b);
    if (a > b)
      swap(a, b);
    query[a].push(mp(b, i));
  }
  f[n][0] = n;
  DFOR(i, n - 1, 1) {
    f[i][0] = f[i + 1][0];
    while (x[f[i][0]] - x[i] > l)
      f[i][0]--;
  }
  if (f[1][0] < n)
    for (int j = 1;; j++) {
      FOR(i, 1, n) f[i][j] = f[f[i][j - 1]][j - 1];
      if (f[1][j] == n)
        break;
    }
  pair<int, int> p;
  FOR(i, 1, n) {
    int j = 0, r = 1;
    while (!query[i].empty()) {
      p = query[i].top();
      query[i].pop();
      while (f[i][j] < p.first) {
        r <<= 1;
        j++;
      }
      if (j == 0)
        ans[p.second]++;
      else {
        ans[p.second] += r >> 1;
        query[f[i][j - 1]].push(p);
      }
    }
  }
  FOR(i, 1, q) writeln(ans[i]);
}