Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
  int x, y, x1, y1, id;
  node(int a = 0, int b = 0, int e = 0, int f = 0, int c = 0) {
    x = a, y = b, id = c;
    x1 = e;
    y1 = f;
  }
} e[N];
int n, m, k;
int cl(node p) {
  if (p.x == 0)
    return 0;
  else if (p.y == m)
    return 1;
  else if (p.x == n)
    return 2;
  else if (p.y == 0)
    return 3;
  else
    return -1;
}
bool cmp(node a, node b) {
  int t1 = cl(a), t2 = cl(b);
  if (t1 != t2) {
    return t1 < t2;
  } else {
    if (t1 == 0) {
      return a.y < b.y;
    } else if (t1 == 1) {
      return a.x < b.x;
    } else if (t1 == 2) {
      return a.y > b.y;
    } else if (t2 == 3) {
      return a.x > b.x;
    }
  }
}
stack<pair<int, int>> q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int cnt = 0;
  for (int i = 1, x1, x2, y1, y2; i <= k; i++) {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    node p1 = node(x1, y1, 0, 0, 0);
    node p2 = node(x2, y2, 0, 0, 0);
    if (cl(p1) != -1 && cl(p2) != -1) {
      ++cnt;
      if (cmp(p1, p2)) {
        p1.x1 = p2.x, p1.y1 = p2.y;
        p1.id = 0, p2.id = 1;
        e[cnt] = p1, e[++cnt] = p2;
      } else {
        p2.x1 = p1.x, p2.y1 = p1.y;
        p1.id = 1, p2.id = 0;
        e[cnt] = p1, e[++cnt] = p2;
      }
    }
  }
  sort(e + 1, e + cnt + 1, cmp);
  int flag = 0;
  // for(int i=1;i<=cnt;i++)cout<<e[i].x<<" "<<e[i].y<<" "<<e[i].x1<<"
  // "<<e[i].y1<<" "<<e[i].id<<endl;
  for (int i = 1; i <= cnt && !flag; i++) {
    if (e[i].id == 0) {
      q.push(make_pair(e[i].x1, e[i].y1));
    } else {
      if (q.empty())
        flag = 1;
      else {
        pair<int, int> now = q.top();
        q.pop();
        if (now.first == e[i].x && now.second == e[i].y) {
          continue;
        } else {
          flag = 1;
        }
      }
    }
  }
  if (flag)
    puts("NO");
  else
    puts("YES");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
struct node {
  int x, y, x1, y1, id;
  node(int a = 0, int b = 0, int e = 0, int f = 0, int c = 0) {
    x = a, y = b, id = c;
    x1 = e;
    y1 = f;
  }
} e[N];
int n, m, k;
int cl(node p) {
  if (p.x == 0)
    return 0;
  else if (p.y == m)
    return 1;
  else if (p.x == n)
    return 2;
  else if (p.y == 0)
    return 3;
  else
    return -1;
}
bool cmp(node a, node b) {
  int t1 = cl(a), t2 = cl(b);
  if (t1 != t2) {
    return t1 < t2;
  } else {
    if (t1 == 0) {
      return a.y < b.y;
    } else if (t1 == 1) {
      return a.x < b.x;
    } else if (t1 == 2) {
      return a.y > b.y;
    } else if (t2 == 3) {
      return a.x > b.x;
    }
  }
}
stack<pair<int, int>> q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int cnt = 0;
  for (int i = 1, x1, x2, y1, y2; i <= k; i++) {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    node p1 = node(x1, y1, 0, 0, 0);
    node p2 = node(x2, y2, 0, 0, 0);
    if (cl(p1) != -1 && cl(p2) != -1) {
      ++cnt;
      if (cmp(p1, p2)) {
        p1.x1 = p2.x, p1.y1 = p2.y;
        p1.id = 0, p2.id = 1;
        e[cnt] = p1, e[++cnt] = p2;
      } else {
        p2.x1 = p1.x, p2.y1 = p1.y;
        p1.id = 1, p2.id = 0;
        e[cnt] = p1, e[++cnt] = p2;
      }
    }
  }
  sort(e + 1, e + cnt + 1, cmp);
  int flag = 0;
  // for(int i=1;i<=cnt;i++)cout<<e[i].x<<" "<<e[i].y<<" "<<e[i].x1<<"
  // "<<e[i].y1<<" "<<e[i].id<<endl;
  for (int i = 1; i <= cnt && !flag; i++) {
    if (e[i].id == 0) {
      q.push(make_pair(e[i].x1, e[i].y1));
    } else {
      if (q.empty())
        flag = 1;
      else {
        pair<int, int> now = q.top();
        q.pop();
        if (now.first == e[i].x && now.second == e[i].y) {
          continue;
        } else {
          flag = 1;
        }
      }
    }
  }
  if (flag)
    puts("NO");
  else
    puts("YES");
}