Problem p02347 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#include <algorithm>
#include <cstdio>

struct point {
  int x, y, id;
};

point init(int pos_x, int pos_y, int index) {
  point p;
  p.x = pos_x, p.y = pos_y, p.id = index;
  return p;
}

bool cmp_x(const point &P1, const point &P2) {
  if (P1.x != P2.x) {
    return P1.x < P2.x;
  }
  return P1.y < P2.y;
}
bool cmp_y(const point &P1, const point &P2) {
  if (P1.y != P2.y) {
    return P1.y < P2.y;
  }
  return P1.x < P2.x;
}
bool cmp_id(const point &P1, const point &P2) { return P1.id < P2.id; }

typedef struct node {
  point mark;
  node *less, *more;
} kd_node;

kd_node *init(point pos, node *l, node *m) {
  kd_node *node = new kd_node;
  node->mark = pos, node->less = l, node->more = m;
  return node;
}

kd_node *kd_tree(point *P, int *S, int *T, int *U, int left, int right, int d) {
  if (left == right)
    return NULL;
  if (left + 1 == right) {
    kd_node *leaf = new kd_node;
    leaf->less = NULL, leaf->more = NULL;
    if ((d & 1) == 0) {
      leaf->mark = P[S[left]];
    } else {
      leaf->mark = P[T[left]];
    }
    return leaf;
  }

  kd_node *node = new kd_node;
  int mid = (left + right) / 2;
  int a = left, b = mid;

  if ((d & 1) == 0) {
    node->mark = P[S[mid]];
    for (int i = left; i < right; i++) {
      if (cmp_x(P[T[i]], P[S[mid]])) {
        U[a] = T[i], a++;
      } else {
        U[b] = T[i], b++;
      }
    }
    for (int i = left; i < right; i++) {
      T[i] = U[i];
    }

  } else {
    node->mark = P[T[mid]];
    for (int i = left; i < right; i++) {
      if (cmp_y(P[S[i]], P[T[mid]])) {
        U[a] = S[i], a++;
      } else {
        U[b] = S[i], b++;
      }
    }
    for (int i = left; i < right; i++) {
      S[i] = U[i];
    }
  }
  node->less = kd_tree(P, S, T, U, left, mid, d + 1);
  node->more = kd_tree(P, S, T, U, mid, right, d + 1);

  return node;
}

int cnt;

void find_pt(int *Q, int sx, int tx, int sy, int ty, kd_node *node, int d) {
  point p = node->mark;
  int a = p.x, b = p.y;

  if (node->less == NULL && node->more == NULL) {
    if (sx <= a && a <= tx && sy <= b && b <= ty) {
      Q[cnt] = p.id, cnt++;
    }
    return;
  }
  if ((d & 1) == 0) {
    if (sx <= a) {
      find_pt(Q, sx, tx, sy, ty, node->less, d + 1);
    }
    if (a <= tx) {
      find_pt(Q, sx, tx, sy, ty, node->more, d + 1);
    }
  } else {
    if (sy <= b) {
      find_pt(Q, sx, tx, sy, ty, node->less, d + 1);
    }
    if (b <= ty) {
      find_pt(Q, sx, tx, sy, ty, node->more, d + 1);
    }
  }
  return;
}

int main() {
  point *P;
  int *S, *T, *U;
  int Q[100];
  int i, pos_x, pos_y, n;
  scanf("%d", &n);

  P = new point[n];
  S = new int[n], T = new int[n], U = new int[n];

  for (i = 0; i < n; i++) {
    scanf("%d %d", &pos_x, &pos_y);
    P[i] = init(pos_x, pos_y, i);
  }
  sort(P, P + n, cmp_x);
  for (i = 0; i < n; i++) {
    S[i] = P[i].id;
  }
  sort(P, P + n, cmp_y);
  for (i = 0; i < n; i++) {
    T[i] = P[i].id;
  }
  sort(P, P + n, cmp_id);

  kd_node *root = kd_tree(P, S, T, U, 0, n, 0);

  int sx, tx, sy, ty, q;
  scanf("%d", &q);
  while (q) {
    cnt = 0;
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    find_pt(Q, sx, tx, sy, ty, root, 0);
    sort(Q, Q + cnt);
    for (int i = 0; i < cnt; i++) {
      printf("%d\\n", Q[i]);
    }
    printf("\\n");
    q--;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#include <algorithm>
#include <cstdio>

struct point {
  int x, y, id;
};

point init(int pos_x, int pos_y, int index) {
  point p;
  p.x = pos_x, p.y = pos_y, p.id = index;
  return p;
}

bool cmp_x(const point &P1, const point &P2) {
  if (P1.x != P2.x) {
    return P1.x < P2.x;
  }
  return P1.y < P2.y;
}
bool cmp_y(const point &P1, const point &P2) {
  if (P1.y != P2.y) {
    return P1.y < P2.y;
  }
  return P1.x < P2.x;
}
bool cmp_id(const point &P1, const point &P2) { return P1.id < P2.id; }

typedef struct node {
  point mark;
  node *less, *more;
} kd_node;

kd_node *init(point pos, node *l, node *m) {
  kd_node *node = new kd_node;
  node->mark = pos, node->less = l, node->more = m;
  return node;
}

kd_node *kd_tree(point *P, int *S, int *T, int *U, int left, int right, int d) {
  if (left == right)
    return NULL;
  if (left + 1 == right) {
    kd_node *leaf = new kd_node;
    leaf->less = NULL, leaf->more = NULL;
    if ((d & 1) == 0) {
      leaf->mark = P[S[left]];
    } else {
      leaf->mark = P[T[left]];
    }
    return leaf;
  }

  kd_node *node = new kd_node;
  int mid = (left + right) / 2;
  int a = left, b = mid;

  if ((d & 1) == 0) {
    node->mark = P[S[mid]];
    for (int i = left; i < right; i++) {
      if (cmp_x(P[T[i]], P[S[mid]])) {
        U[a] = T[i], a++;
      } else {
        U[b] = T[i], b++;
      }
    }
    for (int i = left; i < right; i++) {
      T[i] = U[i];
    }

  } else {
    node->mark = P[T[mid]];
    for (int i = left; i < right; i++) {
      if (cmp_y(P[S[i]], P[T[mid]])) {
        U[a] = S[i], a++;
      } else {
        U[b] = S[i], b++;
      }
    }
    for (int i = left; i < right; i++) {
      S[i] = U[i];
    }
  }
  node->less = kd_tree(P, S, T, U, left, mid, d + 1);
  node->more = kd_tree(P, S, T, U, mid, right, d + 1);

  return node;
}

int cnt;

void find_pt(int *Q, int sx, int tx, int sy, int ty, kd_node *node, int d) {
  point p = node->mark;
  int a = p.x, b = p.y;

  if (node->less == NULL && node->more == NULL) {
    if (sx <= a && a <= tx && sy <= b && b <= ty) {
      Q[cnt] = p.id, cnt++;
    }
    return;
  }
  if ((d & 1) == 0) {
    if (sx <= a) {
      find_pt(Q, sx, tx, sy, ty, node->less, d + 1);
    }
    if (a <= tx) {
      find_pt(Q, sx, tx, sy, ty, node->more, d + 1);
    }
  } else {
    if (sy <= b) {
      find_pt(Q, sx, tx, sy, ty, node->less, d + 1);
    }
    if (b <= ty) {
      find_pt(Q, sx, tx, sy, ty, node->more, d + 1);
    }
  }
  return;
}

int main() {
  point P[400000];
  int S[400000], T[400000], U[400000];
  int Q[100];
  int i, pos_x, pos_y, n;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &pos_x, &pos_y);
    P[i] = init(pos_x, pos_y, i);
  }
  sort(P, P + n, cmp_x);
  for (i = 0; i < n; i++) {
    S[i] = P[i].id;
  }
  sort(P, P + n, cmp_y);
  for (i = 0; i < n; i++) {
    T[i] = P[i].id;
  }
  sort(P, P + n, cmp_id);

  kd_node *root = kd_tree(P, S, T, U, 0, n, 0);

  int sx, tx, sy, ty, q;
  scanf("%d", &q);
  while (q) {
    cnt = 0;
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    find_pt(Q, sx, tx, sy, ty, root, 0);
    sort(Q, Q + cnt);
    for (int i = 0; i < cnt; i++) {
      printf("%d\\n", Q[i]);
    }
    printf("\\n");
    q--;
  }

  return 0;
}