Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int A, B, Q;
  cin >> A >> B >> Q;

  vs64 sh(A);
  rep(i, A) cin >> sh[i];
  vs64 te(B);
  rep(i, B) cin >> te[i];
  vs64 x(Q);
  rep(i, Q) cin >> x[i];

  sort(sh.begin(), sh.end());
  sort(te.begin(), te.end());

  vector<pair<s64, s64>> s2t(A);
  rep(i, A) {
    auto it = lower_bound(te.begin(), te.end(), sh[i]);
    if (it == te.begin()) {
      s2t[i] = make_pair(sh[i], abs(sh[i] - te[0]));
    } else if (it == te.end()) {
      s2t[i] = make_pair(sh[i], abs(sh[i] - te[B - 1]));
    } else {
      int temp = MIN(abs(sh[i] - *it), abs(sh[i] - *(it - 1)));
      s2t[i] = make_pair(sh[i], temp);
    }
  }

  vector<pair<s64, s64>> t2s(B);
  rep(i, B) {
    auto it = lower_bound(sh.begin(), sh.end(), te[i]);
    if (it == sh.begin()) {
      t2s[i] = make_pair(te[i], abs(te[i] - sh[0]));
    } else if (it == sh.end()) {
      t2s[i] = make_pair(te[i], abs(te[i] - sh[A - 1]));
    } else {
      int temp = MIN(abs(te[i] - *it), abs(te[i] - *(it - 1)));
      t2s[i] = make_pair(te[i], temp);
    }
  }

  // cout << "s2t\n";
  // rep (i, A)
  // {
  //     cout << "s, dis = " << s2t[i].first << ", " << s2t[i].second << "\n";
  // }
  // cout << "t2s\n";
  // rep (i, A)
  // {
  //     cout << "t, dis = " << t2s[i].first << ", " << t2s[i].second << "\n";
  // }

  s64 ans;
  rep(i, Q) {
    auto its =
        lower_bound(s2t.begin(), s2t.end(), pair<s64, s64>(x[i], -1),
                    [](const pair<s64, s64> &xx, const pair<s64, s64> &yy) {
                      return xx.first < yy.first;
                    });
    s64 temps;
    if (its == s2t.begin())
      temps = abs(x[i] - its->first) + its->second;
    else if (its == s2t.end())
      temps = abs(x[i] - s2t[A - 1].first) + s2t[A - 1].second;
    else {
      temps = MIN(abs(x[i] - its->first) + its->second,
                  abs(x[i] - (its - 1)->first) + (its - 1)->second);
    }

    auto itt =
        lower_bound(t2s.begin(), t2s.end(), pair<s64, s64>(x[i], -1),
                    [](const pair<s64, s64> &xx, const pair<s64, s64> &yy) {
                      return xx.first < yy.first;
                    });
    s64 tempt;
    if (itt == t2s.begin())
      tempt = abs(x[i] - itt->first) + itt->second;
    else if (itt == t2s.end())
      tempt = abs(x[i] - t2s[A - 1].first) + t2s[A - 1].second;
    else {
      tempt = MIN(abs(x[i] - itt->first) + itt->second,
                  abs(x[i] - (itt - 1)->first) + (itt - 1)->second);
    }
    // cout << "Q" << i << ": " << temps << ", " << tempt << "\n";
    ans = MIN(temps, tempt);
    cout << ans << "\n";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int A, B, Q;
  cin >> A >> B >> Q;

  vs64 sh(A);
  rep(i, A) cin >> sh[i];
  vs64 te(B);
  rep(i, B) cin >> te[i];
  vs64 x(Q);
  rep(i, Q) cin >> x[i];

  sort(sh.begin(), sh.end());
  sort(te.begin(), te.end());

  vector<pair<s64, s64>> s2t(A);
  rep(i, A) {
    auto it = lower_bound(te.begin(), te.end(), sh[i]);
    if (it == te.begin()) {
      s2t[i] = make_pair(sh[i], abs(sh[i] - te[0]));
    } else if (it == te.end()) {
      s2t[i] = make_pair(sh[i], abs(sh[i] - te[B - 1]));
    } else {
      int temp = MIN(abs(sh[i] - *it), abs(sh[i] - *(it - 1)));
      s2t[i] = make_pair(sh[i], temp);
    }
  }

  vector<pair<s64, s64>> t2s(B);
  rep(i, B) {
    auto it = lower_bound(sh.begin(), sh.end(), te[i]);
    if (it == sh.begin()) {
      t2s[i] = make_pair(te[i], abs(te[i] - sh[0]));
    } else if (it == sh.end()) {
      t2s[i] = make_pair(te[i], abs(te[i] - sh[A - 1]));
    } else {
      int temp = MIN(abs(te[i] - *it), abs(te[i] - *(it - 1)));
      t2s[i] = make_pair(te[i], temp);
    }
  }

  // cout << "s2t\n";
  // rep (i, A)
  // {
  //     cout << "s, dis = " << s2t[i].first << ", " << s2t[i].second << "\n";
  // }
  // cout << "t2s\n";
  // rep (i, A)
  // {
  //     cout << "t, dis = " << t2s[i].first << ", " << t2s[i].second << "\n";
  // }

  s64 ans;
  rep(i, Q) {
    auto its =
        lower_bound(s2t.begin(), s2t.end(), pair<s64, s64>(x[i], -1),
                    [](const pair<s64, s64> &xx, const pair<s64, s64> &yy) {
                      return xx.first < yy.first;
                    });
    s64 temps;
    if (its == s2t.begin())
      temps = abs(x[i] - its->first) + its->second;
    else if (its == s2t.end())
      temps = abs(x[i] - s2t[A - 1].first) + s2t[A - 1].second;
    else {
      temps = MIN(abs(x[i] - its->first) + its->second,
                  abs(x[i] - (its - 1)->first) + (its - 1)->second);
    }

    auto itt =
        lower_bound(t2s.begin(), t2s.end(), pair<s64, s64>(x[i], -1),
                    [](const pair<s64, s64> &xx, const pair<s64, s64> &yy) {
                      return xx.first < yy.first;
                    });
    s64 tempt;
    if (itt == t2s.begin())
      tempt = abs(x[i] - itt->first) + itt->second;
    else if (itt == t2s.end())
      tempt = abs(x[i] - t2s[B - 1].first) + t2s[B - 1].second;
    else {
      tempt = MIN(abs(x[i] - itt->first) + itt->second,
                  abs(x[i] - (itt - 1)->first) + (itt - 1)->second);
    }
    // cout << "Q" << i << ": " << temps << ", " << tempt << "\n";
    ans = MIN(temps, tempt);
    cout << ans << "\n";
  }

  return 0;
}