Problem p03112 (C++)

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

#define BR "\n"
#define SP " "
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << SP;                                                        \
  }                                                                            \
  cout << BR;
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << BR;

int main() {
  ll A, B, Q;
  cin >> A >> B >> Q;

  ll INF = 1e11;

  vector<ll> s(A + 2);
  vector<ll> t(B + 2);

  for (int i = 1; i <= A; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= B; i++) {
    cin >> t[i];
  }

  s[0] = -INF, t[0] = -INF;
  s[A + 1] = INF, t[A + 1] = INF;

  for (int i = 0; i < Q; i++) {
    ll x;
    cin >> x;

    int l, r, m;
    l = -1, r = A + 1, m;
    while (l + 1 < r) {
      m = (l + r) / 2;
      if (s[m] <= x) {
        l = m;
      } else {
        r = m;
      }
    }
    int la = l, ra = r;
    // cout << SP << la << SP << ra << BR;

    l = -1, r = B + 1, m;
    while (l + 1 < r) {
      m = (l + r) / 2;
      if (t[m] <= x) {
        l = m;
      } else {
        r = m;
      }
    }
    int lb = l, rb = r;
    // cout << SP << lb << SP << rb << BR;

    ll al = abs(x - s[la]), ar = abs(x - s[ra]);
    ll bl = abs(x - t[lb]), br = abs(x - t[rb]);

    ll ans = min(max(al, bl), max(ar, br));
    ans = min(ans, min(al, ar) * 2 + min(bl, br));
    ans = min(ans, min(al, ar) + min(bl, br) * 2);
    cout << ans << BR;
  }
  return 0;
}

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

#define BR "\n"
#define SP " "
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << SP;                                                        \
  }                                                                            \
  cout << BR;
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << BR;

int main() {
  ll A, B, Q;
  cin >> A >> B >> Q;

  ll INF = 1e11;

  vector<ll> s(A + 2);
  vector<ll> t(B + 2);

  for (int i = 1; i <= A; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= B; i++) {
    cin >> t[i];
  }

  s[0] = -INF, t[0] = -INF;
  s[A + 1] = INF, t[B + 1] = INF;

  for (int i = 0; i < Q; i++) {
    ll x;
    cin >> x;

    int l, r, m;
    l = -1, r = A + 1, m;
    while (l + 1 < r) {
      m = (l + r) / 2;
      if (s[m] <= x) {
        l = m;
      } else {
        r = m;
      }
    }
    int la = l, ra = r;
    // cout << SP << la << SP << ra << BR;

    l = -1, r = B + 1, m;
    while (l + 1 < r) {
      m = (l + r) / 2;
      if (t[m] <= x) {
        l = m;
      } else {
        r = m;
      }
    }
    int lb = l, rb = r;
    // cout << SP << lb << SP << rb << BR;

    ll al = abs(x - s[la]), ar = abs(x - s[ra]);
    ll bl = abs(x - t[lb]), br = abs(x - t[rb]);

    ll ans = min(max(al, bl), max(ar, br));
    ans = min(ans, min(al, ar) * 2 + min(bl, br));
    ans = min(ans, min(al, ar) + min(bl, br) * 2);
    cout << ans << BR;
  }
  return 0;
}