BZOJ 3053 (The Closest M Points-n-dimensional KD_Tree), points-nkd_tree

Source: Internet
Author: User

BZOJ 3053 (The Closest M Points-n-dimensional KD_Tree), points-nkd_tree

3053: The Closest M PointsTime Limit: 10 Sec Memory Limit: 128 MB
Submit: 442 Solved: 173
[Submit] [Status] [Discuss] Description

The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem. there are using points in K-dimen1_space. given a point. 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 p and q is the length of the line segment connecting them. in Cartesian coordinates, if p = (p1, p2 ,..., pn) and q = (q1, q2 ,..., qn) are two points in Euclidean n-space, then the distance from p to q, or from q to p is given:
D (p, q) = D (q, p) = sqrt (q1-p1) ^ 2 + (q2-p2) ^ 2 + (q3-p3) ^ 2... + (Qn-pn) ^ 2
Can you help him solve this problem?


Soft Institute of Technology courses are annoying! Comrade ZLC encountered a headache: there are many points in the K-dimensional space. For some given points, ZLC needs to find m points closest to it.

(The distance here refers to the Euclidean distance: D (p, q) = D (q, p) = sqrt (q1-p1) ^ 2 + (q2-p2) ^ 2 + (q3-p3) ^ 2 +... + (qn-pn) ^ 2)

ZLC is going to fight Dota, so you have to help solve it ......

[Input]

The first line has two non-negative integers: Point n (1 <= n <= 50000), and dimension k (1 <= k <= 5 ).
In the next n rows, each line contains k integers, representing the coordinates of a point.
The next positive integer: The given number of queries t (1 <= t <= 10000)
The following 2 * t lines:
The first line, k integers: coordinates of the given point
Row 2: query the nearest m points (1 <= m <= 10)

The absolute values of all coordinates cannot exceed 10000.
There are multiple groups of data!

[Output]

For each query, output m + 1 line:
The first line: "the closest m points are:" m is m in the query
Next, each row in the m line represents a vertex, sorted from near to far.

Ensure that the solution is unique. The following situations do not occur:
2 2
1 1
3 3
1
2 2
1

Input

In the first line of the text file. there are two non-negative integers n and K. they denote respectively: the number of points, 1 <= n <= 50000, and the number of Dimensions, 1 <= K <= 5. in each of the following n lines there is written k integers, representing the coordinates of a point. this followed by a line with one positive integer t, representing the number of queries, 1 <= t <= 10000. each query contains two lines. the k integers in the first line represent the given point. in the second line, there is one integer m, the number of closest points you shoshould find, 1 <= m <= 10. the absolute value of all the coordinates will not be more than 10000.
There are multiple test cases. Process to end of file.

Output

For each query, output m + 1 lines:
The first line saying: "the closest m points are:" where m is the number of the points.
The following m lines representing m 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 + 1 points are different. That means input like this:
2 2
1 1
3 3
1
2 2
1
Will not exist.

Sample Input3 2
1 1
1 3
3 4
2
2 3
2
2 3
1
Sample Outputthe closest 2 points are:
1 3
3 4
The closest 1 points are:
1 3
HINT

Source

K-d tree

[Submit] [Status] [Discuss]

Bytes


N-dimensional KD_Tree

Note: Use cin on Bzoj and RE


Python test data, and std

import randomimport sys#os.mknod("data.txt")fp=open("bzoj3053_2.in",'w')sys.stdout=fpmaxxi=10000n=50000k=5print(n,k)for i in range(0,n):    for j in range(0,k):        print(random.randint(-maxxi,maxxi+1),end=' ')    print('')t=100print(t)for i in range(0,t):    for j in range(0,k):        print(random.randint(-maxxi,maxxi+1),end=' ')    print('')    print(random.randint(1,10))fp.close()





# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <functional> # include <iostream> # include <cmath> # include <queue> using namespace std; # define For (I, n) for (int I = 1; I <= n; I ++) # define Fork (I, k, n) for (int I = k; I <= n; I ++) # define Rep (I, n) for (int I = 0; I <n; I ++) # define ForD (I, n) for (int I = n; I --) # define RepD (I, n) for (int I = n; I> = 0; I --) # define Forp (x) for (int p = pre [x]; p = next [p]) # define Forpiter (x) for (int & p = iter [x]; p = next [p]) # define Lson (x <1) # define Rson (x <1) + 1) # define MEM (a) memset (a, 0, sizeof (a); # define MEMI () memset (a, 127, sizeof (a); # define MEMi (a) memset (a, 128, sizeof (a); # define INF (2139062143) # define F (100000007) # define MAXN (100000 + 10) # define MAXK (5) # define MAXT (10000 + 10) # define MAXXi (10000) # define MAXM (10 + 10) typedef long ll; ll mul (ll a, ll B) {return (A * B) % F;} ll add (ll a, ll B) {return (a + B) % F;} ll sub (ll a, ll B) {return (a-B + (a-B)/F * F + F) % F;} void upd (ll & a, ll B) {a = (a % F + B % F) % F;} int n, K, t, m; int cmp_d = 0; class node {public: int x [MAXK]; int l, r, minv [MAXK], maxv [MAXK]; int & operator [] (int I) {return x [I];} node () {} node (int a []) {memcpy (x, a, sizeof (int) * K); l = r = 0; Rep (I, K) minv [I] = maxv [I] = x [I];} void read () {Rep (I, K) scanf ("% d ", & x [I]); l = r = 0; Rep (I, K) minv [I] = maxv [I] = X [I];} void print () {printf ("% d", x [0]); For (I, K-1) printf ("% d ", x [I]); printf ("\ n") ;}}; priority_queue <pair <int, int>, vector <pair <int, int>, greater <vector <pair <int, int> >:: value_type> q, qans; int sqr (int x) {return x * x;} int dis (node, node B) {int ans = 0; Rep (I, K) ans + = sqr (. x [I]-B. x [I]); return ans;} int dis2 (node p, node a) // Euclidean distance between point p and Square area a {int ans = 0; Rep (I, k) {if (p. x [I] <. minv [I]) ans + = sqr (. min V [I]-p. x [I]); elseif (p. x [I]>. maxv [I]) ans + = sqr (p. x [I]-. maxv [I]);} return ans;} int cmp (node a, node B) {return a [cmp_d] <B [cmp_d];} class KD_Tree {public: node a [MAXN]; void update (node & o) {if (o. l) {node p = a [o. l]; Rep (I, K) o. minv [I] = min (o. minv [I], p. minv [I]); Rep (I, K) o. maxv [I] = max (o. maxv [I], p. maxv [I]);} if (o. r) {node p = a [o. r]; Rep (I, K) o. minv [I] = min (o. minv [I], p. minv [I]); Rep (I, K) o. maxv [I] = max (o. maxv [I], p. max V [I]) ;}} int build (int L, int R, int nowd) {int m = (L + R)> 1; cmp_d = nowd; nth_element (a + L, a + m, a + R + 1, cmp); if (L ^ m) a [m]. l = build (L, M-1, (nowd + 1) % K); if (R ^ m) a [m]. r = build (m + 1, R, (nowd + 1) % K); update (a [m]); return m;} int root; void _ build (int L, int R, int nowd) // nodes of 1-N must be at least 1 {root = build (L, R, nowd);} node _ p; int _ ans; void ask_min_dis (int o) {if (o = 0) return; q. push (make_pair (dis2 (_ p, a [o]), o); qans. push (make_pair (dis (_ p, A [o]), o); while (m &&! Q. empty () {o = q. top (). second; if (o = 0) return; // cout <q. size () <':' <o <endl; int nowdis = q. top (). first; while (m &&! Qans. empty () & nowdis> = qans. top (). first) {a [qans. top (). second]. print (); qans. pop (); m --;} if (m = 0) return; q. pop (); int ans1 = a [o]. l? Dis2 (_ p, a [a [o]. l]): INF; // the minimum value of the distance between point p and any point in the area int ans2 = a [o]. r? Dis2 (_ p, a [a [o]. r]): INF; pair <int, int> p1 = make_pair (ans1, a [o]. l); pair <int, int> p2 = make_pair (ans2, a [o]. r); if (ans1 <ans2) {if (ans1 <_ ans) qans. push (make_pair (dis (_ p, a [a [o]. l]), a [o]. l), q. push (p1); if (ans2 <_ ans) qans. push (make_pair (dis (_ p, a [a [o]. r]), a [o]. r), q. push (p2);} else {if (ans2 <_ ans) qans. push (make_pair (dis (_ p, a [a [o]. r]), a [o]. r), q. push (p2); if (ans1 <_ ans) qans. push (make_pair (dis (_ p, a [a [o]. l ]), A [o]. l), q. push (p1) ;}} while (m &&! Qans. empty () {a [qans. top (). second]. print (); qans. pop (); m -- ;}} int _ ask (node p) {while (! Q. empty () q. pop (); while (! Qans. empty () qans. pop (); _ p = p; _ ans = INF; ask_min_dis (root); return _ ans;} S; int main () {// freopen ("bzoj3053_2.in ", "r", stdin); // freopen ("bzoj3053.out", "w", stdout); while (cin> n> K) {cmp_d = 0; for (I, n) {S. a [I]. read ();} S. _ build (1, n, 0); cin> t; For (I, t) {node p; p. read (); scanf ("% d", & m); printf ("the closest % d points are: \ n", m); S. _ ask (p) ;}} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.