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

// ???????????????
#include <algorithm>
#include <vector>
std::vector<int> answer;
// ?????°
const int MAX_N = 500000;
const int NIL = -1;
// ???
struct Vertex {
  int label, x, y;
};
struct Node {
  int left_child, right_child;
};
// ?????°
int root;
Node node[MAX_N];
Vertex vertex[MAX_N];
// ?????????
class Compare {
public:
  Compare(int d) : d(d) {}
  bool operator()(const Vertex &lhs, const Vertex &rhs) const {
    return d & 1 ? lhs.y < rhs.y : lhs.x < rhs.x;
  }

private:
  int d;
};
// ?§????
int build(int low, int high, int depth) {
  if (!(low < high))
    return NIL;
  std::sort(vertex + low, vertex + high, Compare(depth));
  int middle = (low + high) / 2;
  node[middle].left_child = build(low, middle, depth ^ 1);
  node[middle].right_child = build(middle + 1, high, depth ^ 1);
  return middle;
}
// ?????¢?????????????????°?????????????????°??¨???????????¢???????????§?¨????????????§?¨??????????
void search(int index, int sx, int sy, int tx, int ty, int depth) {
  if (index == NIL)
    return;
  if (sx <= vertex[index].x && vertex[index].x <= tx && sy <= vertex[index].y &&
      vertex[index].y <= ty)
    answer.push_back(vertex[index].label);
  if (depth & 1) {
    if (sy <= vertex[index].y)
      search(node[index].left_child, sx, sy, tx, ty, depth ^ 1);
    if (vertex[index].y <= ty)
      search(node[index].right_child, sx, sy, tx, ty, depth ^ 1);
  } else {
    if (sx <= vertex[index].x)
      search(node[index].left_child, sx, sy, tx, ty, depth ^ 1);
    if (vertex[index].x <= tx)
      search(node[index].right_child, sx, sy, tx, ty, depth ^ 1);
  }
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    vertex[i] = Vertex({i, x, y});
  }
  root = build(0, n, 0);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int sx, tx, sy, ty;
    cin >> sx >> tx >> sy >> ty;
    answer.clear();
    search(root, sx, sy, tx, ty, 0);
    sort(begin(answer), end(answer));
    for (auto a : answer)
      cout << a << "\\n";
    cout << "\\n";
  }
}

Error: TLE

Fixed submission:

// ???????????????
#include <algorithm>
#include <vector>
std::vector<int> answer;
// ?????°
const int MAX_N = 500000;
const int NIL = -1;
// ???
struct Vertex {
  int label, x, y, left_child, right_child;
};
// ?????°
int root;
Vertex vertex[MAX_N];
// ?????????
class Compare {
public:
  Compare(int d) : d(d) {}
  bool operator()(const Vertex &lhs, const Vertex &rhs) const {
    return d & 1 ? lhs.y < rhs.y : lhs.x < rhs.x;
  }

private:
  int d;
};
// ?§????
int build(int low, int high, int depth) {
  if (!(low < high))
    return NIL;
  std::sort(vertex + low, vertex + high, Compare(depth));
  int middle = (low + high) / 2;
  vertex[middle].left_child = build(low, middle, depth ^ 1);
  vertex[middle].right_child = build(middle + 1, high, depth ^ 1);
  return middle;
}
// ?????¢?????????????????°?????????????????°??¨???????????¢???????????§?¨????????????§?¨??????????
void search(int node, int sx, int sy, int tx, int ty, int depth) {
  if (node == NIL)
    return;
  if (sx <= vertex[node].x && vertex[node].x <= tx && sy <= vertex[node].y &&
      vertex[node].y <= ty)
    answer.push_back(vertex[node].label);
  if (depth & 1) {
    if (sy <= vertex[node].y)
      search(vertex[node].left_child, sx, sy, tx, ty, depth ^ 1);
    if (vertex[node].y <= ty)
      search(vertex[node].right_child, sx, sy, tx, ty, depth ^ 1);
  } else {
    if (sx <= vertex[node].x)
      search(vertex[node].left_child, sx, sy, tx, ty, depth ^ 1);
    if (vertex[node].x <= tx)
      search(vertex[node].right_child, sx, sy, tx, ty, depth ^ 1);
  }
}

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    vertex[i] = Vertex({i, x, y});
  }
  root = build(0, n, 0);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int sx, tx, sy, ty;
    cin >> sx >> tx >> sy >> ty;
    answer.clear();
    search(root, sx, sy, tx, ty, 0);
    sort(begin(answer), end(answer));
    for (auto a : answer)
      cout << a << endl;
    cout << endl;
  }
}