Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (long long x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
#define INF 10000000000000000
#define debug(x) cout << #x << ": " << x << endl

#define all(x) x.begin(), x.end()
typedef pair<long long, long long> P;
typedef pair<long long, P> PP;
long long a, b, q;
long long s[10007];
long long t[10007];
long long x[10007];
long long now;
long long ss, tt;
long long rs, rt;
long long ans;
int main() {

  cin >> a >> b >> q;
  rep(i, a) cin >> s[i];
  rep(i, b) cin >> t[i];
  rep(i, q) cin >> x[i];
  s[a] = INF;
  t[b] = INF;
  rep(i, q) {
    ans = INF;
    now = x[i];
    ss = (long long)(lower_bound(s, s + a + 1, now) - s);
    tt = (long long)(lower_bound(t, t + b + 1, now) - t);
    //
    if (ss != a && tt != b) {
      rs = ss;
      rt = tt;
      ans = min(ans, max(s[rs], t[rt]) - now);
    }
    //
    if (ss != 0 && tt != b) {
      rs = ss - 1;
      rt = tt;
      ans = min(ans, (now - s[rs]) + (t[rt] - now) +
                         min((now - s[rs]), (t[rt] - now)));
    }
    //
    if (ss != a && tt != 0) {
      rs = ss;
      rt = tt - 1;
      ans = min(ans, (now - t[rt]) + (s[rs] - now) +
                         min((now - t[rt]), (s[rs] - now)));
    }
    //
    if (ss != 0 && tt != 0) {
      rs = ss - 1;
      rt = tt - 1;
      ans = min(ans, now - min(s[rs], t[rt]));
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (long long x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
#define INF 10000000000000000
#define debug(x) cout << #x << ": " << x << endl

#define all(x) x.begin(), x.end()
typedef pair<long long, long long> P;
typedef pair<long long, P> PP;
long long a, b, q;
long long s[100007];
long long t[100007];
long long x[100007];
long long now;
long long ss, tt;
long long rs, rt;
long long ans;
int main() {

  cin >> a >> b >> q;
  rep(i, a) cin >> s[i];
  rep(i, b) cin >> t[i];
  rep(i, q) cin >> x[i];
  s[a] = INF;
  t[b] = INF;
  rep(i, q) {
    ans = INF;
    now = x[i];
    ss = (long long)(lower_bound(s, s + a + 1, now) - s);
    tt = (long long)(lower_bound(t, t + b + 1, now) - t);
    //
    if (ss != a && tt != b) {
      rs = ss;
      rt = tt;
      ans = min(ans, max(s[rs], t[rt]) - now);
    }
    //
    if (ss != 0 && tt != b) {
      rs = ss - 1;
      rt = tt;
      ans = min(ans, (now - s[rs]) + (t[rt] - now) +
                         min((now - s[rs]), (t[rt] - now)));
    }
    //
    if (ss != a && tt != 0) {
      rs = ss;
      rt = tt - 1;
      ans = min(ans, (now - t[rt]) + (s[rs] - now) +
                         min((now - t[rt]), (s[rs] - now)));
    }
    //
    if (ss != 0 && tt != 0) {
      rs = ss - 1;
      rt = tt - 1;
      ans = min(ans, now - min(s[rs], t[rt]));
    }
    cout << ans << endl;
  }
  return 0;
}