Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned ll
#define vi vector<ll>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ld long double
#define pii pair<ll, ll>
#define mt make_tuple
#define mn(a, b) a = min(a, b)
#define mx(a, b) a = max(a, b)

using namespace std;

const ll INF = (ll)2e9;
const ll inf = (ll)2e18;
const ld eps = (ld)1e-8;
const ll mod = (ll)998244353;
const ll MAXN = (ll)1e5 + 1;
const ll MAXC = (ll)1e6 + 1;
const ll MAXE = (ll)1000;
const ll MAXLOG = 21;
const ll maxlen = (ll)1e5;
const ll asci = (ll)256;
const ll block = 480;
const ld PI = acos(-1);
const ld e = 2.7182818284;

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<
pii,
null_type,
less<pii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/

template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (T &cnt : arr) {
    in >> cnt;
  }
  return in;
};

void solve() {
  int r, c, n;
  cin >> r >> c >> n;
  vector<pii> pts;
  for (int i = 0; i < n; ++i) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    if ((!x1 || x1 == r || !y1 || y1 == c) &&
        (!x2 || x2 == r || !y2 || y2 == c)) {
      if (!x1 || y1 == c)
        pts.push_back(mp(x1 + y1, i));
      else
        pts.push_back(mp(r + c + (r - x1) + (c - y1), i));
      if (!x2 || y2 == c)
        pts.push_back(mp(x2 + y2, i));
      else
        pts.push_back(mp(r + c + (r - x2) + (c - y2), i));
    }
  }
  sort(all(pts));
  stack<int> q;
  for (int i = 0; i < pts.size(); ++i) {
    if (q.top() == pts[i].second) {
      q.pop();
    } else {
      q.push(pts[i].second);
    }
  }
  if (!q.size())
    cout << "YES\n";
  else
    cout << "NO\n";
}

int main() {
  srand(time(0));
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#endif
  cout.precision(30);
  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned ll
#define vi vector<ll>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ld long double
#define pii pair<ll, ll>
#define mt make_tuple
#define mn(a, b) a = min(a, b)
#define mx(a, b) a = max(a, b)

using namespace std;

const ll INF = (ll)2e9;
const ll inf = (ll)2e18;
const ld eps = (ld)1e-8;
const ll mod = (ll)998244353;
const ll MAXN = (ll)1e5 + 1;
const ll MAXC = (ll)1e6 + 1;
const ll MAXE = (ll)1000;
const ll MAXLOG = 21;
const ll maxlen = (ll)1e5;
const ll asci = (ll)256;
const ll block = 480;
const ld PI = acos(-1);
const ld e = 2.7182818284;

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<
pii,
null_type,
less<pii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/

template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (T &cnt : arr) {
    in >> cnt;
  }
  return in;
};

void solve() {
  int r, c, n;
  cin >> r >> c >> n;
  vector<pii> pts;
  for (int i = 0; i < n; ++i) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    if ((!x1 || x1 == r || !y1 || y1 == c) &&
        (!x2 || x2 == r || !y2 || y2 == c)) {
      if (!x1 || y1 == c)
        pts.push_back(mp(x1 + y1, i));
      else
        pts.push_back(mp(r + c + (r - x1) + (c - y1), i));
      if (!x2 || y2 == c)
        pts.push_back(mp(x2 + y2, i));
      else
        pts.push_back(mp(r + c + (r - x2) + (c - y2), i));
    }
  }
  sort(all(pts));
  stack<int> q;
  for (int i = 0; i < pts.size(); ++i) {
    if (q.size() && q.top() == pts[i].second) {
      q.pop();
    } else {
      q.push(pts[i].second);
    }
  }
  if (!q.size())
    cout << "YES\n";
  else
    cout << "NO\n";
}

int main() {
  srand(time(0));
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#endif
  cout.precision(30);
  solve();

  return 0;
}