Problem p03112 (C++)

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

const long long INF = 20000000001;

void Main() {
  int a, b, q;
  cin >> a >> b >> q;
  vector<long long> s, t;
  for (int i = 0; i < a; i++) {
    long long tmp;
    cin >> tmp;
    s.push_back(tmp);
  }
  for (int i = 0; i < b; i++) {
    long long tmp;
    cin >> tmp;
    t.push_back(tmp);
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  for (int i = 0; i < q; i++) {
    long long x;
    cin >> x;
    int sl = -1, sr = s.size();
    while (sr - sl > 1) {
      int c = (sl + sr) / 2;
      if (x <= s[c])
        sr = c;
      else
        sl = c;
    }

    int tl = -1, tr = s.size();
    while (tr - tl > 1) {
      int c = (tl + tr) / 2;
      if (x <= t[c])
        tr = c;
      else
        tl = c;
    }

    long long ans = INF;
    if (sr < s.size() && tr < t.size()) {
      ans = min(ans, abs(s[sr] - x) + abs(t[tr] - s[sr]));
    }
    if (sr < s.size() && tr > 0) {
      ans = min(ans, abs(s[sr] - x) + abs(t[tr - 1] - s[sr]));
    }
    if (sr > 0 && tr < t.size()) {
      ans = min(ans, abs(s[sr - 1] - x) + abs(t[tr] - s[sr - 1]));
    }
    if (sr > 0 && tr > 0) {
      ans = min(ans, abs(s[sr - 1] - x) + abs(t[tr - 1] - s[sr - 1]));
    }
    if (sr < s.size() && tr < t.size()) {
      ans = min(ans, abs(t[tr] - x) + abs(s[sr] - t[tr]));
    }
    if (tr < t.size() && sr > 0) {
      ans = min(ans, abs(t[tr] - x) + abs(s[sr - 1] - t[tr]));
    }
    if (tr > 0 && sr < s.size()) {
      ans = min(ans, abs(t[tr - 1] - x) + abs(s[sr] - t[tr - 1]));
    }
    if (tr > 0 && sr > 0) {
      ans = min(ans, abs(t[tr - 1] - x) + abs(s[sr - 1] - t[tr - 1]));
    }
    cout << ans << endl;
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


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

const long long INF = 20000000001;

void Main() {
  int a, b, q;
  cin >> a >> b >> q;
  vector<long long> s, t;
  for (int i = 0; i < a; i++) {
    long long tmp;
    cin >> tmp;
    s.push_back(tmp);
  }
  for (int i = 0; i < b; i++) {
    long long tmp;
    cin >> tmp;
    t.push_back(tmp);
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  for (int i = 0; i < q; i++) {
    long long x;
    cin >> x;
    int sl = -1, sr = s.size();
    while (sr - sl > 1) {
      int c = (sl + sr) / 2;
      if (x <= s[c])
        sr = c;
      else
        sl = c;
    }

    int tl = -1, tr = t.size();
    while (tr - tl > 1) {
      int c = (tl + tr) / 2;
      if (x <= t[c])
        tr = c;
      else
        tl = c;
    }

    long long ans = INF;
    if (sr < s.size() && tr < t.size()) {
      ans = min(ans, abs(s[sr] - x) + abs(t[tr] - s[sr]));
    }
    if (sr < s.size() && tr > 0) {
      ans = min(ans, abs(s[sr] - x) + abs(t[tr - 1] - s[sr]));
    }
    if (sr > 0 && tr < t.size()) {
      ans = min(ans, abs(s[sr - 1] - x) + abs(t[tr] - s[sr - 1]));
    }
    if (sr > 0 && tr > 0) {
      ans = min(ans, abs(s[sr - 1] - x) + abs(t[tr - 1] - s[sr - 1]));
    }
    if (sr < s.size() && tr < t.size()) {
      ans = min(ans, abs(t[tr] - x) + abs(s[sr] - t[tr]));
    }
    if (tr < t.size() && sr > 0) {
      ans = min(ans, abs(t[tr] - x) + abs(s[sr - 1] - t[tr]));
    }
    if (tr > 0 && sr < s.size()) {
      ans = min(ans, abs(t[tr - 1] - x) + abs(s[sr] - t[tr - 1]));
    }
    if (tr > 0 && sr > 0) {
      ans = min(ans, abs(t[tr - 1] - x) + abs(s[sr - 1] - t[tr - 1]));
    }
    cout << ans << endl;
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}