Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int int64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i : a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> x(N);
  rep(i, N) cin >> x[i];
  int L, Q;
  cin >> L >> Q;
  vector<array<int, 2>> ab(Q);
  rep(i, Q) cin >> ab[i][0] >> ab[i][1];
  rep(i, Q) rep(j, 2) ab[i][j]--;

  rep(i, Q) {
    int answer = 1, pos = ab[i][0], goal = ab[i][1];
    int capacity = L;
    if (pos < goal) {
      for (; pos < goal; pos++) {
        int len = x[pos + 1] - x[pos];
        if (len <= capacity)
          capacity -= len;
        else {
          capacity = L - len;
          answer++;
        }
      }
    } else {
      for (; goal < pos; pos--) {
        int len = x[pos] - x[pos - 1];
        if (len <= capacity)
          capacity -= len;
        else {
          capacity = L - len;
          answer++;
        }
      }
    }
    cout << answer << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int int64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i : a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> x(N);
  rep(i, N) cin >> x[i];
  int L, Q;
  cin >> L >> Q;
  vector<array<int, 2>> ab(Q);
  rep(i, Q) cin >> ab[i][0] >> ab[i][1];
  rep(i, Q) rep(j, 2) ab[i][j]--;

  // table[i][j]:=ホテルiから(1<<j)日以内に到達できるような最も右のホテル。
  vector<vector<int>> table(N, vector<int>(17, 0));
  rep(j, 17) table[N - 1][j] = N - 1;

  // ホテルiから1日以内に到達できる最も右のホテルを2分探索する。
  rep(i, N - 1) {
    int lb = i + 1, ub = N; // 真の解は[lb,ub)の間。
    while (lb + 1 < ub) {
      const int med = (lb + ub) / 2;
      const int dist = x[med] - x[i];
      if (dist <= L)
        lb = med;
      else
        ub = med;
    }
    table[i][0] = lb;
  }

  // ホテルiから1<<j日以内に到達できる最も右のホテルを再帰的に求める。
  REP(j, 1, 17) rep(i, N - 1) { table[i][j] = table[table[i][j - 1]][j - 1]; }

  rep(qq, Q) {
    int a = min(ab[qq][0], ab[qq][1]);
    int b = max(ab[qq][0], ab[qq][1]);

    int answer = 1;
    for (int i = 16; 0 <= i; i--) {
      if (table[a][i] < b) {
        a = table[a][i];
        answer += 1 << i;
      }
    }
    cout << answer << endl;
  }
}