#bzoj3053. The Closest M Points

    ID: 3392 远端评测题 1000ms 256MiB 尝试: 1 已通过: 0 难度: 7.11 上传者: 标签>图结构其他数学数据结构k-d树bzoj

The Closest M Points

Title Description

The course of Software Design and Development Practice is objectionable. ZLC\text{ZLC} is facing a serious problem .There are many points in KK-dimensional space .Given a point. ZLC\text{ZLC} need to find out the closest m points.

Euclidean distance is used as the distance metric between two points. The Euclidean distance between points pp and qq is the length of the line segment connecting them.In Cartesian coordinates, if p=(p1,p2,,pn)p = (p_1, p_2,\ldots, p_n) and q=(q1,q2,,qn)q = (q_1, q_2,\ldots, q_n) are two points in Euclidean nn-space, then the distance from pp to qq, or from qq to pp is given by:

$$D(p,q)=D(q,p)=\sqrt{(q_1-p_1)^2+(q_2-p_2)^2+(q_3-p_3)^2\ldots+(q_n-p_n)^2} $$

Can you help him solve this problem?

Input format

In the first line of the text file .there are two non-negative integers nn and KK. They denote respectively: the number of points, and the number of Dimensions.

In each of the following nn lines there is written kk integers, representing the coordinates of a point. This followed by a line with one positive integer tt, representing the number of queries.

Each query contains two lines. The kk integers in the first line represent the given point. In the second line, there is one integer mm, the number of closest points you should find.

The absolute value of all the coordinates will not be more than 1000010000.

There are multiple test cases. Process to end of file.

Output format

For each query, output m+1m+1 lines:

  • The first line saying :the closest m points are: where mm is the number of the points.
  • The following mm lines representing mm points ,in accordance with the order from near to far.

It is guaranteed that the answer can only be formed in one ways. The distances from the given point to all the nearest m+1m+1 points are different. That means input like this:

2 2
1 1
3 3
1
2 2
1

will not exist.

Samples

3 2
1 1
1 3
3 4
2
2 3
2
2 3
1
the closest 2 points are:
1 3
3 4
the closest 1 points are:
1 3

Data scale and agreement

For 100%100\% of the data:$1\le k\le 5,1\le n\le 50000,1\le m\le 10,1\le t\le 10000$.