Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repf(i, m, n) for (int(i) = m; (i) < n; (i)++)
#define all(v) (v).begin(), (v).end()
#define ll long long
#define vec(name, num) vector<ll> name((num), 0);
#define op(i) cout << (i) << endl;
#define ip(i) cin >> (i);
#define opN cout << "No" << endl;
#define opY cout << "Yes" << endl;
#define opP cout << "Possible" << endl;
#define opI cout << "Impossible" << endl;
#define mat(name, fnum, snum)                                                  \
  ;                                                                            \
  vector<vector<ll>> name((fnum), vector<ll>((snum), 0));
#define debugP                                                                 \
  int debug_point;                                                             \
  cin >> debug_point;
const ll MOD = 1e9 + 7;
template <typename T> void putv(vector<T> &V) {
  // cout << "The elements in the vector are: " << endl;
  for (auto x : V)
    cout << x << " ";
  cout << endl;
}
ll pown(ll fi, ll se) {
  ll ans = 1;
  rep(i, se) { ans *= fi; }
  return ans;
}
template <class T> vector<T> getv(ll n) {
  vector<T> Vector_temp;
  rep(i, n) {
    T input;
    cin >> input;
    Vector_temp.emplace_back(input);
  }
  return Vector_temp;
}
ll gcd(ll c, ll b) {
  while (1) {
    if (c % b != 0) {
      ll tmp = b;
      b = c % b;
      c = tmp;
    } else {
      return b;
    }
  }
}

/* <3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3*/

int main() {
  ll a, b, q;
  cin >> a >> b >> q;
  vec(s, a + 2);
  vec(t, b + 2);
  vec(x, q);
  repf(i, 1, a + 1) { cin >> s[i]; }
  repf(i, 1, b + 1) { cin >> t[i]; }
  s[0] = -10000000000;
  s[a + 1] = 30000000000;
  t[0] = -100000000000;
  t[b + 1] = 300000000000;
  rep(i, q) cin >> x[i];
  rep(i, q) {
    ll y[2];
    ll z[2];
    ll ans = LLONG_MAX;

    y[0] = *lower_bound(&s[0], &s[b + 2], x[i]);
    y[1] = *(lower_bound(&s[0], &s[b + 2], x[i]) - 1);
    z[0] = *lower_bound(&t[0], &t[b + 2], x[i]);
    z[1] = *(lower_bound(&t[0], &t[b + 2], x[i]) - 1);
    rep(j, 2) {
      rep(k, 2) {
        ans = min(ans, abs(y[j] - x[i]) + abs(z[k] - y[j]));
        ans = min(ans, abs(z[j] - x[i]) + abs(y[k] - z[j]));
      }
    }
    op(ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repf(i, m, n) for (int(i) = m; (i) < n; (i)++)
#define all(v) (v).begin(), (v).end()
#define ll long long
#define vec(name, num) vector<ll> name((num), 0);
#define op(i) cout << (i) << endl;
#define ip(i) cin >> (i);
#define opN cout << "No" << endl;
#define opY cout << "Yes" << endl;
#define opP cout << "Possible" << endl;
#define opI cout << "Impossible" << endl;
#define mat(name, fnum, snum)                                                  \
  ;                                                                            \
  vector<vector<ll>> name((fnum), vector<ll>((snum), 0));
#define debugP                                                                 \
  int debug_point;                                                             \
  cin >> debug_point;
const ll MOD = 1e9 + 7;
template <typename T> void putv(vector<T> &V) {
  // cout << "The elements in the vector are: " << endl;
  for (auto x : V)
    cout << x << " ";
  cout << endl;
}
ll pown(ll fi, ll se) {
  ll ans = 1;
  rep(i, se) { ans *= fi; }
  return ans;
}
template <class T> vector<T> getv(ll n) {
  vector<T> Vector_temp;
  rep(i, n) {
    T input;
    cin >> input;
    Vector_temp.emplace_back(input);
  }
  return Vector_temp;
}
ll gcd(ll c, ll b) {
  while (1) {
    if (c % b != 0) {
      ll tmp = b;
      b = c % b;
      c = tmp;
    } else {
      return b;
    }
  }
}

/* <3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3*/

int main() {
  ll a, b, q;
  cin >> a >> b >> q;
  vec(s, a + 2);
  vec(t, b + 2);
  vec(x, q);
  repf(i, 1, a + 1) { cin >> s[i]; }
  repf(i, 1, b + 1) { cin >> t[i]; }
  s[0] = -10000000000;
  s[a + 1] = 30000000000;
  t[0] = -100000000000;
  t[b + 1] = 300000000000;
  rep(i, q) cin >> x[i];
  rep(i, q) {
    ll y[2];
    ll z[2];
    ll ans = LLONG_MAX;

    y[0] = *lower_bound(&s[0], &s[a + 2], x[i]);
    y[1] = *(lower_bound(&s[0], &s[a + 2], x[i]) - 1);
    z[0] = *lower_bound(&t[0], &t[b + 2], x[i]);
    z[1] = *(lower_bound(&t[0], &t[b + 2], x[i]) - 1);
    rep(j, 2) {
      rep(k, 2) {
        ans = min(ans, abs(y[j] - x[i]) + abs(z[k] - y[j]));
        ans = min(ans, abs(z[j] - x[i]) + abs(y[k] - z[j]));
      }
    }
    op(ans);
  }
  return 0;
}