Problem p03685 (C++)

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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<ld, ld> id;

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define FILL(x, n, v) fill(x, x + n, v);
#define ALL(x) x.begin(), x.end()
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define f first
#define s second
#define ins insert
#define e emplace
#define eb emplace_back
#define ef emplace_front
#define p push
#define pf push_front
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ft front
#define bk back
#define pp pop
#define ppb pop_back
#define ppf pop_front

#define db cout << "YEET\n";
#define ct(x) cout << x << '\n';

const ll MOD = 1e9 + 7; // 998244353
const ll MAXN = 2e5 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

int main() {
  FAST ll h, w, n;
  cin >> h >> w >> n;
  vector<tuple<ll, ll, ll>> border;
  FOR(i, 1, n) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if ((a == 0 || a == h || b == 0 || b == w) &&
        (c == 0 || c == h || d == 0 || d == w)) {
      border.eb(a, b, i);
      border.eb(c, d, i);
    }
  }
  ll sz = (ll)border.size() - 1;

  vector<tuple<ll, ll, ll>> top, right, bottom, left;
  FOR(i, 0, sz) {
    ll x, y, num;
    tie(x, y, num) = border[i];
    if (x == 0)
      top.eb(x, y, num);
    else if (x == h)
      bottom.eb(x, y, num);
    else if (y == 0)
      left.eb(x, y, num);
    else
      right.eb(x, y, num);
  }
  sort(ALL(top));
  sort(ALL(bottom), greater<tuple<ll, ll, ll>>());
  sort(ALL(right));
  sort(ALL(left), greater<tuple<ll, ll, ll>>());

  bool check[100], imposs = 0;
  MEM(check, false);
  stack<ll> ans;

  sz = (ll)top.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(top[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  sz = (ll)right.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(right[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  sz = (ll)bottom.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(bottom[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  sz = (ll)left.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(left[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  if (!ans.empty())
    imposs = 1;

  if (imposs)
    cout << "NO";
  else
    cout << "YES";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<ld, ld> id;

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define FILL(x, n, v) fill(x, x + n, v);
#define ALL(x) x.begin(), x.end()
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define f first
#define s second
#define ins insert
#define e emplace
#define eb emplace_back
#define ef emplace_front
#define p push
#define pf push_front
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ft front
#define bk back
#define pp pop
#define ppb pop_back
#define ppf pop_front

#define db cout << "YEET\n";
#define ct(x) cout << x << '\n';

const ll MOD = 1e9 + 7; // 998244353
const ll MAXN = 2e5 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

int main() {
  FAST ll h, w, n;
  cin >> h >> w >> n;
  vector<tuple<ll, ll, ll>> border;
  FOR(i, 1, n) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if ((a == 0 || a == h || b == 0 || b == w) &&
        (c == 0 || c == h || d == 0 || d == w)) {
      border.eb(a, b, i);
      border.eb(c, d, i);
    }
  }
  ll sz = (ll)border.size() - 1;

  vector<tuple<ll, ll, ll>> top, right, bottom, left;
  FOR(i, 0, sz) {
    ll x, y, num;
    tie(x, y, num) = border[i];
    if (x == 0)
      top.eb(x, y, num);
    else if (x == h)
      bottom.eb(x, y, num);
    else if (y == 0)
      left.eb(x, y, num);
    else
      right.eb(x, y, num);
  }
  sort(ALL(top));
  sort(ALL(bottom), greater<tuple<ll, ll, ll>>());
  sort(ALL(right));
  sort(ALL(left), greater<tuple<ll, ll, ll>>());

  bool check[MAXN], imposs = 0;
  MEM(check, false);
  stack<ll> ans;

  sz = (ll)top.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(top[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  sz = (ll)right.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(right[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  sz = (ll)bottom.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(bottom[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  sz = (ll)left.size() - 1;
  FOR(i, 0, sz) {
    ll num = get<2>(left[i]);
    if (!check[num]) {
      ans.p(num);
      check[num] = 1;
    } else {
      if (ans.empty() || ans.top() != num)
        imposs = 1;
      else
        ans.pp();
    }
  }

  if (!ans.empty())
    imposs = 1;

  if (imposs)
    cout << "NO";
  else
    cout << "YES";
}