Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // minmax, sort, swap
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <stack>
#include <string> // string, stoi, to_string
#include <vector> // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int h, w, n;
  std::cin >> h >> w >> n;
  auto f = [&](int x, int y) -> int {
    if (y == 0)
      return x;
    if (x == h)
      return h + y;
    if (y == w)
      return 2 * h + w - x;
    if (x == 0)
      return 2 * h + 2 * w - y;
    return -1;
  };
  std::vector<std::pair<int, int>> v;
  rep(i, n) {
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    if (f(a, b) != -1 && f(c, d) != -1) {
      v.emplace_back(f(a, b), i);
      v.emplace_back(f(c, d), i);
    }
  }
  std::sort(all(v));
  std::stack<int> stack;
  rep(i, 2 * n) {
    if (stack.empty()) {
      stack.emplace(v[i].second);
    } else {
      int x = stack.top();
      if (x == v[i].second)
        stack.pop();
      else
        stack.emplace(v[i].second);
    }
  }
  if (stack.empty())
    print("YES");
  else
    print("NO");
  return 0;
}


Fixed submission:
#include <algorithm> // minmax, sort, swap
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <stack>
#include <string> // string, stoi, to_string
#include <vector> // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int h, w, n;
  std::cin >> h >> w >> n;
  auto f = [&](int x, int y) -> int {
    if (y == 0)
      return x;
    if (x == h)
      return h + y;
    if (y == w)
      return 2 * h + w - x;
    if (x == 0)
      return 2 * h + 2 * w - y;
    return -1;
  };
  std::vector<std::pair<int, int>> v;
  rep(i, n) {
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    if (f(a, b) != -1 && f(c, d) != -1) {
      v.emplace_back(f(a, b), i);
      v.emplace_back(f(c, d), i);
    }
  }
  std::sort(all(v));
  std::stack<int> stack;
  rep(i, v.size()) {
    if (stack.empty()) {
      stack.emplace(v[i].second);
    } else {
      int x = stack.top();
      if (x == v[i].second)
        stack.pop();
      else
        stack.emplace(v[i].second);
    }
  }
  if (stack.empty())
    print("YES");
  else
    print("NO");
  return 0;
}