Problem p03112 (C++)

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

#define int long long
#define all(v) (v).begin(), (v).end()
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define uniqueV(x)                                                             \
  sort(x.begin(), x.end());                                                    \
  x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y)                                                             \
  cerr << "(" << #x << ", " << #y << ") = "                                    \
       << "(" << x << ", " << y << ")" << endl;
#define dbg3(x, y, z)                                                          \
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define dbgB(value, size) cerr << #value << ": " << bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int INF = 1e18;

int A, B, Q;
vector<int> s, t;

int bin(vector<int> v, int key) {
  auto check = [&](int mid) -> bool { return v[mid] <= key; };

  // [l, r)
  int l = 0;
  int r = v.size();

  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }

  return l;
}

int solve(int x) {
  // Right, Leftはxより右にあるか左にあるか
  // abs取るのは、値が見つからなかったときの処理をINFで統一させたいから(-INFあるとめんどい）
  int sRight = abs(*lower_bound(all(s), x));
  // int sLeft = abs(*(upper_bound(all(s), x) - 1));
  int sLeft = s[bin(s, x)];
  int tRight = abs(*lower_bound(all(t), x));
  // int tLeft = abs(*(upper_bound(all(t), x) - 1));
  int tLeft = t[bin(t, x)];

  int ret = INF;

  // (s, t) -> x -> (t, s)
  if (sLeft != INF and tLeft != INF) {
    chmin(ret, abs(x - min(sLeft, tLeft)));
    // dbg2(sLeft, tLeft)
  }

  // s -> x -> t {
  if (sLeft != INF and tRight != INF) {
    chmin(ret, min(abs(x - sLeft), abs(x - tRight)) + abs(sLeft - tRight));
    // dbg2(sLeft, tRight)
  }

  // t -> x -> s {
  if (tLeft != INF and sRight != INF) {
    chmin(ret, min(abs(x - tLeft), abs(x - sRight)) + abs(tLeft - sRight));
    // dbg2(tLeft, sRight)
  }

  // x -> (s, t) -> (t, s)
  if (sRight != INF and tRight != INF) {
    chmin(ret, abs(max(sRight, tRight) - x));
    // dbg2(sRight, tRight)
  }

  return ret;
}

signed main() {
  cin >> A >> B >> Q;
  s.resize(A);
  t.resize(B);
  for (int i = 0; i < A; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < B; i++) {
    cin >> t[i];
  }

  // 番兵を設置すると値が見つからなかったときの処理が楽になる
  s.push_back(INF);
  s.push_back(-INF);
  t.push_back(INF);
  t.push_back(-INF);
  sort(all(t));
  sort(all(s));

  while (Q--) {
    int x;
    cin >> x;
    int ans = solve(x);
    cout << ans << endl;
    // line()
  }

  return 0;
}

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

#define int long long
#define all(v) (v).begin(), (v).end()
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define uniqueV(x)                                                             \
  sort(x.begin(), x.end());                                                    \
  x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y)                                                             \
  cerr << "(" << #x << ", " << #y << ") = "                                    \
       << "(" << x << ", " << y << ")" << endl;
#define dbg3(x, y, z)                                                          \
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define dbgB(value, size) cerr << #value << ": " << bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int INF = 1e18;

int A, B, Q;
vector<int> s, t;

int bin(vector<int> &v, int key) {
  auto check = [&](int mid) -> bool { return v[mid] <= key; };

  // [l, r)
  int l = 0;
  int r = v.size();

  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }

  return l;
}

int solve(int x) {
  // Right, Leftはxより右にあるか左にあるか
  // abs取るのは、値が見つからなかったときの処理をINFで統一させたいから(-INFあるとめんどい）
  int sRight = abs(*lower_bound(all(s), x));
  // int sLeft = abs(*(upper_bound(all(s), x) - 1));
  int sLeft = s[bin(s, x)];
  int tRight = abs(*lower_bound(all(t), x));
  // int tLeft = abs(*(upper_bound(all(t), x) - 1));
  int tLeft = t[bin(t, x)];

  int ret = INF;

  // (s, t) -> x -> (t, s)
  if (sLeft != INF and tLeft != INF) {
    chmin(ret, abs(x - min(sLeft, tLeft)));
    // dbg2(sLeft, tLeft)
  }

  // s -> x -> t {
  if (sLeft != INF and tRight != INF) {
    chmin(ret, min(abs(x - sLeft), abs(x - tRight)) + abs(sLeft - tRight));
    // dbg2(sLeft, tRight)
  }

  // t -> x -> s {
  if (tLeft != INF and sRight != INF) {
    chmin(ret, min(abs(x - tLeft), abs(x - sRight)) + abs(tLeft - sRight));
    // dbg2(tLeft, sRight)
  }

  // x -> (s, t) -> (t, s)
  if (sRight != INF and tRight != INF) {
    chmin(ret, abs(max(sRight, tRight) - x));
    // dbg2(sRight, tRight)
  }

  return ret;
}

signed main() {
  cin >> A >> B >> Q;
  s.resize(A);
  t.resize(B);
  for (int i = 0; i < A; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < B; i++) {
    cin >> t[i];
  }

  // 番兵を設置すると値が見つからなかったときの処理が楽になる
  s.push_back(INF);
  s.push_back(-INF);
  t.push_back(INF);
  t.push_back(-INF);
  sort(all(t));
  sort(all(s));

  while (Q--) {
    int x;
    cin >> x;
    int ans = solve(x);
    cout << ans << endl;
    // line()
  }

  return 0;
}