Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double
#define pii pair<int, int>
#define pll pair<LL, LL>
#define vi vector<int>
#define vii vector<vi>

const double pi = 3.14159265358979323846264;
const int mod = 1000000007;
LL d(LL a, LL b, LL c) { return abs(b - a) + abs(c - b); }

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  // std::cout << std::fixed;
  // std::cout << std::setprecision(12);
  // std::cout << std::defaultfloat;
  int a, b, q;
  cin >> a >> b >> q;
  vector<LL> s(a + 3);
  vector<LL> t(b + 3);
  s[0] = t[0] = -mod * 10001LL;
  s[1] = t[1] = -mod * 10000LL;
  s[a + 2] = t[b + 2] = mod * 10000LL;
  s[a + 3] = t[b + 3] = mod * 10001LL;
  FOR(i, 0, a) { cin >> s[i + 2]; }
  FOR(j, 0, b) { cin >> t[j + 2]; }
  FOR(i, 0, q) {
    LL x;
    cin >> x;
    auto si = lower_bound(s.begin(), s.end(), x) - s.begin();
    auto ti = lower_bound(t.begin(), t.end(), x) - t.begin();
    if (s[si] == x) {
      cout << min(abs(x - t[ti]), abs(x - t[ti - 1])) << endl;
    } else if (t[ti] == x) {
      cout << min(abs(x - s[si]), abs(x - s[si - 1])) << endl;
    } else {
      cout << min(min(min(d(x, s[si], t[ti]), d(x, s[si - 1], t[ti - 1])),
                      min(d(x, s[si - 1], t[ti]), d(x, s[si], t[ti - 1]))),
                  min(min(d(x, t[ti], s[si]), d(x, t[ti], s[si - 1])),
                      min(d(x, t[ti - 1], s[si]), d(x, t[ti - 1], s[si - 1]))))
           << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double
#define pii pair<int, int>
#define pll pair<LL, LL>
#define vi vector<int>
#define vii vector<vi>

const double pi = 3.14159265358979323846264;
const int mod = 1000000007;
LL d(LL a, LL b, LL c) { return abs(b - a) + abs(c - b); }

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  // std::cout << std::fixed;
  // std::cout << std::setprecision(12);
  // std::cout << std::defaultfloat;
  int a, b, q;
  cin >> a >> b >> q;
  vector<LL> s(a + 4);
  vector<LL> t(b + 4);
  s[0] = t[0] = -mod * 10001LL;
  s[1] = t[1] = -mod * 10000LL;
  s[a + 2] = t[b + 2] = mod * 10000LL;
  s[a + 3] = t[b + 3] = mod * 10001LL;
  FOR(i, 0, a) { cin >> s[i + 2]; }
  FOR(j, 0, b) { cin >> t[j + 2]; }
  FOR(i, 0, q) {
    LL x;
    cin >> x;
    auto si = lower_bound(s.begin(), s.end(), x) - s.begin();
    auto ti = lower_bound(t.begin(), t.end(), x) - t.begin();
    if (s[si] == x) {
      cout << min(abs(x - t[ti]), abs(x - t[ti - 1])) << endl;
    } else if (t[ti] == x) {
      cout << min(abs(x - s[si]), abs(x - s[si - 1])) << endl;
    } else {
      cout << min(min(min(d(x, s[si], t[ti]), d(x, s[si - 1], t[ti - 1])),
                      min(d(x, s[si - 1], t[ti]), d(x, s[si], t[ti - 1]))),
                  min(min(d(x, t[ti], s[si]), d(x, t[ti], s[si - 1])),
                      min(d(x, t[ti - 1], s[si]), d(x, t[ti - 1], s[si - 1]))))
           << endl;
    }
  }

  return 0;
}