Language: C++

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1)
	{
		cerr << name << " = " << arg1 << endl;
	}
	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args)
	{
		const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " = " << arg1 << " || ";
		__f(comma+1, args...);
	}
#else
#define debug(...)
#endif

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
    os << "{ ";
    for(auto& y : x) os << y << " ; ";
    return os << "}";
}

template <class X, class Y>
ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[ " << p.first << ", " << p.second << "]" ;
}

#define FAST_IO std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;  // Min Heap

typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//K-th smallest
//cout << k << "kth (1 based indexing) smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;

map<pair<ll,ll>, ll>val;
map<ll, ll>cnt, isboundary;

int main()
{
	FAST_IO;

 	ll r, c, n;
 	cin >> r >> c >> n;
 	for(int i=0;i<n;i++)
 	{
 		ll x1, y1, x2, y2;
 		cin >> x1 >> y1 >> x2 >> y2;
 		val[{x1, y1}] = i + 1;
 		val[{x2, y2}] = i + 1;
 		if(x1 == 0 || x1 == r || y1 == 0 || y1 == c) isboundary[i + 1] += 1;
 		if(x2 == 0 || x2 == r || y2 == 0 || y2 == c) isboundary[i + 1] += 1;
 	}
 	stack<ll>s;
 	for(int i=0;i<=c;i++)
 	{
 		if(isboundary[val[{0, i}]] != 2) continue;
 		if(cnt[val[{0, i}]] == 1)
 		{
 			if(s.top() != val[{0, i}])
 			{
 				cout << "NO" << endl;
 				return 0;
 			}
 			else s.pop();
 		}
 		else
 		{
 			s.push(val[{0, i}]);
 			cnt[val[{0, i}]] += 1;
 		}
 	}
 	for(int i=1;i<=r;i++)
 	{
 		if(isboundary[val[{i, c}]] != 2) continue;
 		if(cnt[val[{i, c}]] == 1)
 		{
 			if(s.top() != val[{i, c}])
 			{
 				cout << "NO" << endl;
 				return 0;
 			}
 			else s.pop();
 		}
 		else
 		{
 			s.push(val[{i, c}]);
 			cnt[val[{i, c}]] += 1;
 		}
 	}
 	for(int i=c - 1;i>=0;i--)
 	{
 		if(isboundary[val[{r, i}]] != 2) continue;
 		if(cnt[val[{r, i}]] == 1)
 		{
 			if(s.top() != val[{r, i}])
 			{
 				cout << "NO" << endl;
 				return 0;
 			}
 			else s.pop();
 		}
 		else
 		{
 			s.push(val[{r, i}]);
 			cnt[val[{r, i}]] += 1;
 		}
 	}
 	for(int i=r - 1;i>=0;i--)
 	{
 		if(isboundary[val[{i, 0}]] != 2) continue;
 		if(cnt[val[{i, 0}]] == 1)
 		{
 			if(s.top() != val[{i, 0}])
 			{
 				cout << "NO" << endl;
 				return 0;
 			}
 			else s.pop();
 		}
 		else
 		{
 			s.push(val[{i, 0}]);
 			cnt[val[{i, 0}]] += 1;
 		}
 	}
 	cout << "YES" << endl;
    return 0;
}

Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:5:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Rb_tree<std::pair<long long int, long long int>, std::pair<const std::pair<long long int, long long int>, long long int>, std::_Select1st<std::pair<const std::pair<long long int, long long int>, long long int> >, std::less<std::pair<long long int, long long int> >, std::allocator<std::pair<const std::pair<long long int, long long int>, long long int> > >::_Rb_tree_impl<std::less<std::pair<long long int, long long int> >, true>::~_Rb_tree_impl()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::_Rb_tree_node<std::pair<const std::pair<long long int, long long int>, long long int> >]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152:
/usr/include/c++/14/bits/stl_tree.h:658:16: note: called from here
  658 |         struct _Rb_tree_impl
      |                ^~~~~~~~~~~~~