Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) for (int(i) = (n)-1; (i) >= 0; --(i))
#define ALL(TheArray) TheArray.begin(), TheArray.end()

using lli = long long int;
using pii = std::pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

constexpr int K = 17; // 2^17 > 1e5
constexpr int N = 1e5;
std::array<std::array<int, N>, K + 1> A;
std::array<int, N> X;

int main(void) {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d", &X[i]);

  int L;
  scanf("%d", &L);
  int l = 0, r = 1;
  while (l < n) {
    while (r < n and X[r] - X[l] <= L)
      r++;
    A[0][l] = r - 1;
    l++;
  }
  REP(t, K) REP(i, n) A[t + 1][i] = A[t][A[t][i]];

  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    if (a > b)
      std::swap(a, b);
    int res = 0;
    int ptr = a;
    while (ptr < b) {
      int cnt = 0;
      while (A[cnt + 1][ptr] <= b)
        cnt++;
      res += (1 << cnt);
      ptr = A[cnt][ptr];
    }
    printf("%d\n", res);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) for (int(i) = (n)-1; (i) >= 0; --(i))
#define ALL(TheArray) TheArray.begin(), TheArray.end()

using lli = long long int;
using pii = std::pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

constexpr int K = 17; // 2^17 > 1e5
constexpr int N = 1e5;
std::array<std::array<int, N>, K + 1> A;
std::array<int, N> X;

int main(void) {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d", &X[i]);

  int L;
  scanf("%d", &L);
  int l = 0, r = 1;
  while (l < n) {
    while (r < n and X[r] - X[l] <= L)
      r++;
    A[0][l] = r - 1;
    l++;
  }
  REP(t, K) REP(i, n) A[t + 1][i] = A[t][A[t][i]];

  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    if (a > b)
      std::swap(a, b);
    int res = 0;
    int ptr = a;
    while (ptr < b) {
      int cnt = 0;
      while (cnt + 1 <= K and A[cnt + 1][ptr] < b)
        cnt++;
      res += (1 << cnt);
      ptr = A[cnt][ptr];
    }
    printf("%d\n", res);
  }
  return 0;
}