Problem p03112 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000

long long calc(vector<long long> s, vector<long long> t, long long x) {
  vector<long long>::iterator sRight =
      lower_bound(s.begin(), s.end(), x); // x[i]以上が最初に現れる要素
  vector<long long>::iterator sLeft;
  if (sRight != s.begin()) {
    sLeft = sRight - 1;
  } else {
    sLeft = sRight;
  }
  vector<long long>::iterator tRight = lower_bound(t.begin(), t.end(), x);
  vector<long long>::iterator tLeft;
  if (tRight != t.begin()) {
    tLeft = tRight - 1;
  } else {
    tLeft = tRight;
  }
  long long tmp = min({
      (long long)abs(*sRight - x) + (long long)abs(*sRight - *tRight),
      (long long)abs(*sRight - x) + (long long)abs(*sRight - *tLeft),
      (long long)abs(*sLeft - x) + (long long)abs(*sLeft - *tRight),
      (long long)abs(*sLeft - x) + (long long)abs(*sLeft - *tLeft),
      (long long)abs(*tRight - x) + (long long)abs(*tRight - *sRight),
      (long long)abs(*tRight - x) + (long long)abs(*tRight - *sLeft),
      (long long)abs(*tLeft - x) + (long long)abs(*tLeft - *sRight),
      (long long)abs(*tLeft - x) + (long long)abs(*tLeft - *sLeft),
  });
  return tmp;
}

int main() {
  long long A, B, Q;
  cin >> A >> B >> Q;
  vector<long long> s(A), t(B), x(Q);
  vector<long long> ans(Q);
  for (long long i = 0; i < s.size(); i++) {
    cin >> s[i];
  }
  for (long long i = 0; i < t.size(); i++) {
    cin >> t[i];
  }
  for (long long i = 0; i < x.size(); i++) {
    cin >> x[i];
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  for (long long i = 0; i < ans.size(); i++) {
    ans[i] = calc(s, t, x[i]);
  }
  for (long long i = 0; i < ans.size(); i++) {
    std::cout << ans[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000

long long calc(vector<long long> &s, vector<long long> &t, long long x) {
  vector<long long>::iterator sRight =
      lower_bound(s.begin(), s.end(), x); // x[i]以上が最初に現れる要素
  vector<long long>::iterator sLeft;
  if (sRight != s.begin()) {
    sLeft = sRight - 1;
  } else {
    sLeft = sRight;
  }
  vector<long long>::iterator tRight = lower_bound(t.begin(), t.end(), x);
  vector<long long>::iterator tLeft;
  if (tRight != t.begin()) {
    tLeft = tRight - 1;
  } else {
    tLeft = tRight;
  }
  long long tmp = min({
      (long long)abs(*sRight - x) + (long long)abs(*sRight - *tRight),
      (long long)abs(*sRight - x) + (long long)abs(*sRight - *tLeft),
      (long long)abs(*sLeft - x) + (long long)abs(*sLeft - *tRight),
      (long long)abs(*sLeft - x) + (long long)abs(*sLeft - *tLeft),
      (long long)abs(*tRight - x) + (long long)abs(*tRight - *sRight),
      (long long)abs(*tRight - x) + (long long)abs(*tRight - *sLeft),
      (long long)abs(*tLeft - x) + (long long)abs(*tLeft - *sRight),
      (long long)abs(*tLeft - x) + (long long)abs(*tLeft - *sLeft),
  });
  return tmp;
}

int main() {
  long long A, B, Q;
  cin >> A >> B >> Q;
  vector<long long> s(A), t(B), x(Q);
  vector<long long> ans(Q);
  for (long long i = 0; i < s.size(); i++) {
    cin >> s[i];
  }
  for (long long i = 0; i < t.size(); i++) {
    cin >> t[i];
  }
  for (long long i = 0; i < x.size(); i++) {
    cin >> x[i];
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  for (long long i = 0; i < ans.size(); i++) {
    ans[i] = calc(s, t, x[i]);
  }
  for (long long i = 0; i < ans.size(); i++) {
    std::cout << ans[i] << endl;
  }
}