Problem p03112 (C++)

Buggy submission (status: Runtime Error):
// includes
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

bool comp(const Pl &a, const Pl &b) { return a.first < b.first; }

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int a, b, q;
  cin >> a >> b >> q;
  vector<ll> s(a), t(b);
  rep(i, a) cin >> s[i];
  rep(i, b) cin >> t[i];
  rep(i, q) {
    ll x;
    cin >> x;
    int ls = lower_bound(all(s), x) - s.begin() - 1;
    int rs = ls + 1;
    int lt = lower_bound(all(t), x) - t.begin() - 1;
    int rt = lt + 1;
    ll res = linf;
    if (ls >= 0 && lt >= 0) {
      res = min(res, x - min(s[ls], t[lt]));
    }
    if (ls >= 0 && rt < b) {
      res = min(res, min((ll)2 * (x - s[ls]) + t[rt] - x,
                         (ll)2 * (t[rt] - x) + x - s[ls]));
    }
    if (rs < a && lt >= 0) {
      res = min(res, min((ll)2 * (x - s[lt]) + t[rs] - x,
                         (ll)2 * (t[rs] - x) + x - s[lt]));
    }
    if (rs < a && rt < b) {
      res = min(res, max(s[rs], t[rt]) - x);
    }
    cout << res << endl;
  }
  return 0;
}


Fixed submission:
// includes
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

bool comp(const Pl &a, const Pl &b) { return a.first < b.first; }

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int a, b, q;
  cin >> a >> b >> q;
  vector<ll> s(a), t(b);
  rep(i, a) cin >> s[i];
  rep(i, b) cin >> t[i];
  rep(i, q) {
    ll x;
    cin >> x;
    int ls = lower_bound(all(s), x) - s.begin() - 1;
    int rs = ls + 1;
    int lt = lower_bound(all(t), x) - t.begin() - 1;
    int rt = lt + 1;
    ll res = linf;
    if (ls >= 0 && lt >= 0) {
      res = min(res, x - min(s[ls], t[lt]));
    }
    if (ls >= 0 && rt < b) {
      res = min(res, min((ll)2 * (x - s[ls]) + t[rt] - x,
                         (ll)2 * (t[rt] - x) + x - s[ls]));
    }
    if (rs < a && lt >= 0) {
      res = min(res, min((ll)2 * (x - t[lt]) + s[rs] - x,
                         (ll)2 * (s[rs] - x) + x - t[lt]));
    }
    if (rs < a && rt < b) {
      res = min(res, max(s[rs], t[rt]) - x);
    }
    cout << res << endl;
  }
  return 0;
}