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

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

class Node {
public:
  int location;
  int p, l, r;
  Node(){};
};

class Point {
public:
  int id, x, y;
  Point(){};
  Point(int id, int x, int y) : id(id), x(x), y(y){};
  bool operator<(const Point &p) const { return id < p.id; }

  void print() { printf("%d\\n", id); }
};

const int maxx = 1000010;
const int NIL = -1;

int n;
Point p[maxx];
Node T[maxx];
int np;

bool lessX(const Point &p1, const Point &p2) { return p1.x < p2.x; }
bool lessY(const Point &p1, const Point &p2) { return p1.y < p2.y; }

int makeKDTree(int l, int r, int depth) {
  if (l >= r)
    return NIL;
  int mid = (l + r) / 2;
  int t = np++;
  if (depth % 2 == 0)
    sort(p + l, p + r, lessX);
  else
    sort(p + l, p + r, lessY);

  T[t].location = mid;
  T[t].l = makeKDTree(l, mid, depth + 1);
  T[t].r = makeKDTree(mid + 1, r, depth + 1);

  return t;
}

void find(int v, int sx, int tx, int sy, int ty, int depth,
          vector<Point> &ans) {
  int x = p[T[v].location].x;
  int y = p[T[v].location].y;

  if (sx <= x && x <= tx && sy <= y && y <= ty)
    ans.push_back(p[T[v].location]);

  if (depth % 2 == 0) {
    if (T[v].l != NIL && sx <= x)
      find(T[v].l, sx, tx, sy, ty, depth + 1, ans);
    if (T[v].r != NIL && x <= tx)
      find(T[v].r, sx, tx, sy, ty, depth + 1, ans);
  } else {
    if (T[v].l != NIL && sy <= y)
      find(T[v].l, sx, tx, sy, ty, depth + 1, ans);
    if (T[v].r != NIL && y <= ty)
      find(T[v].r, sx, tx, sy, ty, depth + 1, ans);
  }
}
int main() {
  int x, y;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x, &y);
    p[i] = Point(i, x, y);
    T[i].l = T[i].r = T[i].p = NIL;
  }

  np = 0;
  int root = makeKDTree(0, n, 0);

  int q;
  int sx, tx, sy, ty;
  vector<Point> ans;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    ans.clear();
    find(root, sx, tx, sy, ty, 0, ans);
    sort(ans.begin(), ans.end());
    for (int j = 0; j < ans.size(); j++)
      ans[j].print();
    printf("\\n");
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define Lens 1000100
#define Nil -1

using namespace std;

struct point {
  int x;
  int y;
  int add;
};

struct node {
  int lc;
  int rc;
  int key;
};

int N, q, h;
point p[Lens];
node T[Lens];
vector<int> ans;
bool lessx(point a, point b) { return a.x < b.x; }
bool lessy(point a, point b) { return a.y < b.y; }

int maketree(int l, int r, int depth) {
  if (l >= r)
    return Nil;
  int mid = (l + r) / 2;
  if (depth % 2 == 0)
    sort(p + l, p + r, lessx);
  else
    sort(p + l, p + r, lessy);
  int t = h++;
  T[t].key = mid;
  T[t].lc = maketree(l, mid, depth + 1);
  T[t].rc = maketree(mid + 1, r, depth + 1);
  return t;
}

void findtree(int root, int lx, int hx, int ly, int ry, int depth) {
  int tr = root;
  int tx = p[T[tr].key].x;
  int ty = p[T[tr].key].y;
  if (lx <= tx && tx <= hx && ly <= ty && ty <= ry)
    ans.push_back(p[T[tr].key].add);
  if (depth % 2 == 0) {
    if (T[tr].lc != Nil && lx <= tx)
      findtree(T[tr].lc, lx, hx, ly, ry, depth + 1);
    if (T[tr].rc != Nil && tx <= hx)
      findtree(T[tr].rc, lx, hx, ly, ry, depth + 1);
  } else {
    if (T[tr].lc != Nil && ly <= ty)
      findtree(T[tr].lc, lx, hx, ly, ry, depth + 1);
    if (T[tr].rc != Nil && ty <= ry)
      findtree(T[tr].rc, lx, hx, ly, ry, depth + 1);
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].add = i;
    T[i].lc = T[i].rc = T[i].key = Nil;
  }
  int root = maketree(0, N, 0);
  cin >> q;
  for (int i = 0; i < q; i++) {
    ans.clear();
    int lx, hx, ly, ry;

    cin >> lx >> hx >> ly >> ry;
    findtree(root, lx, hx, ly, ry, 0);
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++)
      cout << ans[i] << endl;
    cout << endl;
  }
  return 0;
}