Problem p03112 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
const ll mod = ll(1e9) + 7;
const ll INF = ll(1e18);
ll hoge(ll cost, ll x, vector<ll> &v) {
  auto it = lower_bound(v.begin(), v.end(), x);
  int idx = it - v.begin();
  ll cost3, cost4;
  cost3 = INF, cost4 = INF;
  if (idx < v.size()) {
    cost4 = cost + abs(x - v[idx]);
  }
  if (idx > 0) {
    cost3 = cost + abs(x - v[idx - 1]);
  }
  return min(cost3, cost4);
}
int main() {
  cin.sync_with_stdio(false);
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A);
  vector<ll> t(B);
  rep(i, A) cin >> s[i];
  rep(i, B) cin >> t[i];
  vector<ll> ans;
  rep(i, Q) {
    ll x;
    cin >> x;
    auto it = lower_bound(s.begin(), s.end(), x);
    int idx = it - s.begin();
    ll cost1 = INF, cost2 = INF;
    ll x1 = INF, x2 = INF;
    if (idx < s.size()) {
      cost2 = abs(x - s[idx]);
      x2 = s[idx];
    }
    if (idx > 0) {
      cost1 = abs(x - s[idx - 1]);
      x1 = s[idx - 1];
    }
    ll cost = min(hoge(cost1, x1, t), hoge(cost2, x2, t));

    it = lower_bound(t.begin(), t.end(), x);
    cost1 = INF, cost2 = INF;
    x1 = INF, x2 = INF;
    if (idx < t.size()) {
      cost2 = abs(x - t[idx]);
      x2 = t[idx];
    }
    if (idx > 0) {
      cost1 = abs(x - t[idx - 1]);
      x1 = t[idx - 1];
    }

    ans.push_back(min({cost, hoge(cost1, x1, s), hoge(cost2, x2, s)}));
  }
  rep(i, Q) cout << ans[i] << endl;

  return 0;
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
const ll mod = ll(1e9) + 7;
const ll INF = ll(1e18);
ll hoge(ll cost, ll x, vector<ll> &v) {
  auto it = lower_bound(v.begin(), v.end(), x);
  int idx = it - v.begin();
  ll cost3, cost4;
  cost3 = INF, cost4 = INF;
  if (idx < v.size()) {
    cost4 = cost + abs(x - v[idx]);
  }
  if (idx > 0) {
    cost3 = cost + abs(x - v[idx - 1]);
  }
  return min(cost3, cost4);
}
int main() {
  cin.sync_with_stdio(false);
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A);
  vector<ll> t(B);
  rep(i, A) cin >> s[i];
  rep(i, B) cin >> t[i];
  vector<ll> ans;
  rep(i, Q) {
    ll x;
    cin >> x;
    auto it = lower_bound(s.begin(), s.end(), x);
    int idx = it - s.begin();
    ll cost1 = INF, cost2 = INF;
    ll x1 = INF, x2 = INF;
    if (idx < s.size()) {
      cost2 = abs(x - s[idx]);
      x2 = s[idx];
    }
    if (idx > 0) {
      cost1 = abs(x - s[idx - 1]);
      x1 = s[idx - 1];
    }
    ll cost = min(hoge(cost1, x1, t), hoge(cost2, x2, t));

    it = lower_bound(t.begin(), t.end(), x);
    idx = it - t.begin();
    cost1 = INF, cost2 = INF;
    x1 = INF, x2 = INF;
    if (idx < t.size()) {
      cost2 = abs(x - t[idx]);
      x2 = t[idx];
    }
    if (idx > 0) {
      cost1 = abs(x - t[idx - 1]);
      x1 = t[idx - 1];
    }

    ans.push_back(min({cost, hoge(cost1, x1, s), hoge(cost2, x2, s)}));
  }
  rep(i, Q) cout << ans[i] << endl;

  return 0;
}