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

// Drawing heavily from 2579705 by vjudge2
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2579705#1
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define NMAX 500010

int dim = 0, tree_idx = 1;

struct Point {
  int xy[2], id, left, right;
  bool operator<(const Point &other) const { return xy[dim] < other.xy[dim]; }
} P[NMAX], tree[NMAX];
istream &operator>>(istream &is, Point &p) { return is >> p.xy[0] >> p.xy[1]; }

int Build(int L, int R, int d) {
  int M = (L + R) / 2;
  dim = d;
  nth_element(P + L, P + M, P + R + 1); // now, P + M is the median of P+L...P+R
  int curr = ++tree_idx;
  tree[curr] = P[M]; // store values at internal nodes, too
  if (L < M)
    tree[curr].left = Build(L, M - 1, !d);
  if (R > M)
    tree[curr].right = Build(M + 1, R, !d);
  //  cout << "Built " << curr << " (" << tree[curr].xy[0] << ", "
  //       << tree[curr].xy[1] << ") " << tree[curr].left << " " <<
  //       tree[curr].right
  //       << endl;
  return curr;
}

void Find(int curr, int d, int x1, int x2, int y1, int y2, set<int> &res) {
  //  cout << "Find " << curr << " " << d << endl;
  if (curr == 0)
    return;
  if (tree[curr].xy[0] >= x1 and tree[curr].xy[0] <= x2 and
      tree[curr].xy[1] >= y1 and tree[curr].xy[1] <= y2) {
    //    cout << "Match at " << curr << endl;
    res.insert(tree[curr].id);
  }
  if (d == 0) {
    if (x1 <= tree[curr].xy[0]) {
      Find(tree[curr].left, !d, x1, x2, y1, y2, res);
    }
    if (x2 >= tree[curr].xy[0]) {
      Find(tree[curr].right, !d, x1, x2, y1, y2, res);
    }
  } else {
    if (y1 <= tree[curr].xy[1]) {
      Find(tree[curr].left, !d, x1, x2, y1, y2, res);
    }
    if (y2 >= tree[curr].xy[1]) {
      Find(tree[curr].right, !d, x1, x2, y1, y2, res);
    }
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, q, x1, x2, y1, y2;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    P[i].id = i;
    cin >> P[i];
  }
  int root = Build(0, n - 1, 0);
  set<int> res;
  cin >> q;
  while (cin >> x1 >> x2 >> y1 >> y2) {
    res.clear();
    Find(root, 0, x1, x2, y1, y2, res);
    for (int x : res)
      cout << x << endl;
    cout << endl;
  }
}


Error: TLE

Fixed submission:

// Drawing heavily from 2579705 by vjudge2
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2579705#1
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define NMAX 500010

int dim = 0, tree_idx = 1;

struct Point {
  int xy[2], id, left, right;
  bool operator<(const Point &other) const { return xy[dim] < other.xy[dim]; }
} P[NMAX], tree[NMAX];
istream &operator>>(istream &is, Point &p) { return is >> p.xy[0] >> p.xy[1]; }

int Build(int L, int R, int d) {
  int M = (L + R) / 2;
  dim = d;
  nth_element(P + L, P + M, P + R + 1); // now, P + M is the median of P+L...P+R
  int curr = ++tree_idx;
  tree[curr] = P[M]; // store values at internal nodes, too
  if (L < M)
    tree[curr].left = Build(L, M - 1, !d);
  if (R > M)
    tree[curr].right = Build(M + 1, R, !d);
  //  cout << "Built " << curr << " (" << tree[curr].xy[0] << ", "
  //       << tree[curr].xy[1] << ") " << tree[curr].left << " " <<
  //       tree[curr].right
  //       << endl;
  return curr;
}

void Find(int curr, int d, int x1, int x2, int y1, int y2, set<int> &res) {
  //  cout << "Find " << curr << " " << d << endl;
  if (curr == 0)
    return;
  if (tree[curr].xy[0] >= x1 and tree[curr].xy[0] <= x2 and
      tree[curr].xy[1] >= y1 and tree[curr].xy[1] <= y2) {
    //    cout << "Match at " << curr << endl;
    res.insert(tree[curr].id);
  }
  if (d == 0) {
    if (x1 <= tree[curr].xy[0]) {
      Find(tree[curr].left, !d, x1, x2, y1, y2, res);
    }
    if (x2 >= tree[curr].xy[0]) {
      Find(tree[curr].right, !d, x1, x2, y1, y2, res);
    }
  } else {
    if (y1 <= tree[curr].xy[1]) {
      Find(tree[curr].left, !d, x1, x2, y1, y2, res);
    }
    if (y2 >= tree[curr].xy[1]) {
      Find(tree[curr].right, !d, x1, x2, y1, y2, res);
    }
  }
}

int main() {
  int n, q, x1, x2, y1, y2;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    P[i].id = i;
    cin >> P[i];
  }
  int root = Build(0, n - 1, 0);
  set<int> res;
  cin >> q;
  while (cin >> x1 >> x2 >> y1 >> y2) {
    res.clear();
    Find(root, 0, x1, x2, y1, y2, res);
    for (int x : res)
      cout << x << endl;
    cout << endl;
  }
}