Problem p03685 (C++)

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

bool onEdge(pair<int, int> p) {
  return (p.first == 0 || p.first == r || p.second == 0 || p.second == c);
}

int edgeInde(pair<int, int> pos) {
  if (pos.first == 0)
    return 0;
  if (pos.second == c)
    return 1;
  if (pos.first == r)
    return 2;
  if (pos.second == 0)
    return 3;
  return -1;
}

bool comp(pair<int, pair<int, int>> lhs, pair<int, pair<int, int>> rhs) {
  int linde = edgeInde(lhs.second);
  int rinde = edgeInde(rhs.second);
  if (linde < rinde)
    return true;
  if (linde > rinde)
    return false;
  if (linde == 0) {
    return lhs.second.second < rhs.second.second;
  }
  if (linde == 1) {
    return lhs.second.first < rhs.second.first;
  }
  if (linde == 2) {
    return lhs.second.second > rhs.second.second;
  }
  if (linde == 3) {
    return lhs.second.first > rhs.second.first;
  }
  throw("Unexpected");
}

int main() {
  cin >> r >> c >> n;
  vector<pair<int, int>> p1(n), p2(n);
  vector<pair<int, pair<int, int>>> edge;
  for (int i = 0; i < n; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    p1[i] = {x1, y1};
    p2[i] = {x2, y2};
    if (onEdge(p1[i]) && onEdge(p2[i])) {
      edge.push_back({i, p1[i]});
      edge.push_back({i, p2[i]});
    }
  }
  sort(edge.begin(), edge.end(), comp);

  vector<int> index(edge.size());
  for (int i = 0; i < edge.size(); i++) {
    index[i] = edge[i].first;
  }

  stack<int> st;
  for (int i = 0; i < index.size(); i++) {
    if (i != 0 && st.top() == index[i])
      st.pop();
    else
      st.push(index[i]);
  }
  cout << (st.empty() ? "YES" : "NO") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int r, c, n;

bool onEdge(pair<int, int> p) {
  return (p.first == 0 || p.first == r || p.second == 0 || p.second == c);
}

int edgeInde(pair<int, int> pos) {
  if (pos.first == 0)
    return 0;
  if (pos.second == c)
    return 1;
  if (pos.first == r)
    return 2;
  if (pos.second == 0)
    return 3;
  return -1;
}

bool comp(pair<int, pair<int, int>> lhs, pair<int, pair<int, int>> rhs) {
  int linde = edgeInde(lhs.second);
  int rinde = edgeInde(rhs.second);
  if (linde < rinde)
    return true;
  if (linde > rinde)
    return false;
  if (linde == 0) {
    return lhs.second.second < rhs.second.second;
  }
  if (linde == 1) {
    return lhs.second.first < rhs.second.first;
  }
  if (linde == 2) {
    return lhs.second.second > rhs.second.second;
  }
  if (linde == 3) {
    return lhs.second.first > rhs.second.first;
  }
  throw("Unexpected");
}

int main() {
  cin >> r >> c >> n;
  vector<pair<int, int>> p1(n), p2(n);
  vector<pair<int, pair<int, int>>> edge;
  for (int i = 0; i < n; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    p1[i] = {x1, y1};
    p2[i] = {x2, y2};
    if (onEdge(p1[i]) && onEdge(p2[i])) {
      edge.push_back({i, p1[i]});
      edge.push_back({i, p2[i]});
    }
  }
  sort(edge.begin(), edge.end(), comp);

  vector<int> index(edge.size());
  for (int i = 0; i < edge.size(); i++) {
    index[i] = edge[i].first;
  }

  stack<int> st;
  for (int i = 0; i < index.size(); i++) {
    if (!st.empty() && st.top() == index[i])
      st.pop();
    else
      st.push(index[i]);
  }
  cout << (st.empty() ? "YES" : "NO") << endl;
  return 0;
}