Problem p02347 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

typedef struct node {
  int x;
  int y;
  int depth;
  int id;
  node *right;
  node *left;
} node;

bool compX(node &n1, node &n2) { return n1.x < n2.x; }

bool compY(node &n1, node &n2) { return n1.y < n2.y; }

node *nil;
std::vector<int> ans;
std::vector<node> nodes;

node *make_tree(int l, int r, int depth) {
  if (l >= r) {
    return nil;
  }

  if (depth % 2 == 0) {
    std::sort(&nodes[l], &nodes[r], compX);
  } else {
    std::sort(&nodes[l], &nodes[r], compY);
  }

  int mid = (l + r) / 2;
  nodes[mid].depth = depth;
  nodes[mid].left = make_tree(l, mid, depth + 1);
  nodes[mid].right = make_tree(mid + 1, r, depth + 1);

  return &nodes[mid];
}

void search(node *n, int x_l, int x_h, int y_l, int y_h) {
  int x = n->x;
  int y = n->y;

  if (x_l <= x && x <= x_h && y_l <= y && y <= y_h)
    ans.push_back(n->id);

  if (n->depth % 2 == 0) {
    if (n->left != nil && x_l <= x)
      search(n->left, x_l, x_h, y_l, y_h);
    if (n->right != nil && x <= x_h)
      search(n->right, x_l, x_h, y_l, y_h);
  }

  else {
    if (n->left != nil && y_l <= y)
      search(n->left, x_l, x_h, y_l, y_h);
    if (n->right != nil && y <= y_h)
      search(n->right, x_l, x_h, y_l, y_h);
  }
}

void search_from_root(node *root, int x_l, int x_h, int y_l, int y_h) {
  search(root, x_l, x_h, y_l, y_h);
  sort(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++)
    printf("%d\\n", ans[i]);
  printf("\\n");
  ans.clear();
}

int main(void) {
  int n, q, x, y;
  node *root;
  scanf("%d", &n);
  nodes.resize(n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x, &y);
    nodes[i].x = x;
    nodes[i].y = y;
    nodes[i].id = i;
  }

  root = make_tree(0, n, 0);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int x_low, x_high, y_low, y_high;
    scanf("%d %d %d %d", &x_low, &x_high, &y_low, &y_high);
    search_from_root(root, x_low, x_high, y_low, y_high);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

typedef struct node {
  int x;
  int y;
  int depth;
  int id;
  node *right;
  node *left;
} node;

node *nil;
std::vector<int> ans;

void init_node(node *n, int x, int y, int id) {
  n->left = nil;
  n->right = nil;
  n->x = x;
  n->y = y;
  n->id = id;
}

void linkage(node *child, node *parent) {
  int parent_num, child_num;
  if (parent->depth % 2 == 0) {
    parent_num = parent->x;
    child_num = child->x;
  } else {
    parent_num = parent->y;
    child_num = child->y;
  }

  if (child_num >= parent_num) {
    if (parent->right != nil) {
      linkage(child, parent->right);
    } else {
      parent->right = child;
      child->depth = parent->depth + 1;
    }
  } else {
    if (parent->left != nil) {
      linkage(child, parent->left);
    } else {
      parent->left = child;
      child->depth = parent->depth + 1;
    }
  }
}

void make_node(int x, int y, int id, node *new_node, node *root) {
  init_node(new_node, x, y, id);
  linkage(new_node, root);
}

void search(node *n, int x_l, int x_h, int y_l, int y_h) {
  int x = n->x;
  int y = n->y;

  if (x_l <= x && x <= x_h && y_l <= y && y <= y_h)
    ans.push_back(n->id);

  if (n->depth % 2 == 0) {
    if (n->left != nil && x_l <= x)
      search(n->left, x_l, x_h, y_l, y_h);
    if (n->right != nil && x <= x_h)
      search(n->right, x_l, x_h, y_l, y_h);
  }

  else {
    if (n->left != nil && y_l <= y)
      search(n->left, x_l, x_h, y_l, y_h);
    if (n->right != nil && y <= y_h)
      search(n->right, x_l, x_h, y_l, y_h);
  }
}

void search_from_root(node *root, int x_l, int x_h, int y_l, int y_h) {
  search(root, x_l, x_h, y_l, y_h);
  sort(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++)
    printf("%d\\n", ans[i]);
  printf("\\n");
  ans.clear();
}

int main(void) {
  int n, q, x, y;
  node root;
  node *new_node;
  scanf("%d", &n);
  scanf("%d %d", &x, &y);
  init_node(&root, x, y, 0);
  root.depth = 0;

  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d", &x, &y);
    new_node = new node;
    make_node(x, y, i + 1, new_node, &root);
  }

  scanf("%d", &q);
  int x_low, x_high, y_low, y_high;
  for (int i = 0; i < q; i++) {
    scanf("%d %d %d %d", &x_low, &x_high, &y_low, &y_high);
    search_from_root(&root, x_low, x_high, y_low, y_high);
  }
}