Language: C++

#include <bits/stdc++.h>

using namespace std;

int r,c;

int sd(pair<int,int> a){
    int x,y;
    tie(x,y) = a;
    if(x==0)
        return 0;
    if(y==c)
        return 1;
    if(x==r)
        return 2;
    if(y==0)
        return 3;
    return -1;
}

struct cmpar{
    bool operator()(const pair<pair<int,int>,int>& a, const pair<pair<int,int>,int>& b){
        if(sd(a.first)!=sd(b.first))
            return sd(a.first)<sd(b.first);
        if(sd(a.first)<2)
            return a.first<b.first;
        return a.first>b.first;
    }
}bcmp;

int main(){
    scanf("%d%d",&r,&c);
    int n;
    scanf("%d",&n);
    vector<pair<pair<int,int>,int>> xvec;
    for(int i = 0; i<n; i++){
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if(sd({x1,y1})!=-1&&sd({x2,y2})!=-1){
            xvec.push_back({{x1,y1},i});
            xvec.push_back({{x2,y2},i});
        }
    }
    sort(xvec.begin(),xvec.end(),bcmp);
    int xsz = xvec.size();
    for(int i = 0; i<xsz; i++)
        xvec.push_back(xvec[i]);
    xsz+=xsz;
    stack<int> ist;
    vector<int> xc(n,0);
    bool failed = false;
    for(int i = 0;i<xsz; i++){
        if(!ist.empty()&&ist.top()==xvec[i].second){
            xc[xvec[i].second]--;
            ist.pop();
        }else if(!xc[xvec[i].second]){
            xc[xvec[i].second]++;
            ist.push(xvec[i].second);
        }else{
            failed = true;
            break;
        }
    }
    if(failed)
        printf("NO\n");
    else
        printf("YES\n");
    return 0;
}

Compiler output:
a.cc:29:2: error: 'cmpar bcmp' redeclared as different kind of entity
   29 | }bcmp;
      |  ^~~~
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/strings.h:34:12: note: previous declaration 'int bcmp(const void*, const void*, size_t)'
   34 | extern int bcmp (const void *__s1, const void *__s2, size_t __n)
      |            ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::pair<std::pair<int, int>, int>*, std::vector<std::pair<std::pair<int, int>, int> > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::pair<std::pair<int, int>, int>*, std::vector<std::pair<std::pair<int, int>, int> > >; _Compare = int (*)(const void*, const void*, long unsigned int) noexcept]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = int (*)(const void*, const void*, long unsigned int) noexcept]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:44:9:   required from here
   44 |     sort(xvec.begin(),xvec.end(),bcmp);
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: cannot convert 'std::pair<std::pair<int, int>, int>' to 'const void*' in argument passing
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = std::pair<std::pair<int, int>, int>; _Iterator = __gnu_cxx::__normal_iterator<std::pair<std::pair<int, int>, int>*, std::vector<std::pair<std::pair<int, int>, int> > >; _Compare = int (*)(const void*, const void*, long unsigned int) noexcept]':
/usr/include/c++/14/bits/stl_algo.h:1757:20:   required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1757 |       while (__comp(__val, __next))
      |              ~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1785:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1785 |             std::__unguarded_linear_insert(__i,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
 1786 |                                 __gnu_cxx::__ops::__val_comp_iter(__comp));
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = int (*)(const void*, const void*, long unsigned int) noexcept]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:44:9:   required from here
   44 |     sort(xvec.begin(),xvec.end(),bcmp);
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:240:30: error: cannot convert 'std::pair<std::pair<int, int>, int>' to 'const void*' in argument passing
  240 |         { return bool(_M_comp(__val, *__it)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<std::pair<std::pair<int, int>, int>*, std::vector<std::pair<std::pair<int, int>, int> > >; _Value = std::pair<std::pair<int, int>, int>; _Compare = int (*)(const void*, const void*, long unsigned int) noexcept]':
/usr/include/c++/14/bits/stl_heap.h:140:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Distance = long int; _Tp = pair<pair<int, int>, int>; _Compare = __gnu_cxx::__ops::_Iter_comp_val<int (*)(const void*, const void*, long unsigned int) noexcept>]'
  140 |       while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
      |                                          ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:247:23:   required from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Distance = long int; _Tp = pair<pair<int, int>, int>; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
  247 |       std::__push_heap(__first, __holeIndex, __topIndex,
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  248 |                        _GLIBCXX_MOVE(__value), __cmp);
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:356:22:   required from 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
  356 |           std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
      |           ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  357 |                              __comp);
      |                              ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1593:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const void*, const void*, long unsigned int) noexcept>]'
 1593 |       std::__make_heap(__first, __middle, __comp);
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<pair<int, int>, int>*, vector<pair<pair<int, int>, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(c