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

#include <cstdio>
#include <random>
#include <set>
#include <utility>
#include <vector>
using namespace std;
struct Point {
  int x, y, l;
  Point(int x, int y, int l) : x(x), y(y), l(l) {}
  Point() {}
};
struct Node {
  Node *left = NULL;
  Node *right = NULL;
  int x;
  int y;
  int location;
  Node(int x, int y, int location) : x(x), y(y), location(location) {}
  Node() {}
};
class Tree {
private:
  Node *root = NULL;
  void add_Node(Node *n, Node *&pos, int depth) {
    if (pos == NULL) {
      pos = n;
      return;
    }
    if (depth % 2 == 0) {
      if (n->y < pos->y) {
        add_Node(n, pos->left, depth + 1);
      } else {
        add_Node(n, pos->right, depth + 1);
      }
    } else {
      if (n->x < pos->x) {
        add_Node(n, pos->left, depth + 1);
      } else {
        add_Node(n, pos->right, depth + 1);
      }
    }
  }
  void find_Node(int sx, int tx, int sy, int ty, set<int> &ans, Node *pos,
                 int depth) {
    if (pos == NULL)
      return;
    if (sx <= pos->x && pos->x <= tx && sy <= pos->y && pos->y <= ty) {
      ans.insert(pos->location);
    }
    if (depth % 2 == 0) {
      if (sy <= pos->y) {
        find_Node(sx, tx, sy, ty, ans, pos->left, depth + 1);
      }
      if (pos->y <= ty) {
        find_Node(sx, tx, sy, ty, ans, pos->right, depth + 1);
      }
    } else {
      if (sx <= pos->x) {
        find_Node(sx, tx, sy, ty, ans, pos->left, depth + 1);
      }
      if (pos->x <= tx) {
        find_Node(sx, tx, sy, ty, ans, pos->right, depth + 1);
      }
    }
  }

public:
  void add_data(int x_in, int y_in, int loc_in) {
    Node *n = new Node(x_in, y_in, loc_in);
    add_Node(n, root, 0);
  }
  void find_data(int sx, int tx, int sy, int ty, set<int> &ans) {
    find_Node(sx, tx, sy, ty, ans, root, 0);
  }
};

int main() {
  int n;
  scanf("%d", &n);
  vector<Point> data(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    data[i] = Point(x, y, i);
  }
  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> rand_n(0, n - 1);
  for (int i = 0; i < n; i++) {
    swap(data[rand_n(mt)], data[rand_n(mt)]);
  }
  Tree T;
  for (int i = 0; i < n; i++) {
    T.add_data(data[i].x, data[i].y, data[i].l);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    set<int> ans;
    int sx, tx, sy, ty;
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    T.find_data(sx, tx, sy, ty, ans);
    for (auto it = ans.begin(); it != ans.end(); it++) {
      printf("%d\\n", *it);
    }
    printf("\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <set>
using namespace std;

struct Node {
  Node *left = NULL;
  Node *right = NULL;
  int x;
  int y;
  int location;
  Node(int x, int y, int location) : x(x), y(y), location(location) {}
  Node() {}
};
class Tree {
private:
  Node *root = NULL;
  void add_Node(Node *n, Node *&pos, int depth) {
    if (pos == NULL) {
      pos = n;
      return;
    }
    if (depth % 2 == 0) {
      if (n->y < pos->y) {
        add_Node(n, pos->left, depth + 1);
      } else {
        add_Node(n, pos->right, depth + 1);
      }
    } else {
      if (n->x < pos->x) {
        add_Node(n, pos->left, depth + 1);
      } else {
        add_Node(n, pos->right, depth + 1);
      }
    }
  }
  void find_Node(int sx, int tx, int sy, int ty, set<int> &ans, Node *pos,
                 int depth) {
    if (pos == NULL)
      return;
    if (sx <= pos->x && pos->x <= tx && sy <= pos->y && pos->y <= ty) {
      ans.insert(pos->location);
    }
    if (depth % 2 == 0) {
      if (sy <= pos->y) {
        find_Node(sx, tx, sy, ty, ans, pos->left, depth + 1);
      }
      if (pos->y <= ty) {
        find_Node(sx, tx, sy, ty, ans, pos->right, depth + 1);
      }
    } else {
      if (sx <= pos->x) {
        find_Node(sx, tx, sy, ty, ans, pos->left, depth + 1);
      }
      if (pos->x <= tx) {
        find_Node(sx, tx, sy, ty, ans, pos->right, depth + 1);
      }
    }
  }

public:
  void add_data(int x_in, int y_in, int loc_in) {
    Node *n = new Node(x_in, y_in, loc_in);
    add_Node(n, root, 0);
  }
  void find_data(int sx, int tx, int sy, int ty, set<int> &ans) {
    find_Node(sx, tx, sy, ty, ans, root, 0);
  }
};

int main() {
  int n;
  scanf("%d", &n);
  Tree T;
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    T.add_data(x, y, i);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    set<int> ans;
    int sx, tx, sy, ty;
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    T.find_data(sx, tx, sy, ty, ans);
    for (auto it = ans.begin(); it != ans.end(); it++) {
      printf("%d\\n", *it);
    }
    printf("\\n");
  }
  return 0;
}