Spheres and queries

Given N points in 3D and M spheres, answer for each sphere how many points lie inside it, counting boundary points.

Medium6GeometrySortingBinary searchImplementationNo attempts yetTime limit20sMemory limit512 MB

Problem

There are NN points in three-dimensional space. Each query names one sphere and asks how many points lie inside it.

  • x y z r: print how many points lie inside the sphere with center (x,y,z)(x, y, z) and radius rr.

A point on the surface of the sphere counts as a point inside it. That is, the point (a,b,c)(a, b, c) lies inside the sphere when (ax)2+(by)2+(cz)2r2(a - x)^2 + (b - y)^2 + (c - z)^2 \le r^2. Several points may share the same coordinates, and each of them is counted separately.

Input

The first line contains the number of points NN. (1N100,0001 \le N \le 100{,}000)

Each of the next NN lines contains the integer coordinates xx, yy, zz of one point. (0x,y,z1,000,0000 \le x, y, z \le 1{,}000{,}000)

The next line contains the number of queries MM. (1M100,0001 \le M \le 100{,}000)

Each of the next MM lines contains one query as the integers xx, yy, zz, rr. (0x,y,z1,000,0000 \le x, y, z \le 1{,}000{,}000, 1r1,000,0001 \le r \le 1{,}000{,}000)

Output

For each query, print the answer on its own line, in the order the queries are given.