Problem p03112 (C++)

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

using namespace std;

using ll = long long;

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

  std::vector<ll> s(A), t(B), x(Q);
  std::for_each(s.begin(), s.end(), [](auto &x) { cin >> x; });
  std::for_each(t.begin(), t.end(), [](auto &x) { cin >> x; });
  std::for_each(x.begin(), x.end(), [](auto &x) { cin >> x; });

  std::pair<ll, ll> s_nearest, t_nearest;
  for (const auto &e : x) {
    const auto si = std::find_if(s.cbegin(), s.cend(),
                                 [&e](const auto i) { return i > e; });
    const auto ti = std::find_if(t.cbegin(), t.cend(),
                                 [&e](const auto i) { return i > e; });

    if (si == s.begin()) {
      s_nearest = std::make_pair(-1ll << 60, *s.cbegin());
    } else if (si == s.cend()) {
      s_nearest = std::make_pair(*(s.cend() - 1), 1ll << 60);
    } else {
      s_nearest = std::make_pair(*(si - 1), *si);
    }

    if (ti == t.begin()) {
      t_nearest = std::make_pair(-1ll << 60, *t.cbegin());
    } else if (ti == t.cend()) {
      t_nearest = std::make_pair(*(t.cend() - 1), 1ll << 60);
    } else {
      t_nearest = std::make_pair(*(ti - 1), *ti);
    }

    ll ans = std::numeric_limits<ll>::max();
    ans = std::min(ans, std::abs(s_nearest.first - e) +
                            std::abs(t_nearest.first - s_nearest.first));
    ans = std::min(ans, std::abs(s_nearest.first - e) +
                            std::abs(t_nearest.second - s_nearest.first));
    ans = std::min(ans, std::abs(s_nearest.second - e) +
                            std::abs(t_nearest.first - s_nearest.second));
    ans = std::min(ans, std::abs(s_nearest.second - e) +
                            std::abs(t_nearest.second - s_nearest.second));
    ans = std::min(ans, std::abs(t_nearest.first - e) +
                            std::abs(s_nearest.first - t_nearest.first));
    ans = std::min(ans, std::abs(t_nearest.first - e) +
                            std::abs(s_nearest.second - t_nearest.first));
    ans = std::min(ans, std::abs(t_nearest.second - e) +
                            std::abs(s_nearest.first - t_nearest.second));
    ans = std::min(ans, std::abs(t_nearest.second - e) +
                            std::abs(s_nearest.second - t_nearest.second));

    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

using ll = long long;

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

  std::vector<ll> s(A), t(B), x(Q);
  std::for_each(s.begin(), s.end(), [](auto &x) { cin >> x; });
  std::for_each(t.begin(), t.end(), [](auto &x) { cin >> x; });
  std::for_each(x.begin(), x.end(), [](auto &x) { cin >> x; });

  std::pair<ll, ll> s_nearest, t_nearest;
  for (const auto &e : x) {
    const auto si = std::lower_bound(s.cbegin(), s.cend(), e);
    const auto ti = std::lower_bound(t.cbegin(), t.cend(), e);

    if (si == s.begin()) {
      s_nearest = std::make_pair(-1ll << 60, *s.cbegin());
    } else if (si == s.cend()) {
      s_nearest = std::make_pair(*(s.cend() - 1), 1ll << 60);
    } else {
      s_nearest = std::make_pair(*(si - 1), *si);
    }

    if (ti == t.begin()) {
      t_nearest = std::make_pair(-1ll << 60, *t.cbegin());
    } else if (ti == t.cend()) {
      t_nearest = std::make_pair(*(t.cend() - 1), 1ll << 60);
    } else {
      t_nearest = std::make_pair(*(ti - 1), *ti);
    }

    ll ans = std::numeric_limits<ll>::max();
    ans = std::min(ans, std::abs(s_nearest.first - e) +
                            std::abs(t_nearest.first - s_nearest.first));
    ans = std::min(ans, std::abs(s_nearest.first - e) +
                            std::abs(t_nearest.second - s_nearest.first));
    ans = std::min(ans, std::abs(s_nearest.second - e) +
                            std::abs(t_nearest.first - s_nearest.second));
    ans = std::min(ans, std::abs(s_nearest.second - e) +
                            std::abs(t_nearest.second - s_nearest.second));
    ans = std::min(ans, std::abs(t_nearest.first - e) +
                            std::abs(s_nearest.first - t_nearest.first));
    ans = std::min(ans, std::abs(t_nearest.first - e) +
                            std::abs(s_nearest.second - t_nearest.first));
    ans = std::min(ans, std::abs(t_nearest.second - e) +
                            std::abs(s_nearest.first - t_nearest.second));
    ans = std::min(ans, std::abs(t_nearest.second - e) +
                            std::abs(s_nearest.second - t_nearest.second));

    cout << ans << endl;
  }

  return 0;
}