Problem p04017 (C++)

Buggy submission (status: Runtime Error):
// includes
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// macros
#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue
#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 = ((int)(n)-1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int N[30][100001];

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<ll> x(n);
  rep(i, n) cin >> x[i];
  ll l;
  int q;
  cin >> l >> q;
  rep(i, n) {
    N[0][i] = upper_bound(all(x), x[i] + l) - x.begin() - 1;
    if (i == n - 1)
      N[0][i] = n;
  }
  FOR(k, 1, 20) {
    rep(i, n) { N[k][i] = N[k - 1][N[k - 1][i]]; }
  }

  rep(i_, q) {
    int a, b;
    cin >> a >> b, a--, b--;
    if (a > b)
      swap(a, b);
    int res = 0;
    int at = a;
    while (true) {
      int k = 0;
      while (N[k][at] < b) {
        k++;
      }
      if (N[k][at] == b || k == 0) {
        cout << res + (1 << k) << endl;
        break;
      }
      k--;
      res += 1 << k;
      at = N[k][at];
    }
  }
  return 0;
}


Fixed submission:
// includes
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// macros
#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue
#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 = ((int)(n)-1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int N[30][100001];

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<ll> x(n);
  rep(i, n) cin >> x[i];
  ll l;
  int q;
  cin >> l >> q;
  rep(i, n) {
    N[0][i] = upper_bound(all(x), x[i] + l) - x.begin() - 1;
    if (i == n - 1)
      N[0][i] = n;
  }
  FOR(k, 1, 20) {
    rep(i, n) {
      if (N[k - 1][i] < n)
        N[k][i] = N[k - 1][N[k - 1][i]];
      else
        N[k][i] = n;
    }
  }

  rep(i_, q) {
    int a, b;
    cin >> a >> b, a--, b--;
    if (a > b)
      swap(a, b);
    int res = 0;
    int at = a;
    while (true) {
      int k = 0;
      while (N[k][at] < b) {
        k++;
      }
      if (N[k][at] == b || k == 0) {
        cout << res + (1 << k) << endl;
        break;
      }
      k--;
      res += 1 << k;
      at = N[k][at];
    }
  }
  return 0;
}