Problem p02347 (C++) — buggy submission, verdict: Runtime Error

#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 int NIL = -1;
const double PI = acos(-1);
const double EPS = 1e-9;
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, id;
  Point() {}
  Point(int x, int y, int id = 0) : x(x), y(y), id(id) {}
  bool operator<(const Point &p) const { return id < p.id; }
  void print() { printf("%d\\n", id); }
};
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() : index(NIL), l(NIL), r(NIL) {}
  };
  int n, np, root;
  vector<Point> points;
  vector<Node> nodes;
  KDtree(const vector<Point> &ps) : n(ps.size()), np(0), points(ps), nodes(n) {
    root = makeKDtree(0, n, true);
  }
  int makeKDtree(int l, int r, bool is_x_base = true) {
    if (!(l < r))
      return NIL;
    int t = np++, mid = (l + r) / 2;
    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].index = mid;
    nodes[t].l = makeKDtree(l, mid, !is_x_base);
    nodes[t].r = makeKDtree(mid + 1, r, !is_x_base);
    return t;
  }
  vector<Point> ret;
  bool inrange(Point &p, Point &s, Point &t) {
    return (s.x <= p.x && p.x <= t.x) && (s.y <= p.y && p.y <= t.y);
  }
  vector<Point> find(Point s, Point t) {
    ret.clear();
    return find(root, s, t, true);
  }
  vector<Point> find(int v, Point s, Point t, bool is_x_base = true) {
    auto p = points[nodes[v].index];
    if (inrange(p, s, t))
      ret.emplace_back(p);
    if (is_x_base) {
      if (nodes[v].l != NIL && s.x <= p.x)
        find(nodes[v].l, s, t, !is_x_base);
      if (nodes[v].r != NIL && p.x <= t.x)
        find(nodes[v].r, s, t, !is_x_base);
    } else {
      if (nodes[v].l != NIL && s.y <= p.y)
        find(nodes[v].l, s, t, !is_x_base);
      if (nodes[v].r != NIL && p.y <= t.y)
        find(nodes[v].r, s, t, !is_x_base);
    }
    return ret;
  }
};
signed main() {
  int n;
  scanf("%d", &n);
  vector<Point> ps(n);
  rep(i, 0, n) {
    int x, y;
    scanf("%d%d", &x, &y);
    ps[i] = Point(x, y, i);
  }
  KDtree tree(ps);
  int q;
  scanf("%d", &q);
  rep(i, 0, q) {
    int sx, tx, sy, ty;
    scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
    auto ans = tree.find(Point(sx, sy), Point(tx, ty));
    sort(all(ans));
    rep(j, 0, ans.size()) ans[j].print();
    printf("\\n");
  }
  return 0;
}

Error: -11

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 int NIL = -1;
const double PI = acos(-1);
const double EPS = 1e-9;
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, id;
  Point() {}
  Point(int x, int y, int id = 0) : x(x), y(y), id(id) {}
  bool operator<(const Point &p) const { return id < p.id; }
  void print() { printf("%d\\n", id); }
};
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() : index(NIL), l(NIL), r(NIL) {}
  };
  int n, np, root;
  vector<Point> points;
  vector<Node> nodes;
  KDtree(const vector<Point> &ps) : n(ps.size()), np(0), points(ps), nodes(n) {
    root = makeKDtree(0, n, true);
  }
  int makeKDtree(int l, int r, bool is_x_base = true) {
    if (!(l < r))
      return NIL;
    int t = np++, mid = (l + r) / 2;
    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].index = mid;
    nodes[t].l = makeKDtree(l, mid, !is_x_base);
    nodes[t].r = makeKDtree(mid + 1, r, !is_x_base);
    return t;
  }
  vector<Point> ret;
  bool inrange(Point &p, Point &s, Point &t) {
    return (s.x <= p.x && p.x <= t.x) && (s.y <= p.y && p.y <= t.y);
  }
  vector<Point> find(Point s, Point t) {
    ret.clear();
    return find(root, s, t, true);
  }
  vector<Point> find(int v, Point s, Point t, bool is_x_base = true) {
    auto p = points[nodes[v].index];
    if (inrange(p, s, t))
      ret.emplace_back(p);
    if (is_x_base) {
      if (nodes[v].l != NIL && s.x <= p.x)
        find(nodes[v].l, s, t, !is_x_base);
      if (nodes[v].r != NIL && p.x <= t.x)
        find(nodes[v].r, s, t, !is_x_base);
    } else {
      if (nodes[v].l != NIL && s.y <= p.y)
        find(nodes[v].l, s, t, !is_x_base);
      if (nodes[v].r != NIL && p.y <= t.y)
        find(nodes[v].r, s, t, !is_x_base);
    }
    return ret;
  }
};
signed main() {
  int n;
  scanf("%d", &n);
  vector<Point> ps(n);
  rep(i, 0, n) {
    int x, y;
    scanf("%d%d", x, y);
    ps[i] = Point(x, y, i);
  }
  KDtree tree(ps);
  int q;
  scanf("%d", &q);
  rep(i, 0, q) {
    int sx, tx, sy, ty;
    scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
    auto ans = tree.find(Point(sx, sy), Point(tx, ty));
    sort(all(ans));
    rep(j, 0, ans.size()) ans[j].print();
    printf("\\n");
  }
  return 0;
}