Language: C++

#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(int x, int y, int id = 0) :id(id), x(x), y(y) {}
	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;

	bool inrange(const Point &p, const Point &s, const Point &t) {
		return (s.x <= p.x&&p.x <= t.x) && (s.y <= p.y&&p.y <= t.y);
	}
	KDTree(const vector<Point>&ps) :points(ps), n(ps.size()), np(0), nodes(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;
		int t = np++;
		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;
	vector<Point> find(Point s, Point t) {
		ret.clear();
		return find(s, t, root, true);
	}
	vector<Point> find(Point s, Point t, int v, 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(s, t, nodes[v].l, !is_x_base);
			if (nodes[v].r != NIL && p.x <= t.x)find(s, t, nodes[v].r, !is_x_base);
		}
		else {
			if (nodes[v].l != NIL && s.y <= p.y)find(s, t, nodes[v].l, !is_x_base);
			if (nodes[v].r != NIL && p.y <= t.y)find(s, t, nodes[v].r, !is_x_base);
		}
		return ret;
	}

};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N; scanf("%d", &N);
	vector<Point>v(N);
	rep(i, 0, N) {
		int x, y; cin >> x >> y;
		v[i] = (Point(x, y, i));
	}
	KDTree tree(v);
	int q; scanf("%d", &q);
	rep(i, 0, q) {
		int sx, tx, sy, ty; scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
		Point s(sx, sy), t(tx, ty);
		auto ans = tree.find(s, t);
		sort(all(ans));
		rep(j, 0, ans.size())ans[j].print();
		printf("\n");
	}

	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/bits/stl_tempbuf.h:61,
                 from /usr/include/c++/14/bits/stl_algo.h:69,
                 from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_construct.h: In instantiation of 'void std::_Construct(_Tp*, _Args&& ...) [with _Tp = Point; _Args = {}]':
/usr/include/c++/14/bits/stl_uninitialized.h:643:18:   required from 'static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Point*; _Size = long unsigned int; bool _TrivialValueType = false]'
  643 |                 std::_Construct(std::__addressof(*__cur));
      |                 ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_uninitialized.h:712:20:   required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Point*; _Size = long unsigned int]'
  710 |       return __uninitialized_default_n_1<__is_trivial(_ValueType)
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  711 |                                          && __can_fill>::
      |                                          ~~~~~~~~~~~~~~~~
  712 |         __uninit_default_n(__first, __n);
      |         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_uninitialized.h:779:44:   required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, allocator<_Tp>&) [with _ForwardIterator = Point*; _Size = long unsigned int; _Tp = Point]'
  779 |     { return std::__uninitialized_default_n(__first, __n); }
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1720:36:   required from 'void std::vector<_Tp, _Alloc>::_M_default_initialize(size_type) [with _Tp = Point; _Alloc = std::allocator<Point>; size_type = long unsigned int]'
 1720 |           std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1721 |                                            _M_get_Tp_allocator());
      |                                            ~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:558:9:   required from 'std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp = Point; _Alloc = std::allocator<Point>; size_type = long unsigned int; allocator_type = std::allocator<Point>]'
  558 |       { _M_default_initialize(__n); }
      |         ^~~~~~~~~~~~~~~~~~~~~
a.cc:82:18:   required from here
   82 |         vector<Point>v(N);
      |                         ^
/usr/include/c++/14/bits/stl_construct.h:119:7: error: no matching function for call to 'Point::Point()'
  119 |       ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:22:9: note: candidate: 'Point::Point(int, int, int)'
   22 |         Point(int x, int y, int id = 0) :id(id), x(x), y(y) {}
      |         ^~~~~
a.cc:22:9: note:   candidate expects 3 arguments, 0 provided
a.cc:20:8: note: candidate: 'constexpr Point::Point(const Point&)'
   20 | struct Point {
      |        ^~~~~
a.cc:20:8: note:   candidate expects 1 argument, 0 provided
a.cc:20:8: note: candidate: 'constexpr Point::Point(Point&&)'
a.cc:20:8: note:   candidate expects 1 argument, 0 provided