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

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

using namespace std;

int D;

struct OwnNode {
  int x, y;
  int index;
  int right;
  int left;

  OwnNode() : x(-1), y(-1), index(-1), right(-1), left(-1) {}
  OwnNode(int x, int y) : x(x), y(y) {}

  bool operator<(const OwnNode &n) const {
    if (D % 2 == 0) {
      return x < n.x;
    } else {
      return y < n.y;
    }
  }
  bool operator>(const OwnNode &n) const {
    if (D % 2 == 0) {
      return x > n.x;
    } else {
      return y > n.y;
    }
  }
};

OwnNode nodes[500000];

void find(int index, OwnNode &ld, OwnNode &ru, int d, vector<int> &v) {
  if (ld.x <= nodes[index].x && nodes[index].x <= ru.x &&
      ld.y <= nodes[index].y && nodes[index].y <= ru.y) {
    v.push_back(nodes[index].index);
  }

  if (d % 2 == 0) {
    if (ld.x <= nodes[index].x) {
      if (nodes[index].left != -1) {
        find(nodes[index].left, ld, ru, d + 1, v);
      }
    }
    if (nodes[index].x <= ru.x) {
      if (nodes[index].right != -1) {
        find(nodes[index].right, ld, ru, d + 1, v);
      }
    }
  } else {
    if (ld.y <= nodes[index].y) {
      if (nodes[index].left != -1) {
        find(nodes[index].left, ld, ru, d + 1, v);
      }
    }
    if (nodes[index].y <= ru.y) {
      if (nodes[index].right != -1) {
        find(nodes[index].right, ld, ru, d + 1, v);
      }
    }
  }
}

int BuildKDTree(int s, int e, int d) {
  int m = (s + e) / 2;
  D = d;
  nth_element(nodes + s, nodes + m, nodes + e + 1);
  nodes[m].left = m == s ? -1 : BuildKDTree(s, m - 1, d + 1);
  nodes[m].right = m == e ? -1 : BuildKDTree(m + 1, e, d + 1);
  return m;
}

int main() {
  int n, q;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    nodes[i].x = x;
    nodes[i].y = y;
    nodes[i].index = i;
  }

  int root = BuildKDTree(0, n - 1, 0);

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
    OwnNode n1(x1, y1), n2(x2, y2);
    vector<int> v;
    find(root, n1, n2, 0, v);
    sort(v.begin(), v.end());
    for (auto &n : v) {
      cout << n << endl;
    }
    cout << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

using namespace std;

struct OwnNode {
  int x, y;
  int index;
  OwnNode *right;
  OwnNode *left;

  OwnNode(int x, int y, int index)
      : x(x), y(y), index(index), right(nullptr), left(nullptr) {}

  void insert(int x, int y, int d, int index) {
    if (d % 2 == 0) {
      if (this->x < x) {
        if (right == nullptr) {
          right = new OwnNode(x, y, index);
        } else {
          right->insert(x, y, d + 1, index);
        }
      } else {
        if (left == nullptr) {
          left = new OwnNode(x, y, index);
        } else {
          left->insert(x, y, d + 1, index);
        }
      }
    } else {
      if (this->y < y) {
        if (right == nullptr) {
          right = new OwnNode(x, y, index);
        } else {
          right->insert(x, y, d + 1, index);
        }
      } else {
        if (left == nullptr) {
          left = new OwnNode(x, y, index);
        } else {
          left->insert(x, y, d + 1, index);
        }
      }
    }
  }

  void find(OwnNode &ld, OwnNode &ru, int d,
            priority_queue<int, vector<int>, greater<int>> &pq) {
    if (ld.x <= x && x <= ru.x && ld.y <= y && y <= ru.y) {
      pq.push(index);
    }

    if (d % 2 == 0) {
      if (ld.x <= x) {
        if (left != nullptr) {
          left->find(ld, ru, d + 1, pq);
        }
      }
      if (x <= ru.x) {
        if (right != nullptr) {
          right->find(ld, ru, d + 1, pq);
        }
      }
    } else {
      if (ld.y <= y) {
        if (left != nullptr) {
          left->find(ld, ru, d + 1, pq);
        }
      }
      if (y <= ru.y) {
        if (right != nullptr) {
          right->find(ld, ru, d + 1, pq);
        }
      }
    }
  }
};

int main() {
  OwnNode *top = nullptr;
  int n, q;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (top == nullptr) {
      top = new OwnNode(x, y, i);
    } else {
      top->insert(x, y, 0, i);
    }
  }

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
    OwnNode n1(x1, y1, -1), n2(x2, y2, -1);
    priority_queue<int, vector<int>, greater<int>> pq;
    top->find(n1, n2, 0, pq);
    while (!pq.empty()) {
      cout << pq.top() << endl;
      pq.pop();
    }
    cout << endl;
  }

  return 0;
}