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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
const int NIL = -1;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct Point {
  int x, y;
  int id;
  Point() {}
  Point(int x, int y, int id = -1) : x(x), y(y), id(id) {}
  bool operator<(const Point &p) { return id < p.id; }
};
ostream &operator<<(ostream &os, Point &p) {
  os << p.id;
  return os;
}
bool lessX(const Point &p1, const Point &p2) {
  return (p1.x == p2.x) ? (p1.y < p2.y) : (p1.x < p2.x);
}
bool lessY(const Point &p1, const Point &p2) {
  return (p1.y == p2.y) ? (p1.x < p2.x) : (p1.y < p2.y);
}
struct KDTree {
  struct Node {
    int index, l, r;
    Node() {}
    Node(int index, int l, int r) : index(index), l(l), r(r) {}
  };
  vector<Node> nodes;
  vector<Point> points, ret;
  int n, root, nn;
  KDTree(const vector<Point> &p) : points(p), n(p.size()), nn(0) {
    nodes.resize(n);
    root = makeKDTree(0, n, true);
  }
  int makeKDTree(int l, int r, bool is_x_base) {
    if (!(l < r))
      return NIL;
    int mid = (l + r) / 2, t = nn++;
    vector<Point>::iterator first = points.begin() + l,
                            nth = points.begin() + mid,
                            last = points.begin() + r;
    if (is_x_base)
      nth_element(first, nth, last, lessX);
    else
      nth_element(first, nth, last, lessY);
    nodes[t] = Node(mid, makeKDTree(l, mid, !is_x_base),
                    makeKDTree(mid + 1, r, !is_x_base));
    return t;
  }
  bool inrange(const Point &z, const Point &p1, const Point &p2) {
    return p1.x <= z.x && z.x <= p2.x && p1.y <= z.y && z.y <= p2.y;
  }
  vector<Point> find(Point &s, Point &t) {
    ret.clear();
    return find(root, true, s, t);
  }
  vector<Point> find(int v, bool is_x_base, const Point &s, const Point &t) {
    Point u = points[nodes[v].index];
    Node node = nodes[v];
    if (inrange(u, s, t))
      ret.emplace_back(u);
    if (is_x_base) {
      if (node.l != NIL && s.x <= u.x)
        find(node.l, !is_x_base, s, t);
      if (node.r != NIL && u.x <= t.x)
        find(node.r, !is_x_base, s, t);
    } else {
      if (node.l != NIL && s.y <= u.y)
        find(node.l, !is_x_base, s, t);
      if (node.r != NIL && u.y <= t.y)
        find(node.r, !is_x_base, s, t);
    }
    return ret;
  }
};
signed main() {
  int N;
  scanf("%d", &N);
  vector<Point> p;
  rep(i, 0, N) {
    int x, y;
    scanf("%d%d", &x, &y);
    p.emplace_back(Point(x, y, i));
  }
  KDTree tree(p);
  int Q;
  scanf("%d", &Q);
  rep(i, 0, Q) {
    int sx, tx, sy, ty;
    scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
    auto s = Point(sx, sy), t = Point(tx, ty);
    auto ret = tree.find(s, t);
    sort(all(ret));
    rep(j, 0, ret.size()) printf("%d\\n", ret[j].id);
    puts("");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
const int NIL = -1;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct Point {
  int x, y;
  int id;
  Point() {}
  Point(int x, int y, int id = -1) : x(x), y(y), id(id) {}
  bool operator<(const Point &p) { return id < p.id; }
};
ostream &operator<<(ostream &os, Point &p) {
  os << p.id;
  return os;
}
bool lessX(const Point &p1, const Point &p2) {
  return (p1.x == p2.x) ? (p1.y < p2.y) : (p1.x < p2.x);
}
bool lessY(const Point &p1, const Point &p2) {
  return (p1.y == p2.y) ? (p1.x < p2.x) : (p1.y < p2.y);
}
struct KDTree {
  struct Node {
    int index, l, r;
    Node() {}
    Node(int index, int l, int r) : index(index), l(l), r(r) {}
  };
  vector<Node> nodes;
  vector<Point> points, ret;
  int n, root, nn;
  KDTree(const vector<Point> &p) : points(p), n(p.size()), nn(0) {
    nodes.resize(n);
    root = makeKDTree(0, n, true);
  }
  int makeKDTree(int l, int r, bool is_x_base) {
    if (!(l < r))
      return NIL;
    int mid = (l + r) / 2, t = nn++;
    vector<Point>::iterator first = points.begin() + l,
                            nth = points.begin() + mid,
                            last = points.begin() + r;
    if (is_x_base)
      nth_element(first, nth, last, lessX);
    else
      nth_element(first, nth, last, lessY);
    nodes[t] = Node(mid, makeKDTree(l, mid, !is_x_base),
                    makeKDTree(mid + 1, r, !is_x_base));
    return t;
  }
  bool inrange(const Point &z, const Point &p1, const Point &p2) {
    return p1.x <= z.x && z.x <= p2.x && p1.y <= z.y && z.y <= p2.y;
  }
  vector<Point> find(Point &s, Point &t) {
    ret.clear();
    return find(root, true, s, t);
  }
  vector<Point> find(int v, bool is_x_base, const Point &s, const Point &t) {
    Point u = points[nodes[v].index];
    Node node = nodes[v];
    if (inrange(u, s, t))
      ret.emplace_back(u);
    if (is_x_base) {
      if (node.l != NIL && s.x <= u.x)
        find(node.l, !is_x_base, s, t);
      if (node.r != NIL && u.x <= t.x)
        find(node.r, !is_x_base, s, t);
    } else {
      if (node.l != NIL && s.y <= u.y)
        find(node.l, !is_x_base, s, t);
      if (node.r != NIL && u.y <= t.y)
        find(node.r, !is_x_base, s, t);
    }
    return ret;
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<Point> p;
  rep(i, 0, N) {
    int x, y;
    cin >> x >> y;
    p.emplace_back(Point(x, y, i));
  }
  KDTree tree(p);
  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int sx, tx, sy, ty;
    cin >> sx >> tx >> sy >> ty;
    auto s = Point(sx, sy), t = Point(tx, ty);
    auto ret = tree.find(s, t);
    sort(all(ret));
    rep(j, 0, ret.size()) cout << ret[j] << endl;
    cout << endl;
  }
  return 0;
}