Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
  int a, b, q;
  cin >> a >> b >> q;
  vector<long long> s(a + 4);
  vector<long long> t(b + 4);
  s[0] = t[0] = -LINF / 2;
  s[1] = t[1] = -LINF / 4;
  rep(i, a) cin >> s[i + 2];
  rep(i, b) cin >> t[i + 2];
  s[a + 2] = t[a + 2] = LINF / 4;
  s[a + 3] = t[a + 3] = LINF / 2;
  while (q--) {
    long long ans = LINF;
    long long x;
    cin >> x;
    // 神社先
    // 神社左
    {
      long long sh = *(lower_bound(all(s), x) - 1);
      long long te = LINF;
      // 寺左
      te = *(lower_bound(all(t), sh) - 1);
      ans = min(ans, abs(sh - x) + abs(sh - te));
      // 寺右
      te = *lower_bound(all(t), sh);
      ans = min(ans, abs(sh - x) + abs(sh - te));
    }
    // 神社右
    {
      long long sh = *lower_bound(all(s), x);
      long long te = LINF;
      // 寺左
      te = *(lower_bound(all(t), sh) - 1);
      ans = min(ans, abs(sh - x) + abs(sh - te));
      // 寺右
      te = *lower_bound(all(t), sh);
      ans = min(ans, abs(sh - x) + abs(sh - te));
    }
    // 寺先
    // 寺左
    {
      long long te = *(lower_bound(all(t), x) - 1);
      long long sh = LINF;
      // 神社左
      sh = *(lower_bound(all(s), te) - 1);
      ans = min(ans, abs(te - x) + abs(sh - te));
      // 神社右
      sh = *lower_bound(all(s), te);
      ans = min(ans, abs(te - x) + abs(sh - te));
    }
    // 寺右
    {
      long long te = *lower_bound(all(t), x);
      long long sh = LINF;
      // 神社左
      sh = *(lower_bound(all(s), te) - 1);
      ans = min(ans, abs(te - x) + abs(sh - te));
      // 神社右
      sh = *lower_bound(all(s), te);
      ans = min(ans, abs(te - x) + abs(sh - te));
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
  int a, b, q;
  cin >> a >> b >> q;
  vector<long long> s(a + 4);
  vector<long long> t(b + 4);
  s[0] = t[0] = -LINF / 2;
  s[1] = t[1] = -LINF / 4;
  rep(i, a) cin >> s[i + 2];
  rep(i, b) cin >> t[i + 2];
  s[a + 2] = t[b + 2] = LINF / 4;
  s[a + 3] = t[b + 3] = LINF / 2;
  while (q--) {
    long long ans = LINF;
    long long x;
    cin >> x;
    // 神社先
    // 神社左
    {
      long long sh = *(lower_bound(all(s), x) - 1);
      long long te = LINF;
      // 寺左
      te = *(lower_bound(all(t), sh) - 1);
      ans = min(ans, abs(sh - x) + abs(sh - te));
      // 寺右
      te = *lower_bound(all(t), sh);
      ans = min(ans, abs(sh - x) + abs(sh - te));
    }
    // 神社右
    {
      long long sh = *lower_bound(all(s), x);
      long long te = LINF;
      // 寺左
      te = *(lower_bound(all(t), sh) - 1);
      ans = min(ans, abs(sh - x) + abs(sh - te));
      // 寺右
      te = *lower_bound(all(t), sh);
      ans = min(ans, abs(sh - x) + abs(sh - te));
    }
    // 寺先
    // 寺左
    {
      long long te = *(lower_bound(all(t), x) - 1);
      long long sh = LINF;
      // 神社左
      sh = *(lower_bound(all(s), te) - 1);
      ans = min(ans, abs(te - x) + abs(sh - te));
      // 神社右
      sh = *lower_bound(all(s), te);
      ans = min(ans, abs(te - x) + abs(sh - te));
    }
    // 寺右
    {
      long long te = *lower_bound(all(t), x);
      long long sh = LINF;
      // 神社左
      sh = *(lower_bound(all(s), te) - 1);
      ans = min(ans, abs(te - x) + abs(sh - te));
      // 神社右
      sh = *lower_bound(all(s), te);
      ans = min(ans, abs(te - x) + abs(sh - te));
    }
    cout << ans << endl;
  }
  return 0;
}