Language: C++

#ifndef Local
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/STACK:1024000000,1024000000")
#endif

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
typedef long long Long;

typedef long double Double;
const Double EPS = 1e-10;

bool areEqual(Double x, Double y, Double eps = EPS) {
  auto diff = abs(x - y);
  if (diff < eps) return true;
  if (min(x, y) < eps) return false;
  return diff / max(x, y) < eps;
}

bool isZero(Double x, Double eps = EPS) {
  return abs(x) <= eps;
}

struct Point {
  Double x = 0, y = 0;
  Point(Double x, Double y) :
    x(x), y(y) {
  }
  Point() {
  }
  Point operator +(const Point &p) const {
    return Point { x + p.x, y + p.y };
  }

  Point operator -(const Point &p) const {
    return Point { x - p.x, y - p.y };
  }
  Point operator *(Double c) const {
    return Point(x * c, y * c);
  }
  Point operator /(Double c) {
    return Point(x / c, y / c);
  }
  bool operator<(const Point &p) const {
    return areEqual(x, p.x) ? (y < p.y) : (x < p.x);
  }
  bool operator>(const Point& p) const {
    return areEqual(x, p.x) ? (y > p.y) : (x > p.x);
  }
  bool operator==(const Point &p) const {
    return areEqual(x, p.x) && areEqual(y, p.y);
  }
  bool operator!=(const Point& p) const {
    return !(*this == p);
  }
};

Double dot(Point p, Point q) {
  return p.x * q.x + p.y * q.y;
}

Double dist2(Point p, Point q) {
  return dot(p - q, p - q);
}

Double cross(Point p, Point q) {
  return p.x * q.y - p.y * q.x;
}

Point norm(Point x, Double l) {
  return x * sqrt(l * l / dot(x, x));
}

istream &operator>>(istream &is, Point &p) {
  return is >> p.x >> p.y;
}

Point RotateCCW90(Point p) {
  return Point(-p.y, p.x);
}

Point RotateCCW(Point p, Double t) {
  return Point(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t));
}

// project point c onto line through a and b (assuming a != b)
Point ProjectPointLine(Point a, Point b, Point c) {
  return a + (b - a) * dot(c - a, b - a) / dot(b - a, b - a);
}

// project point c onto line segment through a and b (assuming a != b)
Point ProjectPointSegment(Point a, Point b, Point c) {
  Double r = dot(c - a, b - a), d = dot(b - a, b - a);
  if (r < 0) return a;
  if (r > d) return b;
  return a + (b - a) * r / d;
}

Point reflectAroundLine(Point p, Point a, Point b) {
  return ProjectPointLine(p, a, b) * 2 - p;
}

bool LinesParallel(const Point& a, const Point& b, const Point& c,
  const Point& d) {
  return isZero(cross(b - a, c - d));
}

bool LinesCollinear(const Point& a, const Point& b, const Point& c,
  const Point& d) {
  return LinesParallel(a, b, c, d) && isZero(cross(b - c, a - c));
}

// Returns 1 if CW, 0 if collinear, -1 if ACW.
int getDirection(const Point& a, const Point& b, const Point& c) {
  auto value = cross(c - a, b - a);
  if (isZero(value)) return 0;
  return 2 * (value > 0) - 1;
}

// Checks if c lies in the range [a,b].
bool checkInRange(Double a, Double b, Double c, Double eps = EPS) {
  if (areEqual(a, c)) return true;
  if (areEqual(b, c)) return true;
  return a <= c && c <= b;
}

// Assuming that c already lies on the straight line ab,
// returns true if c lies on the segment ab.
bool onSegment(const Point& a, const Point& b, const Point& c) {
  return checkInRange(a.x, b.x, c.x) && checkInRange(a.y, b.y, c.y);
}

// Determine if a to b intersects with c to d.
bool SegmentsIntersect(const Point& a, const Point& b, const Point& c,
  const Point& d) {
  // Handles collinear cases.
  auto d1 = getDirection(c, d, a);
  auto d2 = getDirection(c, d, b);
  auto d3 = getDirection(a, b, c);
  auto d4 = getDirection(a, b, d);

  // tmp
  if (LinesCollinear(a, b, c, d)
    && (onSegment(a, b, c) && onSegment(a, b, d)
      || onSegment(c, d, a) && onSegment(c, d, b))) {
    return false;
  }

  if (d1 == 0 && onSegment(c, d, a)) return true;
  if (d2 == 0 && onSegment(c, d, b)) return true;
  if (d3 == 0 && onSegment(a, b, c)) return true;
  if (d4 == 0 && onSegment(a, b, d)) return true;
  if (d1 == 0 || d2 == 0 || d3 == 0 || d4 == 0) return false;
  return (((d1 > 0) == (d2 < 0)) && ((d3 > 0) == (d4 < 0)));
}

// ST Line ab intersect ST Line cd assuming unique intersection exists
// for line segments, check if segments intersect first
Point ComputeLineIntersection(Point a, Point b, Point c, Point d) {
  b = b - a;
  d = c - d;
  c = c - a;
  return a + b * cross(c, d) / cross(b, d); // cross(b,d) != 0
}

typedef array<Point, 2> Segment;

struct Event {
  int x, type, y, id;

  tuple<int, int, int, int> getTuple() const {
    return make_tuple(x, type, y, id);
  }

  bool operator <(const Event& other) const {
    return getTuple() < other.getTuple();
  }

  Event(int x, int type, int y_end, int id) :
    x(x), type(type), y(y_end), id(id) {
  }
};

struct SetComparator {
  static vector<Segment> segments;
  bool operator()(int a, int b) const {
    if (segments[a][0].x == segments[b][0].x) {
      return segments[a][0].y < segments[b][0].y;
    }
    if (segments[a][0].y == segments[a][1].y
      && segments[b][0].y == segments[b][1].y
      && segments[a][0].y == segments[b][0].y) {
      return segments[a][0].x < segments[b][0].x;
    }
    if (segments[a][0] > segments[b][0]) {
      int dir = getDirection(segments[b][0], segments[b][1], segments[a][0]);
      return dir > 0;
    }
    int dir = getDirection(segments[a][0], segments[a][1], segments[b][0]);
    return dir < 0;
  }
};

vector<Segment> SetComparator::segments;

bool noIntersection(const vector<Segment>& segments) {
  int n = segments.size();
  vector<Event> events;
  for (int i = 0; i < n; ++i) {
    events.emplace_back(segments[i][0].x, segments[i][0] > segments[i][1],
      segments[i][0].y, i);
    events.emplace_back(segments[i][1].x, segments[i][0] < segments[i][1],
      segments[i][1].y, i);
  }

  sort(events.begin(), events.end());

  SetComparator::segments = segments;
  set<int, SetComparator> st;

  for (auto& event : events) {
    if (event.type == 0) {
      auto nxt = st.upper_bound(event.id);
      if (nxt != st.end()) {
        if (SegmentsIntersect(segments[event.id][0], segments[event.id][1],
          segments[*nxt][0], segments[*nxt][1])) return false;
      }

      if (nxt != st.begin()) {
        --nxt;
        if (SegmentsIntersect(segments[event.id][0], segments[event.id][1],
          segments[*nxt][0], segments[*nxt][1])) return false;
      }

      st.insert(event.id);

    } else {
      st.erase(event.id);
      auto nxt = st.upper_bound(event.id);
      if (nxt != st.end() && nxt != st.begin()) {
        auto before = nxt;
        --before;
        if (SegmentsIntersect(segments[*before][0], segments[*before][1],
          segments[*nxt][0], segments[*nxt][1])) {
          return false;
        }
      }
    }
  }

  return true;
}

bool onBorder(const Point& p, int r, int c) {
  return isZero(p.x) || isZero(p.y) || areEqual(p.x, r) || areEqual(p.y, c);
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  int r, c, n;
  cin >> r >> c >> n;

  vector<Segment> segments;

  while (n--) {
    Segment seg;
    cin >> seg[0] >> seg[1];
    sort(seg , seg + 2);
    if (onBorder(seg[0], r, c) && onBorder(seg[1], r, c)) {
      segments.emplace_back(seg);
    }
  }

  cout << (noIntersection(segments) ? "YES" : "NO") << endl;

}


Compiler output:
a.cc: In function 'int main(int, char**)':
a.cc:279:20: error: no match for 'operator+' (operand types are 'Segment' {aka 'std::array<Point, 2>'} and 'int')
  279 |     sort(seg , seg + 2);
      |                ~~~ ^ ~
      |                |     |
      |                |     int
      |                Segment {aka std::array<Point, 2>}
In file included from /usr/include/c++/14/bits/stl_algobase.h:67,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_iterator.h:627:5: note: candidate: 'template<class _Iterator> constexpr std::reverse_iterator<_Iterator> std::operator+(typename reverse_iterator<_Iterator>::difference_type, const reverse_iterator<_Iterator>&)'
  627 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:627:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'int'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note: candidate: 'template<class _Iterator> constexpr std::move_iterator<_IteratorL> std::operator+(typename move_iterator<_IteratorL>::difference_type, const move_iterator<_IteratorL>&)'
 1798 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'int'
  279 |     sort(seg , seg + 2);
      |                      ^
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/basic_string.h:3598:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3598 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3598:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3616:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3616 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   mismatched types 'const _CharT*' and 'std::array<Point, 2>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3635:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3635 |     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'int'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3652:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)'
 3652 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3670:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 3670 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3682:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3682 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3689:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3689 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3689:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3696:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3696 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3696:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3719:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3719 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3719:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   mismatched types 'const _CharT*' and 'std::array<Point, 2>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3726:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3726 |     operator+(_CharT __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3726:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'int'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3733:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const _CharT*)'
 3733 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3733:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
/usr/include/c++/14/bits/basic_string.h:3740:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, _CharT)'
 3740 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3740:5: note:   template argument deduction/substitution failed:
a.cc:279:22: note:   'Segment' {aka 'std::array<Point, 2>'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  279 |     sort(seg , seg + 2);
      |                      ^
In file included from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/complex:340:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator+(const complex<_Tp>&, const complex<_Tp>&)'
  340 |     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:340:5: note:   template argument d