Problem p03112 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<long long> s(A);
  vector<long long> t(B);
  vector<long long> x(Q);

  for (auto i = 0; i < A; ++i) {
    cin >> s[i];
  }
  for (auto i = 0; i < B; ++i) {
    cin >> t[i];
  }
  for (auto i = 0; i < Q; ++i) {
    cin >> x[i];
  }

  auto search = [](vector<long long> list, long long start, long long &west,
                   long long &east) {
    auto last = list.size() - 1;
    if (start < list[0]) {
      west = -1;
      east = list[0];
      return;
    }
    if (start > list[last]) {
      west = list[last];
      east = -1;
      return;
    }

    auto iter = lower_bound(list.begin(), list.end(), start);
    west = *(iter - 1);
    east = *iter;
  };

  for (auto i = 0; i < Q; ++i) {
    long long sWest, sEast, tWest, tEast;
    auto start = x[i];
    search(s, start, sWest, sEast);
    search(t, start, tWest, tEast);

    long long len = 1e11;
    if (sWest >= 0) {
      if (tWest >= 0) {
        len = min(len, start - min(sWest, tWest));
      }
      if (tEast >= 0) {
        len = min(len, tEast - sWest + min(tEast - start, start - sWest));
      }
    }
    if (sEast >= 0) {
      if (tWest >= 0) {
        len = min(len, sEast - tWest + min(sEast - start, start - tWest));
      }
      if (tEast >= 0) {
        len = min(len, max(sEast, tEast) - start);
      }
    }

    cout << len << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<long long> s(A);
  vector<long long> t(B);
  vector<long long> x(Q);

  for (auto i = 0; i < A; ++i) {
    cin >> s[i];
  }
  for (auto i = 0; i < B; ++i) {
    cin >> t[i];
  }
  for (auto i = 0; i < Q; ++i) {
    cin >> x[i];
  }

  auto search = [](vector<long long> &list, long long start, long long &west,
                   long long &east) {
    auto last = list.size() - 1;
    if (start < list[0]) {
      west = -1;
      east = list[0];
      return;
    }
    if (start > list[last]) {
      west = list[last];
      east = -1;
      return;
    }

    auto iter = lower_bound(list.begin(), list.end(), start);
    west = *(iter - 1);
    east = *iter;
  };

  for (auto i = 0; i < Q; ++i) {
    long long sWest, sEast, tWest, tEast;
    auto start = x[i];
    search(s, start, sWest, sEast);
    search(t, start, tWest, tEast);

    long long len = 1e11;
    if (sWest >= 0) {
      if (tWest >= 0) {
        len = min(len, start - min(sWest, tWest));
      }
      if (tEast >= 0) {
        len = min(len, tEast - sWest + min(tEast - start, start - sWest));
      }
    }
    if (sEast >= 0) {
      if (tWest >= 0) {
        len = min(len, sEast - tWest + min(sEast - start, start - tWest));
      }
      if (tEast >= 0) {
        len = min(len, max(sEast, tEast) - start);
      }
    }

    cout << len << endl;
  }

  return 0;
}