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

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <vector>
#define compare(d, p, q)                                                       \\
  (d ? real(p.point) < real(q.point) : imag(p.point) < imag(q.point))
using namespace std;

const int INF = INT_MAX / 3;

typedef complex<double> Point;

struct P {
  Point point;
  int id;
  P(const Point &point, int id) : point(point), id(id) {}
  bool operator<(const P &p) const { return id < p.id; }
};

struct node {
  P p;
  node *l, *r;
  node(const P &p) : p(p), l(NULL), r(NULL) {}
};

struct kDTree {
  node *root;
  kDTree() : root(NULL) {}

  void insert(const P &p) { root = insert(root, 0, p); }

  node *insert(node *t, int d, const P &p) {
    if (t == NULL)
      return new node(p);
    if (compare(d, p, t->p))
      t->l = insert(t->l, !d, p);
    else
      t->r = insert(t->r, !d, p);
    return t;
  }

  void find(const P &ld, const P &ru, vector<P> &out) {
    find(root, 0, ld, ru, out);
  }

  void find(node *t, int d, const P &ld, const P &ru, vector<P> &out) {
    if (t == NULL)
      return;
    const P &p = t->p;
    if (real(ld.point) <= real(p.point) && real(p.point) <= real(ru.point) &&
        imag(ld.point) <= imag(p.point) && imag(p.point) <= imag(ru.point)) {
      out.push_back(p);
    }
    if (!compare(d, p, ld))
      find(t->l, !d, ld, ru, out);
    if (!compare(d, ru, p))
      find(t->r, !d, ld, ru, out);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  random_device rnd;
  mt19937 mt(rnd());
  uniform_real_distribution<> ran(-0.1, 0.1);

  vector<P> inputList;
  int n, q, x, y, sx, tx, sy, ty;
  cin >> n;
  kDTree k;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    Point point(x + ran(mt), y + ran(mt));
    P p(point, i);
    inputList.push_back(p);
  }
  shuffle(inputList.begin(), inputList.end(), mt);
  for (int i = 0; i < n; i++) {
    k.insert(inputList[i]);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> sx >> tx >> sy >> ty;
    vector<P> result;
    Point ldPoint(sx - 0.5, sy - 0.5);
    P ld(ldPoint, INF);
    Point ruPoint(tx + 0.5, ty + 0.5);
    P ru(ruPoint, INF);
    k.find(ld, ru, result);
    sort(result.begin(), result.end());
    for (int j = 0; j < result.size(); j++) {
      printf("%d\\n", result[j].id);
    }
    printf("\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <vector>
#define compare(d, p, q)                                                       \\
  (d ? real(p.point) < real(q.point) : imag(p.point) < imag(q.point))
using namespace std;

const int INF = INT_MAX / 3;

typedef complex<double> Point;

struct P {
  Point point;
  int id;
  P(const Point &point, int id) : point(point), id(id) {}
  bool operator<(const P &p) const { return id < p.id; }
};

struct node {
  P p;
  node *l, *r;
  node(const P &p) : p(p), l(NULL), r(NULL) {}
};

struct kDTree {
  node *root;
  kDTree() : root(NULL) {}

  void insert(const P &p) { root = insert(root, 0, p); }

  node *insert(node *t, int d, const P &p) {
    if (t == NULL)
      return new node(p);
    if (compare(d, p, t->p))
      t->l = insert(t->l, !d, p);
    else
      t->r = insert(t->r, !d, p);
    return t;
  }

  void find(const P &ld, const P &ru, vector<P> &out) {
    find(root, 0, ld, ru, out);
  }

  void find(node *t, int d, const P &ld, const P &ru, vector<P> &out) {
    if (t == NULL)
      return;
    const P &p = t->p;
    if (real(ld.point) <= real(p.point) && real(p.point) <= real(ru.point) &&
        imag(ld.point) <= imag(p.point) && imag(p.point) <= imag(ru.point)) {
      out.push_back(p);
    }
    if (!compare(d, p, ld))
      find(t->l, !d, ld, ru, out);
    if (!compare(d, ru, p))
      find(t->r, !d, ld, ru, out);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  random_device rnd;
  mt19937 mt(rnd());
  uniform_real_distribution<> ran(-0.1, 0.1);

  vector<P> inputList;
  int n, q, x, y, sx, tx, sy, ty;
  cin >> n;
  kDTree k;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    Point point(x + ran(mt), y + ran(mt));
    P p(point, i);
    inputList.push_back(p);
  }
  shuffle(inputList.begin(), inputList.end(), mt);
  for (int i = 0; i < n; i++) {
    k.insert(inputList[i]);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> sx >> tx >> sy >> ty;
    vector<P> result;
    Point ldPoint(sx - 0.5, sy - 0.5);
    P ld(ldPoint, INF);
    Point ruPoint(tx + 0.5, ty + 0.5);
    P ru(ruPoint, INF);
    k.find(ld, ru, result);
    sort(result.begin(), result.end());
    for (int j = 0; j < result.size(); j++) {
      cout << result[j].id << endl;
    }
    cout << endl;
  }
  return 0;
}