Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFF
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")

#define PInt pair<int, int>
#define PLL pair<long long, long long>
#define VInt vector<int>
#define VLL vector<long long>
#define MatrixInt vector<vector<int>>
#define MatrixLL vector<vector<long long>>

typedef long long ll;
using namespace std;

int main() {

  int a, b, q;
  cin >> a >> b >> q;
  VLL s(a, 0), t(b, 0), x(q, 0);
  REP(i, a) cin >> s[i];
  REP(i, b) cin >> t[i];
  REP(i, q) cin >> x[i];

  sort(ALL_OF(s));
  sort(ALL_OF(t));

  REP(i, q) {

    int js = lower_bound(ALL_OF(s), x[i]) - s.begin();
    int jt = lower_bound(ALL_OF(t), x[i]) - t.begin();

    ll ans = 0x7FFFFFFFFFFFFFFFLL;
    chmin(ans, abs(s[js] - x[i]) + abs(s[js] - s[jt]));
    chmin(ans, abs(t[jt] - x[i]) + abs(s[js] - t[jt]));
    if (js > 0) {
      chmin(ans, abs(s[js - 1] - x[i]) + abs(s[js - 1] - t[jt]));
      chmin(ans, abs(t[jt] - x[i]) + abs(s[js - 1] - t[jt]));
    }
    if (jt > 0) {
      chmin(ans, abs(t[jt - 1] - x[i]) + abs(t[jt - 1] - s[js]));
      chmin(ans, abs(s[js] - x[i]) + abs(t[jt - 1] - s[js]));
    }
    if (js > 0 && jt > 0) {
      chmin(ans, abs(t[jt - 1] - x[i]) + abs(t[jt - 1] - s[js - 1]));
      chmin(ans, abs(s[js - 1] - x[i]) + abs(t[jt - 1] - s[js - 1]));
    }

    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFF
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")

#define PInt pair<int, int>
#define PLL pair<long long, long long>
#define VInt vector<int>
#define VLL vector<long long>
#define MatrixInt vector<vector<int>>
#define MatrixLL vector<vector<long long>>

typedef long long ll;
using namespace std;

int main() {

  int a, b, q;
  cin >> a >> b >> q;
  VLL s(a, 0), t(b, 0), x(q, 0);
  REP(i, a) cin >> s[i];
  REP(i, b) cin >> t[i];
  REP(i, q) cin >> x[i];

  sort(ALL_OF(s));
  sort(ALL_OF(t));

  REP(i, q) {

    int js = lower_bound(ALL_OF(s), x[i]) - s.begin();
    int jt = lower_bound(ALL_OF(t), x[i]) - t.begin();

    ll ans = 0x7FFFFFFFFFFFFFFFLL;
    chmin(ans, abs(s[js] - x[i]) + abs(s[js] - t[jt]));
    chmin(ans, abs(t[jt] - x[i]) + abs(s[js] - t[jt]));
    if (js > 0) {
      chmin(ans, abs(s[js - 1] - x[i]) + abs(s[js - 1] - t[jt]));
      chmin(ans, abs(t[jt] - x[i]) + abs(s[js - 1] - t[jt]));
    }
    if (jt > 0) {
      chmin(ans, abs(t[jt - 1] - x[i]) + abs(t[jt - 1] - s[js]));
      chmin(ans, abs(s[js] - x[i]) + abs(t[jt - 1] - s[js]));
    }
    if (js > 0 && jt > 0) {
      chmin(ans, abs(t[jt - 1] - x[i]) + abs(t[jt - 1] - s[js - 1]));
      chmin(ans, abs(s[js - 1] - x[i]) + abs(t[jt - 1] - s[js - 1]));
    }

    cout << ans << endl;
  }
}