Problem p03112 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef string str;
static const long long INF = INT64_MAX;
static const long long MOD = (ll)1e9 + 7;

#define endl "\n"
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define SZ(v) (ll)((v).size())
#define WHOLE(v) (v).begin(), (v).end()
#define ASC(v) sort((v).begin(), (v).end())
#define DESC(v) sort((v).rbegin(), (v).rend())
#define INV(v) reverse((v).begin(), (v).end())
#define FOLDL(src, dst, lambda)                                                \
  partial_sum((src).begin(), (src).end(), (dst).begin(), lambda)
#define FOLDR(src, dst, lambda)                                                \
  INV(src);                                                                    \
  partial_sum((src).begin(), (src).end(), (dst).begin(), lambda);              \
  INV(dst)
#define CUMSUML(src, dst) FOLDL(src, dst, [](auto a, auto b) { return a + b; })
#define CUMSUMR(src, dst) FOLDR(src, dst, [](auto a, auto b) { return a + b; })
#define ACCUM(v, d0, lambda) accumulate((v).begin(), (v).end(), d0, lambda)
#define SUM(v, d0) ACCUM(v, d0, [](auto a, auto b) { return a + b; })
#define ERASE(v, i) v.erase(v.begin() + i)
#define UNIQ(v)                                                                \
  ASC(v);                                                                      \
  v.erase(unique(v.begin(), v.end()), v.end())

template <typename T> void debug(T t) { cerr << t << endl; }
template <typename T, typename... Args> void debug(T t, Args... args) {
  cerr << t << " ";
  debug(args...);
}
template <typename T> void out(T t) { cout << t << endl; }
template <typename T, typename... Args> void out(T t, Args... args) {
  cout << t << " ";
  out(args...);
}

pll binsearch(ll x, vll v, ll n = 20) {
  ll lb = 0, ub = v.size() - 1;
  REP(i, n) {
    ll md = (lb + ub) / 2;
    if (x < v[md]) {
      ub = md;
    } else {
      lb = md;
    }
  }
  return {lb, ub};
}

void solve() {
  ll A, B, Q;
  cin >> A >> B >> Q;
  vll S(A), T(B);
  for (auto &a : S)
    cin >> a;
  for (auto &a : T)
    cin >> a;
  S.insert(S.begin() + 0, -1e17);
  S.push_back(1e17);
  T.insert(T.begin() + 0, -1e17);
  T.push_back(1e17);
  vll ans(Q);
  REP(i, Q) {
    ll x, s1, s2, t1, t2;
    cin >> x;
    pll ps = binsearch(x, S);
    pll pt = binsearch(x, T);
    s1 = S[ps.first];
    s2 = S[ps.first + 1];
    t1 = T[pt.first];
    t2 = T[pt.first + 1];
    vll a = {abs(x - s1) + abs(s1 - t1), abs(x - s1) + abs(s1 - t2),
             abs(x - t1) + abs(t1 - s1), abs(x - t1) + abs(t1 - s2),
             abs(x - s2) + abs(s2 - t1), abs(x - s2) + abs(s2 - t2),
             abs(x - t2) + abs(t2 - s1), abs(x - t2) + abs(t2 - s2)};
    ASC(a);
    ans[i] = a[0];
  }
  REP(i, Q) out(ans[i]);
}

int main() {
  fast_io;
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef string str;
static const long long INF = INT64_MAX;
static const long long MOD = (ll)1e9 + 7;

#define endl "\n"
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define SZ(v) (ll)((v).size())
#define WHOLE(v) (v).begin(), (v).end()
#define ASC(v) sort((v).begin(), (v).end())
#define DESC(v) sort((v).rbegin(), (v).rend())
#define INV(v) reverse((v).begin(), (v).end())
#define FOLDL(src, dst, lambda)                                                \
  partial_sum((src).begin(), (src).end(), (dst).begin(), lambda)
#define FOLDR(src, dst, lambda)                                                \
  INV(src);                                                                    \
  partial_sum((src).begin(), (src).end(), (dst).begin(), lambda);              \
  INV(dst)
#define CUMSUML(src, dst) FOLDL(src, dst, [](auto a, auto b) { return a + b; })
#define CUMSUMR(src, dst) FOLDR(src, dst, [](auto a, auto b) { return a + b; })
#define ACCUM(v, d0, lambda) accumulate((v).begin(), (v).end(), d0, lambda)
#define SUM(v, d0) ACCUM(v, d0, [](auto a, auto b) { return a + b; })
#define ERASE(v, i) v.erase(v.begin() + i)
#define UNIQ(v)                                                                \
  ASC(v);                                                                      \
  v.erase(unique(v.begin(), v.end()), v.end())

template <typename T> void debug(T t) { cerr << t << endl; }
template <typename T, typename... Args> void debug(T t, Args... args) {
  cerr << t << " ";
  debug(args...);
}
template <typename T> void out(T t) { cout << t << endl; }
template <typename T, typename... Args> void out(T t, Args... args) {
  cout << t << " ";
  out(args...);
}

pll binsearch(ll x, vll &v, ll n = 20) {
  ll lb = 0, ub = v.size() - 1;
  REP(i, n) {
    ll md = (lb + ub) / 2;
    if (x < v[md]) {
      ub = md;
    } else {
      lb = md;
    }
  }
  return {lb, ub};
}

void solve() {
  ll A, B, Q;
  cin >> A >> B >> Q;
  vll S(A), T(B);
  for (auto &a : S)
    cin >> a;
  for (auto &a : T)
    cin >> a;
  S.insert(S.begin() + 0, -1e17);
  S.push_back(1e17);
  T.insert(T.begin() + 0, -1e17);
  T.push_back(1e17);
  vll ans(Q);
  REP(i, Q) {
    ll x, s1, s2, t1, t2;
    cin >> x;
    pll ps = binsearch(x, S);
    pll pt = binsearch(x, T);
    s1 = S[ps.first];
    s2 = S[ps.first + 1];
    t1 = T[pt.first];
    t2 = T[pt.first + 1];
    vll a = {abs(x - s1) + abs(s1 - t1), abs(x - s1) + abs(s1 - t2),
             abs(x - t1) + abs(t1 - s1), abs(x - t1) + abs(t1 - s2),
             abs(x - s2) + abs(s2 - t1), abs(x - s2) + abs(s2 - t2),
             abs(x - t2) + abs(t2 - s1), abs(x - t2) + abs(t2 - s2)};
    ASC(a);
    ans[i] = a[0];
  }
  REP(i, Q) out(ans[i]);
}

int main() {
  fast_io;
  solve();
  return 0;
}