Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------
ll tugi[20][101010];
//--MAIN-----------------------------------
void Main() {
  ll N;
  cin >> N;
  vector<ll> x(N);
  rep(i, N) cin >> x[i];
  ll L;
  cin >> L;
  ll Q;
  cin >> Q;
  rep(i, N) {
    ll tu = upper_bound(ALL(x), x[i] + L) - x.begin();
    tu--;
    tugi[0][i] = tu;
  }
  rep(i, 20) {
    rep(j, N) { tugi[i + 1][j] = tugi[i][tugi[i][j]]; }
  }
  while (Q--) {
    ll a, b;
    cin >> a >> b;
    --a, --b;
    if (a > b)
      swap(a, b);
    ll ans = 0;
    repr(i, 19, 0) {
      if (tugi[i][a] < b) {
        ans += (1LL << i);
        a = tugi[i][a];
      }
    }
    ++ans;
    cout << ans << "\n";
  }
}
//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------


Fixed submission:
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------
ll tugi[20][101010];
//--MAIN-----------------------------------
void Main() {
  ll N;
  cin >> N;
  vector<ll> x(N);
  rep(i, N) cin >> x[i];
  ll L;
  cin >> L;
  ll Q;
  cin >> Q;
  rep(i, N) {
    ll tu = upper_bound(ALL(x), x[i] + L) - x.begin();
    tu--;
    tugi[0][i] = tu;
  }
  rep(i, 19) {
    rep(j, N) { tugi[i + 1][j] = tugi[i][tugi[i][j]]; }
  }
  while (Q--) {
    ll a, b;
    cin >> a >> b;
    --a, --b;
    if (a > b)
      swap(a, b);
    ll ans = 0;
    repr(i, 19, 0) {
      if (tugi[i][a] < b) {
        ans += (1LL << i);
        a = tugi[i][a];
      }
    }
    ++ans;
    cout << ans << "\n";
  }
}
//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------