Language: C++

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long

int main(){
  int a,b,q;
  cin >> a >> b >> q;
  ll x[a],y[b],z[q];
  for(int i=0;i<a;i++){
    cin >> x[i];
  }
  for(int i=0;i<b;i++){
    cin >> y[i];
  }
  for(int i=0;i<q;i++){
    cin >> z[i];
  }
  sort(x,x+a);
  sort(y,y+b);
  for(int i=0;i<q;i++){
    ll c,d;
    if(z[i]>=x[0]&&z[i]>=y[0]){
      auto pp=lower_bound(x[0],x[a],z[i]);
      auto ppp=lower_bound(y[0],y[b],z[i]);
      c=pp-x[0];
      d=ppp-y[0]; 
    }
    else if(z[i]<x[0]){
      c=0;
    }
    else{
      d=0;
    }
    ll len=abs(x[0]-z[i])+abs(y[0]-z[i]);
    if(c!=0&&d!=0){
      len=min({abs(x[c-1]-z[i])+abs(y[d-1]-z[i]),abs(x[c-1]-z[i])+abs(y[d]-z[i]),abs(x[c]-z[i])+abs(y[d-1]-z[i]),abs(x[c]-z[i])+abs(y[d]-z[i])});
    }
    else if(c==0){
      len=min(abs(x[c]-z[i])+abs(y[d-1]-z[i]),abs(x[c]-z[i])+abs(y[d]-z[i]));
    }
    else{
      len=min(abs(x[c-1]-z[i])+abs(y[d]-z[i]),abs(x[c]-z[i])+abs(y[d]-z[i]));
    }
    cout << len << endl;
  }
}

Compiler output:
In file included from /usr/include/c++/14/string:51,
                 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/c++/14/bits/stl_algobase.h: In instantiation of '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = long long int; _Tp = long long int; _Compare = __gnu_cxx::__ops::_Iter_less_val]':
/usr/include/c++/14/bits/stl_algobase.h:1539:32:   required from '_ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = long long int; _Tp = long long int]'
 1539 |       return std::__lower_bound(__first, __last, __val,
      |              ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
 1540 |                                 __gnu_cxx::__ops::__iter_less_val());
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:26:   required from here
   25 |       auto pp=lower_bound(x[0],x[a],z[i]);
      |               ~~~~~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1495:9: error: no type named 'difference_type' in 'struct std::iterator_traits<long long int>'
 1495 |         _DistanceType;
      |         ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1497:42: error: no matching function for call to 'distance(long long int&, long long int&)'
 1497 |       _DistanceType __len = std::distance(__first, __last);
      |                             ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:47:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note: candidate: 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator)'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h: In substitution of 'template<class _InputIterator> constexpr typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::distance(_InputIterator, _InputIterator) [with _InputIterator = long long int]':
/usr/include/c++/14/bits/stl_algobase.h:1497:42:   required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = long long int; _Tp = long long int; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
 1497 |       _DistanceType __len = std::distance(__first, __last);
      |                             ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1539:32:   required from '_ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = long long int; _Tp = long long int]'
 1539 |       return std::__lower_bound(__first, __last, __val,
      |              ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
 1540 |                                 __gnu_cxx::__ops::__iter_less_val());
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:26:   required from here
   25 |       auto pp=lower_bound(x[0],x[a],z[i]);
      |               ~~~~~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:148:5: error: no type named 'difference_type' in 'struct std::iterator_traits<long long int>'
  148 |     distance(_InputIterator __first, _InputIterator __last)
      |     ^~~~~~~~