Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

int main() {
  int n, l;
  cin >> n;
  vector<int> x(n), d[n];
  for (int i = 0; i < n; ++i)
    cin >> x[i];
  cin >> l;
  {
    int j = 1;
    for (int i = 0; i < n; ++i) {
      while (j < n - 1 && x[j + 1] - x[i] <= l)
        j++;
      if (i != n - 1)
        d[i].push_back(j);
    }
  }
  for (int i = 0; i < 20; ++i) {
    for (int j = 0; j < n; ++j) {
      if (d[j].size() == i + 1 && d[d[j][i]].size() == i + 1)
        d[j].push_back(d[d[j][i]][i]);
      else
        break;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    while (a < b) {
      for (int j = d[a].size() - 1; j >= 0; --j)
        if (d[a][j] <= b) {
          ans += 1 << j;
          a = d[a][j];
          break;
        }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

int main() {
  int n, l;
  cin >> n;
  vector<int> x(n), d[n];
  for (int i = 0; i < n; ++i)
    cin >> x[i];
  cin >> l;
  {
    int j = 1;
    for (int i = 0; i < n; ++i) {
      while (j < n - 1 && x[j + 1] - x[i] <= l)
        j++;
      if (i != n - 1)
        d[i].push_back(j);
    }
  }
  for (int i = 0; i < 20; ++i) {
    for (int j = 0; j < n; ++j) {
      if (d[j].size() == i + 1 && d[d[j][i]].size() == i + 1)
        d[j].push_back(d[d[j][i]][i]);
      else
        break;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    while (a < b) {
      for (int j = d[a].size() - 1; j >= 0; --j)
        if (d[a][j] <= b || j == 0) {
          ans += 1 << j;
          a = d[a][j];
          break;
        }
    }
    cout << ans << endl;
  }
  return 0;
}