Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e6 + 10;
int n, r, c, m, size, q[N];
pair<int, int> p[N];
int ID(int x, int y) {
  if (!x)
    return y;
  if (y == c)
    return c + x;
  if (x == r)
    return c + r + c - y;
  if (!y)
    return c + r + c + r - x;
  return -1;
}
int Max[N];
#define lc x << 1
#define rc x << 1 | 1
void change(int x, int l, int r, int p, int v) {
  if (l == r)
    return void(Max[x] = max(Max[x], v));
  int mid = (l + r) >> 1;
  if (p > mid)
    change(rc, mid + 1, r, p, v);
  else
    change(lc, l, mid, p, v);
  Max[x] = max(Max[lc], Max[rc]);
}
int query(int x, int l, int r, int L, int R) {
  if (l >= L && r <= R)
    return Max[x];
  int mid = (l + r) >> 1, ans = 0;
  if (L <= mid)
    ans = max(ans, query(lc, l, mid, L, R));
  if (R > mid)
    ans = max(ans, query(rc, mid + 1, r, L, R));
  return ans;
}
int main() {
  scanf("%d%d%d", &r, &c, &n);
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    int a = ID(x1, y1), b = ID(x2, y2);
    if (a == -1 || b == -1)
      continue;
    if (a > b)
      swap(a, b);
    p[++m] = make_pair(a, b);
    q[++size] = a;
    q[++size] = b;
  }
  sort(q + 1, q + size + 1);
  for (int i = 1; i <= m; i++) {
    p[i].first = lower_bound(q + 1, q + size + 1, p[i].first) - q;
    p[i].second = lower_bound(q + 1, q + size + 1, p[i].second) - q;
    change(1, 1, size, p[i].first, p[i].second);
  }
  for (int i = 1; i <= m; i++)
    if (query(1, 1, m, p[i].first, p[i].second) > p[i].second)
      return puts("NO"), 0;
  puts("YES");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e6 + 10;
int n, r, c, m, size, q[N];
pair<int, int> p[N];
int ID(int x, int y) {
  if (!x)
    return y;
  if (y == c)
    return c + x;
  if (x == r)
    return c + r + c - y;
  if (!y)
    return c + r + c + r - x;
  return -1;
}
int Max[N];
#define lc x << 1
#define rc x << 1 | 1
void change(int x, int l, int r, int p, int v) {
  if (l == r)
    return void(Max[x] = max(Max[x], v));
  int mid = (l + r) >> 1;
  if (p > mid)
    change(rc, mid + 1, r, p, v);
  else
    change(lc, l, mid, p, v);
  Max[x] = max(Max[lc], Max[rc]);
}
int query(int x, int l, int r, int L, int R) {
  if (l >= L && r <= R)
    return Max[x];
  int mid = (l + r) >> 1, ans = 0;
  if (L <= mid)
    ans = max(ans, query(lc, l, mid, L, R));
  if (R > mid)
    ans = max(ans, query(rc, mid + 1, r, L, R));
  return ans;
}
int main() {
  scanf("%d%d%d", &r, &c, &n);
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    int a = ID(x1, y1), b = ID(x2, y2);
    if (a == -1 || b == -1)
      continue;
    if (a > b)
      swap(a, b);
    p[++m] = make_pair(a, b);
    q[++size] = a;
    q[++size] = b;
  }
  sort(q + 1, q + size + 1);
  for (int i = 1; i <= m; i++) {
    p[i].first = lower_bound(q + 1, q + size + 1, p[i].first) - q;
    p[i].second = lower_bound(q + 1, q + size + 1, p[i].second) - q;
    change(1, 1, size, p[i].first, p[i].second);
  }
  for (int i = 1; i <= m; i++)
    if (query(1, 1, size, p[i].first, p[i].second) > p[i].second)
      return puts("NO"), 0;
  puts("YES");
  return 0;
}