Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

#define R register
#define maxn 100010
struct Poi {
  int x, y, id, type;
  inline bool operator<(const Poi &that) const {
    return type < that.type || (type == that.type && type == 1 ? y < that.y
                                : type == 2                    ? x < that.x
                                : type == 3                    ? y > that.y
                                                               : x > that.x);
  }
} pp[maxn << 2];
struct Rect {
  int x1, y1, x2, y2, id;
} p[maxn];
int pcnt, st[maxn << 2];
int main() {
  R int r, c, n;
  scanf("%d%d%d", &r, &c, &n);
  for (R int i = 1; i <= n; ++i)
    scanf("%d%d%d%d", &p[i].x1, &p[i].y1, &p[i].x2, &p[i].y2), p[i].id = i;
  for (R int i = 1; i <= n; ++i) {
    if (((p[i].x1 == 0 || p[i].x1 == r) || (p[i].y1 == 0 || p[i].y1 == c)) &&
        ((p[i].x2 == 0 || p[i].x2 == r) || (p[i].y2 == 0 || p[i].y2 == c))) {
      pp[++pcnt] = (Poi){p[i].x1, p[i].y1};
      pp[pcnt].id = i;
      if (pp[pcnt].x == 0)
        pp[pcnt].type = 1;
      else if (pp[pcnt].y == c)
        pp[pcnt].type = 2;
      else if (pp[pcnt].x == r)
        pp[pcnt].type = 3;
      else
        pp[pcnt].type = 4;

      pp[++pcnt] = (Poi){p[i].x2, p[i].y2};
      pp[pcnt].id = i;
      if (pp[pcnt].x == 0)
        pp[pcnt].type = 1;
      else if (pp[pcnt].y == c)
        pp[pcnt].type = 2;
      else if (pp[pcnt].x == r)
        pp[pcnt].type = 3;
      else
        pp[pcnt].type = 4;
    }
  }
  std::sort(pp + 1, pp + pcnt + 1);
  R int top = 0;
  //	for (R int i = 1; i <= pcnt; ++i) printf("x %d y %d id %d type %d\n",
  //pp[i].x, pp[i].y, pp[i].id, pp[i].type);
  for (R int i = 1; i <= pcnt; ++i) {
    if (top && st[top] == pp[i].id)
      --top;
    else
      st[++top] = pp[i].id;
  }
  puts(top ? "NO" : "YES");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>

#define R register
#define maxn 100010
struct Poi {
  int x, y, id, type;
  inline bool operator<(const Poi &that) const {
    return type < that.type ||
           (type == that.type && (type == 1   ? y < that.y
                                  : type == 2 ? x < that.x
                                  : type == 3 ? y > that.y
                                              : x > that.x));
  }
} pp[maxn << 2];
struct Rect {
  int x1, y1, x2, y2, id;
} p[maxn];
int pcnt, st[maxn << 2];
int main() {
  R int r, c, n;
  scanf("%d%d%d", &r, &c, &n);
  for (R int i = 1; i <= n; ++i)
    scanf("%d%d%d%d", &p[i].x1, &p[i].y1, &p[i].x2, &p[i].y2), p[i].id = i;
  for (R int i = 1; i <= n; ++i) {
    if (((p[i].x1 == 0 || p[i].x1 == r) || (p[i].y1 == 0 || p[i].y1 == c)) &&
        ((p[i].x2 == 0 || p[i].x2 == r) || (p[i].y2 == 0 || p[i].y2 == c))) {
      pp[++pcnt] = (Poi){p[i].x1, p[i].y1};
      pp[pcnt].id = i;
      if (pp[pcnt].x == 0)
        pp[pcnt].type = 1;
      else if (pp[pcnt].y == c)
        pp[pcnt].type = 2;
      else if (pp[pcnt].x == r)
        pp[pcnt].type = 3;
      else
        pp[pcnt].type = 4;

      pp[++pcnt] = (Poi){p[i].x2, p[i].y2};
      pp[pcnt].id = i;
      if (pp[pcnt].x == 0)
        pp[pcnt].type = 1;
      else if (pp[pcnt].y == c)
        pp[pcnt].type = 2;
      else if (pp[pcnt].x == r)
        pp[pcnt].type = 3;
      else
        pp[pcnt].type = 4;
    }
  }
  std::sort(pp + 1, pp + pcnt + 1);
  R int top = 0;
  //	for (R int i = 1; i <= pcnt; ++i) printf("x %d y %d id %d type %d\n",
  //pp[i].x, pp[i].y, pp[i].id, pp[i].type);
  for (R int i = 1; i <= pcnt; ++i) {
    if (top && st[top] == pp[i].id)
      --top;
    else
      st[++top] = pp[i].id;
  }
  puts(top ? "NO" : "YES");
  return 0;
}