prithivMLmods/Coder-Stat huggingface dataset:
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000000000000000000000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
int main() {
#define int long long
  long long n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  } else if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 2; i <= sqrt(n); ++i) {
    long long hoge = n;
    long long tmp = 0;
    while (hoge != 0) {
      tmp += hoge % i;
      hoge /= i;
    }
    if (tmp == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int p = sqrt(n); p >= 1; --p) {
    long long b = (n - s) / p + 1;
    long long hoge = n;
    long long tmp = 0;
    while (hoge != 0) {
      tmp += hoge % b;
      hoge /= b;
    }
    if (tmp == s) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000000000000000000000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
int main() {
#define int long long
  long long n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  } else if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 2; i <= sqrt(n); ++i) {
    long long hoge = n;
    long long tmp = 0;
    while (hoge != 0) {
      tmp += hoge % i;
      hoge /= i;
    }
    if (tmp == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int p = sqrt(n); p >= 1; --p) {
    long long b = (n - s) / p + 1;
    if (b != 1) {
      long long hoge = n;
      long long tmp = 0;
      while (hoge != 0) {
        tmp += hoge % b;
        hoge /= b;
      }
      if (tmp == s) {
        cout << b << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p04016 (C++)

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

using namespace std;

typedef long long ll;

ll f(ll b, ll n) {
  ll res = 0;
  while (n >= b) {
    res += n % b;
    n /= b;
  }
  return res + n;
}

ll solve(ll n, ll s) {
  if (n < s)
    return -1;
  if (n == s)
    return n + 1;
  for (ll b = 2; b * b <= n; b++) {
    if (f(b, n) == s)
      return b;
  }
  for (ll p = (ll)sqrt(n - 1); p > 0; p--) {
    ll b = (n - s) / p + 1;
    if (f(b, n) == s)
      return b;
  }
  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, s;
  cin >> n >> s;
  cout << solve(n, s) << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll f(ll b, ll n) {
  ll res = 0;
  while (n >= b) {
    res += n % b;
    n /= b;
  }
  return res + n;
}

ll solve(ll n, ll s) {
  if (n < s)
    return -1;
  if (n == s)
    return n + 1;
  for (ll b = 2; b * b <= n; b++) {
    if (f(b, n) == s)
      return b;
  }
  for (ll p = (ll)sqrt(n - 1); p > 0; p--) {
    if ((n - s) % p)
      continue;
    ll b = (n - s) / p + 1;
    if (f(b, n) == s)
      return b;
  }
  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, s;
  cin >> n >> s;
  cout << solve(n, s) << "\n";
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int R = 700;
int n, q, L;
int x[500000];
int go1[500000];
int goroot[500000];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  cin >> L;
  int i = 0, j = 0;
  for (; i < n; i++) {
    while (j + 1 < n && x[j + 1] - x[i] <= L) {
      j++;
    }
    go1[i] = j;
  }
  for (int i = 0; i < n; i++) {
    int g = go1[i];
    for (int j = 1; j < R; j++)
      g = go1[g];
    goroot[i] = g;
  }
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    if (b < a)
      swap(a, b);
    a--;
    b--;
    int d = 0;
    while (goroot[a] <= b) {
      a = goroot[a];
      d += R;
    }
    while (a < b) {
      a = go1[a];
      d++;
    }
    cout << d << "\n";
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int R = 700;
int n, q, L;
int x[500000];
int go1[500000];
int goroot[500000];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  cin >> L;
  int i = 0, j = 0;
  for (; i < n; i++) {
    while (j + 1 < n && x[j + 1] - x[i] <= L) {
      j++;
    }
    go1[i] = j;
  }
  for (int i = 0; i < n; i++) {
    int g = go1[i];
    for (int j = 1; j < R; j++)
      g = go1[g];
    goroot[i] = g;
  }
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    if (b < a)
      swap(a, b);
    a--;
    b--;
    int d = 0;
    while (goroot[a] < b) {
      a = goroot[a];
      d += R;
    }
    while (a < b) {
      a = go1[a];
      d++;
    }
    cout << d << "\n";
  }
}
Problem p04017 (C++)

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

int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
  return x;
}

/*============ Header Template ============*/

const int N = 100005;
int n, L, q;
int x[N], r[N][20];
int idx[N];
int main() {
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
  n = read();
  x[n + 1] = 1E9 + 7;
  x[0] = -1E9 - 7;
  for (int i = 2; i <= n; i++)
    idx[i] = idx[i >> 1] + 1;
  for (int i = 1; i <= n; i++)
    x[i] = read();
  L = read();
  q = read();
  for (int i = 1; i <= n; i++) {
    int le = i, ri = n + 1;
    while (ri - le > 1) {
      int mid = (le + ri) >> 1;
      if (x[mid] - x[i] <= L)
        le = mid;
      else
        ri = mid;
    }
    r[i][0] = le;
  }
  for (int j = 1; j < 18; j++)
    for (int i = 1; i <= n; i++) {
      r[i][j] = r[r[i][j - 1]][j - 1];
    }
  for (int i = 1; i <= q; i++) {
    int a = read(), b = read();
    if (b < a)
      swap(a, b);
    int ans = 0;
    for (int j = 17; j >= 0; j--) {
      if (r[a][j] < b) {
        a = r[a][j];
        ans += (1 << j);
      }
    }
    printf("%d\n", ans + 1);
  }
  return 0;
}
/*
9
1 3 6 13 15 18 19 29 31
10
4
1 8
7 3
6 7
8 5
*/

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

int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
  return x;
}

/*============ Header Template ============*/

const int N = 100005;
int n, L, q;
int x[N], r[N][20];
int idx[N];
int main() {
  // freopen("A.in", "r", stdin);
  // freopen("A.out", "w", stdout);
  n = read();
  x[n + 1] = 1E9 + 7;
  x[0] = -1E9 - 7;
  for (int i = 2; i <= n; i++)
    idx[i] = idx[i >> 1] + 1;
  for (int i = 1; i <= n; i++)
    x[i] = read();
  L = read();
  q = read();
  for (int i = 1; i <= n; i++) {
    int le = i, ri = n + 1;
    while (ri - le > 1) {
      int mid = (le + ri) >> 1;
      if (x[mid] - x[i] <= L)
        le = mid;
      else
        ri = mid;
    }
    r[i][0] = le;
  }
  for (int j = 1; j < 18; j++)
    for (int i = 1; i <= n; i++) {
      r[i][j] = r[r[i][j - 1]][j - 1];
    }
  for (int i = 1; i <= q; i++) {
    int a = read(), b = read();
    if (b < a)
      swap(a, b);
    int ans = 0;
    for (int j = 17; j >= 0; j--) {
      if (r[a][j] < b) {
        a = r[a][j];
        ans += (1 << j);
      }
    }
    printf("%d\n", ans + 1);
  }
  return 0;
}
/*
9
1 3 6 13 15 18 19 29 31
10
4
1 8
7 3
6 7
8 5
*/
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define ln '\n'
const long long MOD = 1000000007LL;
// const long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
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;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

int mov[101010][50];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<ll> x(N);
  rep(i, N) cin >> x[i];
  ll L;
  cin >> L;

  rep(i, N - 1) {
    int k = upper_bound(all(x), x[i] + L) - x.begin();
    mov[i][0] = k - 1;
  }
  mov[N - 1][0] = N;
  rep(k, 40) {
    rep(i, N) { mov[i][k + 1] = mov[i][k] == N ? N : mov[mov[i][k]][k]; }
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    if (a > b)
      swap(a, b);
    int ans = 0;
    int cur = a;
    while (cur < b) {
      for (int k = 40; k >= 0; --k) {
        if (mov[cur][k] <= b) {
          ans += 1 << k;
          cur = mov[cur][k];
          break;
        }
      }
    }
    cout << ans << ln;
  }
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define ln '\n'
const long long MOD = 1000000007LL;
// const long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
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;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

int mov[101010][50];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<ll> x(N);
  rep(i, N) cin >> x[i];
  ll L;
  cin >> L;

  rep(i, N - 1) {
    int k = upper_bound(all(x), x[i] + L) - x.begin();
    mov[i][0] = k - 1;
  }
  mov[N - 1][0] = N;
  rep(k, 40) {
    rep(i, N) { mov[i][k + 1] = mov[i][k] == N ? N : mov[mov[i][k]][k]; }
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    if (a > b)
      swap(a, b);
    int ans = 0;
    int cur = a;
    while (cur < b) {
      for (int k = 40; k >= 0; --k) {
        if (mov[cur][k] <= b) {
          ans += 1 << k;
          cur = mov[cur][k];
          break;
        }
        if (k == 0 && mov[cur][k] > b) {
          cur = b;
          ans++;
        }
      }
    }
    cout << ans << ln;
  }
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int nxt[100005][17];
int N, L, Q;
int x[100005];
int main() {
  freopen("In.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> x[i];
  cin >> L >> Q;
  for (int i = 0; i < N; i++)
    nxt[i][0] = (upper_bound(x, x + N, x[i] + L) - x) - 1;
  for (int i = 1; i <= 16; i++) {
    for (int j = 0; j < N; j++) {
      nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
    }
  }
  while (Q--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    for (int i = 16; i >= 0; i--) {
      if (nxt[a][i] < b) {
        ans += (1 << i);
        a = nxt[a][i];
      }
    }
    cout << ans + 1 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int nxt[100005][17];
int N, L, Q;
int x[100005];
int main() {
  // freopen("In.txt" , "r" , stdin);
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> x[i];
  cin >> L >> Q;
  for (int i = 0; i < N; i++)
    nxt[i][0] = (upper_bound(x, x + N, x[i] + L) - x) - 1;
  for (int i = 1; i <= 16; i++) {
    for (int j = 0; j < N; j++) {
      nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
    }
  }
  while (Q--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    for (int i = 16; i >= 0; i--) {
      if (nxt[a][i] < b) {
        ans += (1 << i);
        a = nxt[a][i];
      }
    }
    cout << ans + 1 << endl;
  }
  return 0;
}
Problem p04017 (C++)

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

using namespace std;

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

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e5 + 10;

int n, m;
int a[MAXN];

void read() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  scanf("%d", &m);
}

int fa[20][MAXN];

void solve() {
  a[n + 1] = 2e9 + 10;
  for (int i = 1; i <= n; i++) {
    fa[0][i] = lower_bound(a + i + 1, a + n + 2, a[i] + m + 1) - a - 1;
  }
  int cnt = 0;
  for (int j = 0, k = 1; k <= n; j++, k *= 2) {
    for (int i = 1; i <= n; i++)
      fa[j + 1][i] = fa[j][fa[j][i]]; // fa[fa[i][j]][j];
    cnt++;
  }
  assert(cnt < 20);
  int Q;
  scanf("%d", &Q);
  for (int o = 1; o <= Q; o++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x > y)
      swap(x, y);
    int ans = 0;
    for (int i = cnt; i >= 0; i--) {
      if (fa[i][x] < y) {
        ans += (1 << i);
        x = fa[i][x];
      }
    }
    while (x != y) {
      ans++;
      x = fa[0][x];
    }
    printf("%d\n", ans);
  }
}

void printans() {}

int main() {
  int T = 1;
  //	cin>>T;
  while (T--) {
    read();
    solve();
    printans();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e5 + 10;

int n, m;
int a[MAXN];

void read() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  scanf("%d", &m);
}

int fa[20][MAXN];

void solve() {
  a[n + 1] = 2e9 + 10;
  for (int i = 1; i <= n; i++) {
    fa[0][i] = lower_bound(a + i + 1, a + n + 2, a[i] + m + 1) - a - 1;
  }
  int cnt = 0;
  for (int j = 0, k = 1; k <= n; j++, k *= 2) {
    for (int i = 1; i <= n; i++)
      fa[j + 1][i] = fa[j][fa[j][i]]; // fa[fa[i][j]][j];
    cnt++;
  }
  assert(cnt < 20);
  int Q;
  scanf("%d", &Q);
  for (int o = 1; o <= Q; o++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x > y)
      swap(x, y);
    int ans = 0;
    for (int i = cnt; i >= 0; i--) {
      if (fa[i][x] < y) {
        ans += (1 << i);
        x = fa[i][x];
      }
    }
    while (x < y) {
      ans++;
      x = fa[0][x];
    }
    printf("%d\n", ans);
  }
}

void printans() {}

int main() {
  int T = 1;
  //	cin>>T;
  while (T--) {
    read();
    solve();
    printans();
  }
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const int Maxn = 52;
const ll Inf = LONG_LONG_MAX >> 2;
const int Log = 20;
const ll Mod = 10133;

ll a[Maxn];
ll nx[Maxn][Log];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = Inf;
  ll L, Q;
  cin >> L >> Q;
  int po = 1;
  for (int i = 1; i <= n; i++) {
    while (a[po] - a[i] <= L)
      po++;
    nx[i][0] = po - 1;
  }
  for (int j = 1; j < Log; j++)
    for (int i = 1; i <= n; i++)
      nx[i][j] = nx[nx[i][j - 1]][j - 1];
  ll s, e;
  for (int i = 1; i <= Q; i++) {
    cin >> s >> e;
    if (s > e)
      swap(s, e);
    ll ans = 0;
    for (int j = Log - 1; j >= 0; j--) {
      if (nx[s][j] < e) {
        ans += (1 << j);
        s = nx[s][j];
      }
    }
    cout << ans + 1 << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const int Maxn = 1e5 + 10;
const ll Inf = LONG_LONG_MAX >> 2;
const int Log = 20;
const ll Mod = 10133;

ll a[Maxn];
ll nx[Maxn][Log];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = Inf;
  ll L, Q;
  cin >> L >> Q;
  int po = 1;
  for (int i = 1; i <= n; i++) {
    while (a[po] - a[i] <= L)
      po++;
    nx[i][0] = po - 1;
  }
  for (int j = 1; j < Log; j++)
    for (int i = 1; i <= n; i++)
      nx[i][j] = nx[nx[i][j - 1]][j - 1];
  ll s, e;
  for (int i = 1; i <= Q; i++) {
    cin >> s >> e;
    if (s > e)
      swap(s, e);
    ll ans = 0;
    for (int j = Log - 1; j >= 0; j--) {
      if (nx[s][j] < e) {
        ans += (1 << j);
        s = nx[s][j];
      }
    }
    cout << ans + 1 << '\n';
  }
  return 0;
}
Problem p04017 (C++)

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

const int maxn = 1e5 + 100;

typedef long long LL;
LL X[maxn];

int q;
int n;
LL len;

const int STEP = 50;

int reach[maxn][STEP + 5];

int cal(int L, int R) {
  int cur = L;
  int res = 0;
  while (cur < R) {
    if (reach[cur][STEP] < R) {
      res += STEP;
      cur = reach[cur][STEP];
    } else {
      int id =
          lower_bound(reach[cur] + 1, reach[cur] + STEP + 1, R) - reach[cur];
      res += id;
      cur = reach[cur][id];
    }
  }
  return res;
}

int main() {
  // freopen("./test.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &X[i]);
  scanf("%lld", &len);
  int cur = 1;
  LL now = 0;
  while (cur + 1 <= n && now + X[cur + 1] - X[cur] <= len) {
    now += X[cur + 1] - X[cur];
    cur++;
  }
  reach[1][1] = cur;
  for (int i = 2; i <= n; ++i) {
    now -= X[i] - X[i - 1];
    while (cur + 1 <= n && now + X[cur + 1] - X[cur] <= len) {
      now += X[cur + 1] - X[cur];
      cur++;
    }
    reach[i][1] = cur;
    //  printf("*** %d  %d\n",i,reach[i]);
  }

  for (int j = 2; j <= STEP; ++j) {
    for (int i = 1; i <= n; ++i)
      reach[i][j] = reach[reach[i][1]][j - 1];
  }

  scanf("%d", &q);
  while (q--) {
    int L, R;
    scanf("%d %d", &L, &R);
    if (L > R)
      swap(L, R);
    int res = cal(L, R);
    printf("%d\n", res);
  }

  return 0;
}


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

const int maxn = 1e5 + 100;

typedef long long LL;
LL X[maxn];

int q;
int n;
LL len;

const int STEP = 200;

int reach[maxn][STEP + 5];

int cal(int L, int R) {
  int cur = L;
  int res = 0;
  while (cur < R) {
    if (reach[cur][STEP] < R) {
      res += STEP;
      cur = reach[cur][STEP];
    } else {
      int id =
          lower_bound(reach[cur] + 1, reach[cur] + STEP + 1, R) - reach[cur];
      res += id;
      cur = reach[cur][id];
    }
  }
  return res;
}

int main() {
  // freopen("./test.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &X[i]);
  scanf("%lld", &len);
  int cur = 1;
  LL now = 0;
  while (cur + 1 <= n && now + X[cur + 1] - X[cur] <= len) {
    now += X[cur + 1] - X[cur];
    cur++;
  }
  reach[1][1] = cur;
  for (int i = 2; i <= n; ++i) {
    now -= X[i] - X[i - 1];
    while (cur + 1 <= n && now + X[cur + 1] - X[cur] <= len) {
      now += X[cur + 1] - X[cur];
      cur++;
    }
    reach[i][1] = cur;
    //  printf("*** %d  %d\n",i,reach[i]);
  }

  for (int j = 2; j <= STEP; ++j) {
    for (int i = 1; i <= n; ++i)
      reach[i][j] = reach[reach[i][1]][j - 1];
  }

  scanf("%d", &q);
  while (q--) {
    int L, R;
    scanf("%d %d", &L, &R);
    if (L > R)
      swap(L, R);
    int res = cal(L, R);
    printf("%d\n", res);
  }

  return 0;
}
Problem p04017 (C++)

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

typedef long long ll;
const int N = 1005;
const int M = 1000000007;

int n, l, q;
int a[N];
int nxt[20][N];

int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    scanf("%d%d", &l, &q);
    for (int i = 1, j = 1; i <= n; i++) {
      while (j <= n && a[j] - a[i] <= l) {
        j++;
      }
      nxt[0][i] = j - 1;
    }
    for (int i = 1; i < 20; i++) {
      for (int j = 1; j <= n; j++) {
        nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
      }
    }
    for (int i = 0, x, y; i < q; i++) {
      scanf("%d%d", &x, &y);
      if (x == y) {
        puts("0");
        continue;
      }
      if (x > y) {
        swap(x, y);
      }
      int ans = 1;
      for (int i = 19; i >= 0; i--) {
        if (nxt[i][x] < y) {
          ans += 1 << i;
          x = nxt[i][x];
        }
      }
      printf("%d\n", ans);
    }
  }
}


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

typedef long long ll;
const int N = 100005;
const int M = 1000000007;

int n, l, q;
int a[N];
int nxt[20][N];

int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    scanf("%d%d", &l, &q);
    for (int i = 1, j = 1; i <= n; i++) {
      while (j <= n && a[j] - a[i] <= l) {
        j++;
      }
      nxt[0][i] = j - 1;
    }
    for (int i = 1; i < 20; i++) {
      for (int j = 1; j <= n; j++) {
        nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
      }
    }
    for (int i = 0, x, y; i < q; i++) {
      scanf("%d%d", &x, &y);
      if (x == y) {
        puts("0");
        continue;
      }
      if (x > y) {
        swap(x, y);
      }
      int ans = 1;
      for (int i = 19; i >= 0; i--) {
        if (nxt[i][x] < y) {
          ans += 1 << i;
          x = nxt[i][x];
        }
      }
      printf("%d\n", ans);
    }
  }
}
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

int main(void) {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<vector<int>> d(n, vector<int>(20, -1));
  rep(i, n) cin >> v[i];
  int l;
  cin >> l;
  rep(i, n) d[i][0] = upper_bound(v.begin(), v.end(), v[i] + l) - v.begin() - 1;
  rep(i, 19) rep(j, n) d[j][i + 1] = d[d[j][i]][i];

  int q;
  cin >> q;
  rep(i, q) {
    int a, b, ans = 0;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    while (a < b) {
      int p = lower_bound(d[a].begin(), d[a].end(), b) - d[a].begin();
      if (d[a][p] == b && p == 0) {
        ans += 1 << p;
        break;
      } else {
        ans += 1 << (p - 1);
        a = d[a][p - 1];
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

int main(void) {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<vector<int>> d(n, vector<int>(20, -1));
  rep(i, n) cin >> v[i];
  int l;
  cin >> l;
  rep(i, n) d[i][0] = upper_bound(v.begin(), v.end(), v[i] + l) - v.begin() - 1;
  rep(i, 19) rep(j, n) d[j][i + 1] = d[d[j][i]][i];

  int q;
  cin >> q;
  rep(i, q) {
    int a, b, ans = 0;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    while (a < b) {
      int p = lower_bound(d[a].begin(), d[a].end(), b) - d[a].begin();
      if (d[a][p] >= b && p == 0) {
        ans += 1 << p;
        break;
      } else {
        ans += 1 << (p - 1);
        a = d[a][p - 1];
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
const int mod = 1e9 + 7;
const int INFINT = INT_MAX / 2;
typedef long long ll;
typedef unsigned long long ull;
int dp[40][100000];

void makeDoubling(vector<ull> &xs, ull L) {
  int N = xs.size();
  for (int i = 0; i < N; i++) {
    dp[0][i] =
        lower_bound(xs.begin(), xs.end(), xs[i] + L + 1) - xs.begin() - 1;
  }
  for (int i = 1; i < 40; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = dp[i - 1][dp[i - 1][j]];
    }
  }
  return;
}

int MinimumDays(int a, int b) {
  if (a > b)
    swap(a, b);
  int now = a;
  int res = 0;
  for (int i = 40; i >= 0; i--) {
    if (dp[i][now] < b) {
      now = dp[i][now];
      res += 1 << i;
    }
  }
  res++;
  return res;
}

int main() {
  int N, x;
  cin >> N;
  vector<ull> xs(N);
  for (int i = 0; i < N; i++) {
    cin >> x;
    xs[i] = x;
  }
  ull L;
  int Q, a, b;
  cin >> L >> Q;
  makeDoubling(xs, L);

  for (int i = 0; i < Q; i++) {
    cin >> a >> b;
    cout << MinimumDays(a - 1, b - 1) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
const int mod = 1e9 + 7;
const int INFINT = INT_MAX / 2;
typedef long long ll;
typedef unsigned long long ull;
int dp[40][100000];

void makeDoubling(vector<ull> &xs, ull L) {
  int N = xs.size();
  for (int i = 0; i < N; i++) {
    dp[0][i] =
        lower_bound(xs.begin(), xs.end(), xs[i] + L + 1) - xs.begin() - 1;
  }
  for (int i = 1; i < 40; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = dp[i - 1][dp[i - 1][j]];
    }
  }
  return;
}

int MinimumDays(int a, int b) {
  if (a > b)
    swap(a, b);
  int now = a;
  int res = 0;
  for (int i = 39; i >= 0; i--) {
    if (dp[i][now] < b) {
      now = dp[i][now];
      res += 1 << i;
    }
  }
  res++;
  return res;
}

int main() {
  int N, x;
  cin >> N;
  vector<ull> xs(N);
  for (int i = 0; i < N; i++) {
    cin >> x;
    xs[i] = x;
  }
  ull L;
  int Q, a, b;
  cin >> L >> Q;
  makeDoubling(xs, L);

  for (int i = 0; i < Q; i++) {
    cin >> a >> b;
    cout << MinimumDays(a - 1, b - 1) << endl;
  }

  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int n, q, r, x[100010];
int d[17][100010];
int Pow2[20];
void PRE() {
  int k = 1;
  for (int i = 0; i < 20; i++) {
    Pow2[i] = k;
    k *= 2;
  }
  return;
}
int main() {
  PRE();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  x[n] = 1145141919;
  cin >> r >> q;
  for (int i = 0; i < n; i++) {
    d[0][i] = upper_bound(x, x + n + 1, x[i] + r) - x;
    d[0][i]--;
  }
  d[0][n - 1] = n;
  for (int i = 1; i < 17; i++) {
    for (int j = 0; j < n; j++) {
      if (d[i - 1][j] < n)
        d[i][j] = d[i - 1][d[i - 1][j]];
      else
        d[i][j] = 114514;
    }
  }
  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 (true) {
      if (a >= b)
        break;
      int h, w;
      for (int j = 0; j < 17; j++) {
        if (d[j][a] <= b)
          h = d[j][a], w = Pow2[j];
      }
      ans += w;
      a = h;
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int n, q, r, x[100010];
int d[17][100010];
int Pow2[20];
void PRE() {
  int k = 1;
  for (int i = 0; i < 20; i++) {
    Pow2[i] = k;
    k *= 2;
  }
  return;
}
int main() {
  PRE();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  x[n] = 1145141919;
  cin >> r >> q;
  for (int i = 0; i < n; i++) {
    d[0][i] = upper_bound(x, x + n + 1, x[i] + r) - x;
    d[0][i]--;
  }
  d[0][n - 1] = n;
  for (int i = 1; i < 17; i++) {
    for (int j = 0; j < n; j++) {
      if (d[i - 1][j] < n)
        d[i][j] = d[i - 1][d[i - 1][j]];
      else
        d[i][j] = 114514;
    }
  }
  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 (true) {
      if (a >= b)
        break;
      int h = d[0][a], w = 1;
      for (int j = 1; j < 17; j++) {
        if (d[j][a] <= b)
          h = d[j][a], w = Pow2[j];
      }
      ans += w;
      a = h;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef vector<pi> vpi;
typedef set<ll> si;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define f first
#define s second
ll INF = 1e9;
ll MOD = 1e9;
#define lb lower_bound
#define ub upper_bound
#define SZ(x) (ll) x.size()
#define ALL(x) x.begin(), x.end()

int N, a, L, b, Q;
vi V;
int dst[100100][18];

int query(int x, int y) {
  int ans = 0;
  for (int i = 17; i >= 0; --i) {
    if (dst[x][i] == -1 || dst[x][i] > y)
      continue;
    x = dst[x][i];
    ans += (1 << i);
    if (x == y)
      return ans;
  }
  assert(x == y);
  return ans;
}

int main() {
  // freopen("in.txt","r",stdin);
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    V.pb(a);
  }
  V.pb(INF);
  cin >> L;
  memset(dst, -1, sizeof(dst));
  for (int i = 0; i < N; ++i) {
    int t = V[i] + L;
    int x = ub(ALL(V), t) - V.begin() - 1;
    if (i != x)
      dst[i][0] = x;
  }
  // for (int i=0;i<N;++i)cout<<dst[i][0]<<' ';cout<<'\n';
  for (int i = 1; i < 18; ++i) {
    for (int j = 0; j < N; ++j) {
      if (dst[j][i - 1] != -1)
        dst[j][i] = dst[dst[j][i - 1]][i - 1];
    }
  }
  // for (int i=0;i<4;++i){
  // for (int j=0;j<=N;++j)cout<<dst[j][i]<<' ';cout<<'\n';
  // }
  cin >> Q;
  while (Q--) {
    cin >> a >> b;
    --a;
    --b;
    if (a > b)
      swap(a, b);
    cout << query(a, b) << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef vector<pi> vpi;
typedef set<ll> si;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define f first
#define s second
ll INF = 1e9;
ll MOD = 1e9;
#define lb lower_bound
#define ub upper_bound
#define SZ(x) (ll) x.size()
#define ALL(x) x.begin(), x.end()

int N, a, L, b, Q;
vi V;
int dst[100100][18];

int query(int x, int y) {
  int ans = 0;
  for (int i = 17; i >= 0; --i) {
    if (dst[x][i] == -1 || dst[x][i] > y)
      continue;
    x = dst[x][i];
    ans += (1 << i);
    if (x == y)
      return ans;
  }
  return ans + 1;
}

int main() {
  // freopen("in.txt","r",stdin);
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    V.pb(a);
  }
  V.pb(INF);
  cin >> L;
  memset(dst, -1, sizeof(dst));
  for (int i = 0; i < N; ++i) {
    int t = V[i] + L;
    int x = ub(ALL(V), t) - V.begin() - 1;
    if (i != x)
      dst[i][0] = x;
  }
  // for (int i=0;i<N;++i)cout<<dst[i][0]<<' ';cout<<'\n';
  for (int i = 1; i < 18; ++i) {
    for (int j = 0; j < N; ++j) {
      if (dst[j][i - 1] != -1)
        dst[j][i] = dst[dst[j][i - 1]][i - 1];
    }
  }
  // for (int i=0;i<4;++i){
  // for (int j=0;j<=N;++j)cout<<dst[j][i]<<' ';cout<<'\n';
  // }
  cin >> Q;
  while (Q--) {
    cin >> a >> b;
    --a;
    --b;
    if (a > b)
      swap(a, b);
    cout << query(a, b) << '\n';
  }
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ALL(v) (v).begin(), (v).end()

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const ll inf = 1e18;

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

  ll n;
  cin >> n;
  vll x(n);
  REP(i, n) cin >> x[i];
  ll l;
  cin >> l;

  // reach[k][i] : i から出発して 2^k 日で到達できる一番遠いホテル
  vvll reach(31, vll(n));

  REP(i, n) {
    ll nxt = upper_bound(ALL(x), x[i] + l) - x.begin() - 1;
    reach[0][i] = nxt;
  }
  REP(k, 30) {
    REP(i, n) { reach[k + 1][i] = reach[k][reach[k][i]]; }
  }
  ll q;
  cin >> q;
  ll a, b;
  while (q) {
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    ll res = 0;
    REPR(k, 31) {
      if (reach[k + 1][a] < b) {
        res += (1 << k);
        a = reach[k][a];
      }
    }
    cout << res << endl;
    q--;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ALL(v) (v).begin(), (v).end()

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const ll inf = 1e18;

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

  ll n;
  cin >> n;
  vll x(n);
  REP(i, n) cin >> x[i];
  ll l;
  cin >> l;

  // reach[k][i] : i から出発して 2^k 日で到達できる一番遠いホテル
  vvll reach(31, vll(n));

  REP(i, n) {
    ll nxt = upper_bound(ALL(x), x[i] + l) - x.begin() - 1;
    reach[0][i] = nxt;
  }
  REP(k, 30) {
    REP(i, n) { reach[k + 1][i] = reach[k][reach[k][i]]; }
  }
  ll q;
  cin >> q;
  ll a, b;
  while (q) {
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    ll res = 1;
    REPR(k, 30) {
      if (reach[k][a] < b) {
        res += (1 << k);
        a = reach[k][a];
      }
    }
    cout << res << endl;
    q--;
  }
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 200003;

int n, L, a[MAXN], q, mx, ans, l, r;
int dp[MAXN][19]; // dp[i][j]表示从第i个点跳2^j次，最多能跳到的点的下标

void solve() {
  a[n + 1] = 0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) { // 二分找到第i个点跳一次最远能到达的点的下标
    dp[i][0] = lower_bound(a + 1, a + n + 2, a[i] + L + 1) - a - 1;
  }
  for (mx = 1; (1 << mx) <= n; ++mx) { // 更新dp[i][j]
    for (int i = 1; i <= n; ++i) {
      dp[i][mx] = dp[dp[i][mx - 1]][mx - 1];
    }
  }
  --mx;
  scanf("%d", &q);
  while (q-- > 0) {
    scanf("%d%d", &l, &r);
    if (l > r) {
      swap(l, r);
    }
    ans = 0;
    for (int i = mx; i >= 0; --i) { // 从权重最大的开始枚举
      if (dp[l][i] < r) {
        ans += 1 << i;
        l = dp[l][i];
      }
    }
    while (l < r) {
      ++ans;
      l = dp[l][0];
    }
    printf("%d\n", ans);
  }
}

int main() {
  while (1 == scanf("%d", &n)) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", a + i);
    }
    scanf("%d", &L);
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 200003;

int n, L, a[MAXN], q, mx, ans, l, r;
int dp[MAXN][19]; // dp[i][j]表示从第i个点跳2^j次，最多能跳到的点的下标

void solve() {
  a[n + 1] = 0x7f7f7f7f;
  for (int i = 1; i <= n; ++i) { // 二分找到第i个点跳一次最远能到达的点的下标
    dp[i][0] = lower_bound(a + 1, a + n + 2, a[i] + L + 1) - a - 1;
  }
  for (mx = 1; (1 << mx) <= n; ++mx) { // 更新dp[i][j]
    for (int i = 1; i <= n; ++i) {
      dp[i][mx] = dp[dp[i][mx - 1]][mx - 1];
    }
  }
  --mx;
  scanf("%d", &q);
  while (q-- > 0) {
    scanf("%d%d", &l, &r);
    if (l > r) {
      swap(l, r);
    }
    ans = 0;
    for (int i = mx; i >= 0; --i) { // 从权重最大的开始枚举
      if (dp[l][i] < r) {
        ans += 1 << i;
        l = dp[l][i];
      }
    }
    while (l < r) {
      ++ans;
      l = dp[l][0];
    }
    printf("%d\n", ans);
  }
}

int main() {
  while (1 == scanf("%d", &n)) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", a + i);
    }
    scanf("%d", &L);
    solve();
  }
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;

const int SIZE = 30;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> mov(n + 1, vector<int>(SIZE, 0));
  vector<ll> x(n);
  map<ll, int> pos;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    pos[x[i]] = i;
  }
  pos[1e9 + 1] = n;
  ll l;
  cin >> l;
  for (int i = 0; i < n + 1; i++)
    mov[i][0] = i;
  for (int i = 0; i < n + 1; i++) {
    if (i != n)
      mov[i][1] = (pos.upper_bound(x[i] + l)->second) - 1;
    else
      mov[i][1] = n;
  }
  for (int i = 1; i + 1 < SIZE; i++) {
    for (int j = 0; j < n + 1; j++) {
      if (j != n)
        mov[j][i + 1] = mov[mov[j][i]][i];
      else
        mov[j][i + 1] = n;
    }
  }
  int q;
  cin >> q;
  for (int loop = 0; loop < q; loop++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    int res = 0;
    while (true) {
      int lb = 0;
      int ub = SIZE;
      while (ub - lb > 1) {
        int mid = (lb + ub) / 2;
        int to = mov[a][mid];
        if (b <= to)
          ub = mid;
        else
          lb = mid;
      }
      if (lb == 0) {
        res++;
        break;
      }
      res += (1 << (lb - 1));
      a = mov[a][lb];
    }
    cout << res << endl;
  }
  return 0;
}

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

const int SIZE = 30;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> mov(n + 1, vector<int>(SIZE, 0));
  vector<ll> x(n);
  map<ll, int> pos;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    pos[x[i]] = i;
  }
  pos[1e10l] = n;
  ll l;
  cin >> l;
  for (int i = 0; i < n + 1; i++)
    mov[i][0] = i;
  for (int i = 0; i < n + 1; i++) {
    if (i != n)
      mov[i][1] = (pos.upper_bound(x[i] + l)->second) - 1;
    else
      mov[i][1] = n;
  }
  for (int i = 1; i + 1 < SIZE; i++) {
    for (int j = 0; j < n + 1; j++) {
      if (j != n)
        mov[j][i + 1] = mov[mov[j][i]][i];
      else
        mov[j][i + 1] = n;
    }
  }
  int q;
  cin >> q;
  for (int loop = 0; loop < q; loop++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (a > b)
      swap(a, b);
    int res = 0;
    while (true) {
      int lb = 0;
      int ub = SIZE;
      while (ub - lb > 1) {
        int mid = (lb + ub) / 2;
        int to = mov[a][mid];
        if (b <= to)
          ub = mid;
        else
          lb = mid;
      }
      if (lb == 0) {
        res++;
        break;
      }
      res += (1 << (lb - 1));
      a = mov[a][lb];
    }
    cout << res << endl;
  }
  return 0;
}
Problem p04017 (C++)

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

int a[1005];
int dp[1005][50];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int L, Q;
  scanf("%d%d", &L, &Q);
  for (int i = 1; i <= n; i++) {
    int id = upper_bound(a + 1, a + 1 + n, a[i] + L) - a - 1;
    if (a[i] + L >= a[n])
      dp[i][0] = n;
    else
      dp[i][0] = id;
  }

  for (int i = 1; i <= 30; i++) {
    for (int j = 1; j <= n; j++)
      dp[j][i] = dp[dp[j][i - 1]][i - 1];
  }
  while (Q--) {
    int L, R;
    scanf("%d%d", &L, &R);
    if (R < L)
      swap(L, R);
    long long ans = 0;
    for (int i = 30; i >= 0; i--) {
      if (dp[L][i] < R) {
        ans = ans + (1ll << i);
        L = dp[L][i];
      }
    }
    printf("%lld\n", ans + 1);
  }
  return 0;
}

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

int a[100005];
int dp[100005][50];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int L, Q;
  scanf("%d%d", &L, &Q);
  for (int i = 1; i <= n; i++) {
    int id = upper_bound(a + 1, a + 1 + n, a[i] + L) - a - 1;
    if (a[i] + L >= a[n])
      dp[i][0] = n;
    else
      dp[i][0] = id;
  }

  for (int i = 1; i <= 30; i++) {
    for (int j = 1; j <= n; j++)
      dp[j][i] = dp[dp[j][i - 1]][i - 1];
  }
  while (Q--) {
    int L, R;
    scanf("%d%d", &L, &R);
    if (R < L)
      swap(L, R);
    long long ans = 0;
    for (int i = 30; i >= 0; i--) {
      if (dp[L][i] < R) {
        ans = ans + (1ll << i);
        L = dp[L][i];
      }
    }
    printf("%lld\n", ans + 1);
  }
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#define RG register
using namespace std;
const int N = 1e5 + 10;
int n, l, q, cnt = 1, dp[N][35], x[N], pw[35] = {1}, ans;
template <typename I> inline void read(I &ot) {
  I ch = getchar(), x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  ot = x * f;
}
template <typename I, typename... U> inline void read(I &x, U &...y) {
  read(x);
  read(y...);
}
template <typename I> inline void swp(I &a, I &b) {
  I t = a;
  a = b;
  b = t;
}
int main() {
  //	freopen("Tak and Hotels.in","r",stdin);
  // freopen(".out","w",stdout);
  read(n);
  for (int i = 1; i <= n; i++)
    read(x[i]);
  read(l, q);
  for (int i = 1; i <= n; i++) {
    while (x[i] + l >= x[cnt])
      cnt++;
    if (cnt - 1 > n)
      continue;
    dp[i][0] = cnt - 1;
    //		printf("asd %d %d\n",i,dp[i][0]);
  }
  for (int i = 1; i <= 20; i++) {
    pw[i] = pw[i - 1] << 1;
    for (int j = 1; j <= n; j++) {
      if (dp[j][i - 1])
        dp[j][i] = dp[dp[j][i - 1]][i - 1];
    }
  }
  for (int i = 1, a, b; i <= q; i++) {
    read(a, b);
    if (a > b)
      swp(a, b);
    ans = 0;
    for (int j = 20; j >= 0; j--)
      if (dp[a][j] && dp[a][j] < b)
        a = dp[a][j], ans += pw[j];
    printf("%d\n", ans + 1);
  }
  // fclose(stdin); fclose(stdout);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#define RG register
using namespace std;
const int N = 1e5 + 10;
int n, l, q, cnt = 1, dp[N][35], x[N], pw[35] = {1}, ans;
template <typename I> inline void read(I &ot) {
  I ch = getchar(), x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  ot = x * f;
}
template <typename I, typename... U> inline void read(I &x, U &...y) {
  read(x);
  read(y...);
}
template <typename I> inline void swp(I &a, I &b) {
  I t = a;
  a = b;
  b = t;
}
int main() {
  //	freopen("Tak and Hotels.in","r",stdin);
  // freopen(".out","w",stdout);
  read(n);
  for (int i = 1; i <= n; i++)
    read(x[i]);
  read(l, q);
  /*for(int i=1;i<=n;i++)
  {
          while(x[i]+l>=x[cnt]) cnt++;
          if(cnt-1>n || cnt<=i) continue;
          dp[i][0]=cnt-1;
//		printf("asd %d %d\n",i,dp[i][0]);
  }*/
  for (int i = 1; i <= n; i++)
    while (x[i] > x[cnt] + l)
      dp[cnt++][0] = i - 1;
  for (int i = 1; i <= 20; i++) {
    pw[i] = pw[i - 1] << 1;
    for (int j = 1; j <= n; j++) {
      if (dp[j][i - 1])
        dp[j][i] = dp[dp[j][i - 1]][i - 1];
    }
  }
  for (int i = 1, a, b; i <= q; i++) {
    read(a, b);
    if (a > b)
      swp(a, b);
    ans = 0;
    for (int j = 20; j >= 0; j--)
      if (dp[a][j] && dp[a][j] < b)
        a = dp[a][j], ans += pw[j];
    printf("%d\n", ans + 1);
  }
  // fclose(stdin); fclose(stdout);
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  int l;
  cin >> l;
  int q;
  cin >> q;
  int d1[n + 1], d2[n + 1];
  for (int i = 0; i < n; i++) {
    int now = x[i];
    d1[i] = upper_bound(x.begin(), x.end(), x[i] + l) - x.begin() - 1;
  }
  d1[n - 1] = n;
  d1[n] = n;
  int h = pow(n, 0.5);
  // cout<<h;return 0;
  for (int i = 0; i < n; i++) {
    d2[i] = n;
    int num = i;
    for (int j = 0; j < h; j++) {
      num = d1[num];
    }
    d2[i] = num;
  }
  // for(int i=0;i<n;i++)cout<<d1[i]<<" "<<d2[i]<<endl;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    int c = 0;
    for (int j = a;;) {
      int num = d2[j];
      if (num > b) {
        int now = j;
        for (int k = j;; k++) {
          ans++;
          now = d1[now];
          if (now == b) {
            cout << ans << endl;
            c = 1;
            break;
          }
        }
      }
      ans += h;
      if (num == b) {
        cout << ans << endl;
        c = 1;
      }
      if (c == 1)
        break;
      j = num;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  int l;
  cin >> l;
  int q;
  cin >> q;
  int d1[n + 1], d2[n + 1];
  for (int i = 0; i < n; i++) {
    int now = x[i];
    d1[i] = upper_bound(x.begin(), x.end(), x[i] + l) - x.begin() - 1;
  }
  d1[n - 1] = n;
  d1[n] = n;
  int h = pow(n, 0.5);
  // cout<<h;return 0;
  for (int i = 0; i < n; i++) {
    d2[i] = n;
    int num = i;
    for (int j = 0; j < h; j++) {
      num = d1[num];
    }
    d2[i] = num;
  }
  // for(int i=0;i<n;i++)cout<<d1[i]<<" "<<d2[i]<<endl;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    int c = 0;
    for (int j = a;;) {
      int num = d2[j];
      if (num > b) {
        int now = j;
        for (int k = j;; k++) {
          ans++;
          now = d1[now];
          if (now >= b) {
            cout << ans << endl;
            c = 1;
            break;
          }
        }
      }
      ans += h;
      if (num == b) {
        cout << ans << endl;
        c = 1;
      }
      if (c == 1)
        break;
      j = num;
    }
  }
}