Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18 + 1;

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A, 0), t(B, 0);
  for (int i = 0; i < A; i++)
    cin >> s[i];
  for (int i = 0; i < B; i++)
    cin >> t[i];
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  for (int i = 0; i < Q; i++) {
    ll x;
    cin >> x;
    ll ans = LLINF;

    int sp = lower_bound(s.begin(), s.end(), x) - s.begin();
    int tp = lower_bound(t.begin(), t.end(), x) - t.begin();
    vector<int> S, T;
    if (sp < A)
      S.push_back(sp);
    if (tp < B)
      T.push_back(tp);
    if (sp > 0)
      S.push_back(sp - 1);
    if (tp > 0)
      T.push_back(tp - 1);
    for (auto ss : S) {
      tp = lower_bound(t.begin(), t.end(), s[ss]) - t.begin();
      if (tp < B)
        T.push_back(tp);
      if (tp > 0)
        T.push_back(sp);
    }
    for (auto tt : T) {
      sp = lower_bound(s.begin(), s.end(), t[tt]) - s.begin();
      if (sp < A)
        S.push_back(sp);
      if (sp > 0)
        S.push_back(sp - 1);
    }
    for (auto ss : S)
      for (auto tt : T) {
        ans = min(ans, abs(x - s[ss]) + abs(s[ss] - t[tt]));
      }
    for (auto tt : T)
      for (auto ss : S) {
        ans = min(ans, abs(x - t[tt]) + abs(t[tt] - s[ss]));
      }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18 + 1;

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A, 0), t(B, 0);
  for (int i = 0; i < A; i++)
    cin >> s[i];
  for (int i = 0; i < B; i++)
    cin >> t[i];
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  for (int i = 0; i < Q; i++) {
    ll x;
    cin >> x;
    ll ans = LLINF;

    int sp = lower_bound(s.begin(), s.end(), x) - s.begin();
    int tp = lower_bound(t.begin(), t.end(), x) - t.begin();
    vector<int> S, T;
    if (sp < A)
      S.push_back(sp);
    if (tp < B)
      T.push_back(tp);
    if (sp > 0)
      S.push_back(sp - 1);
    if (tp > 0)
      T.push_back(tp - 1);
    for (auto ss : S) {
      tp = lower_bound(t.begin(), t.end(), s[ss]) - t.begin();
      if (tp < B)
        T.push_back(tp);
      if (tp > 0)
        T.push_back(tp - 1);
    }
    for (auto tt : T) {
      sp = lower_bound(s.begin(), s.end(), t[tt]) - s.begin();
      if (sp < A)
        S.push_back(sp);
      if (sp > 0)
        S.push_back(sp - 1);
    }
    for (auto ss : S)
      for (auto tt : T) {
        ans = min(ans, abs(x - s[ss]) + abs(s[ss] - t[tt]));
      }
    for (auto tt : T)
      for (auto ss : S) {
        ans = min(ans, abs(x - t[tt]) + abs(t[tt] - s[ss]));
      }
    cout << ans << endl;
  }
  return 0;
}