Language: C++

#include <iostream>
#define INF 1000000000000000
using namespace std;

int search_index(long long x, long long* A, int n) {
  // return i s.t. A[i] < x < A[i+1]
  // if x < A[0] return -1
  // if A[n-1] < x return n
  int left, right, mid;
  left = 0; right = n-1;
  while (true) {
    mid = (left + right) / 2;
    if (A[mid] < x && x < A[mid+1]) return mid;
    if (A[mid] < x) {
      left = mid+1;
    } else {
      right = mid-1;
    }
  }
}

long long min(long long a, long long b) {
  if (a < b) return a;
  return b;
}

long long abs(long long a) {
  if (a < 0) return -a;
  return a;
}

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;

  long long S[A+2], T[B+2], x;
  for (int i=1; i<A+1; i++) cin >> S[i];
  for (int i=1; i<B+1; i++) cin >> T[i];
  S[A+1] = T[B+1] = INF;
  S[0] = T[0] = -INF;

  int A_i, B_i;
  long long a[2], b[2], ans;
  for (int i=0; i<Q; i++) {
    ans = INF;
    cin >> x;
    A_i = search_index(x, S, A+2);
    B_i = search_index(x, T, B+2);
    a[0] = S[A_i]; a[1] = S[A_i+1];
    b[0] = T[B_i]; b[1] = T[B_i+1];
    for (int j=0; j<2; j++) {
      for (int k=0; k<2; k++) {
        long long d1 = abs(a[j] - x) + abs(b[k] - a[j]);
        long long d2 = abs(b[k] - x) + abs(a[j] - b[k]);
        ans = min(ans, min(d1, d2));
      }
    }
    cout << ans << endl;
  }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:53:27: error: call of overloaded 'abs(long long int)' is ambiguous
   53 |         long long d1 = abs(a[j] - x) + abs(b[k] - a[j]);
      |                        ~~~^~~~~~~~~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:27:11: note: candidate: 'long long int abs(long long int)'
   27 | long long abs(long long a) {
      |           ^~~
In file included from /usr/include/c++/14/cstdlib:81:
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:53:43: error: call of overloaded 'abs(long long int)' is ambiguous
   53 |         long long d1 = abs(a[j] - x) + abs(b[k] - a[j]);
      |                                        ~~~^~~~~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:27:11: note: candidate: 'long long int abs(long long int)'
   27 | long long abs(long long a) {
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:54:27: error: call of overloaded 'abs(long long int)' is ambiguous
   54 |         long long d2 = abs(b[k] - x) + abs(a[j] - b[k]);
      |                        ~~~^~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:27:11: note: candidate: 'long long int abs(long long int)'
   27 | long long abs(long long a) {
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:54:43: error: call of overloaded 'abs(long long int)' is ambiguous
   54 |         long long d2 = abs(b[k] - x) + abs(a[j] - b[k]);
      |                                        ~~~^~~~~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:27:11: note: candidate: 'long long int abs(long long int)'
   27 | long long abs(long long a) {
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~