"BZOJ3053" the Closest M Points kdtree good template One "Array Edition!!! 】

Source: Internet
Author: User

The multi-dimensional valuation is the same as the two dimensions, and the contribution of each dimension is identical to the two-dimensional ' x ', ' Y '.


Really do not understand how to think, there is a difference?!!


Details:


Mom eggs with multiple sets of data! I've been adjusting for a long time.

Then there is no space at the end of the line, otherwise the PE


To get rid of these, I am 1 A.

Unfortunately, there is no "removal" of this saying.


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 50100# Define K 10#define inf 0x3f3f3f3fusing namespace Std;int judge,n,m,q;struct point {int c[k];bool operator < (const poin T &a) Const{return C[judge]<a.c[judge];} int d (point &a) {int ret=0;for (int i=0;i<m;i++) ret+= (C[i]-a.c[i]) * (C[i]-a.c[i]); return ret;}} P[n];struct Ans{int x; Point P;bool operator < (const ANS &a) const {return x<a.x;}} Ans[15];struct Kdt{int son[n][2],cnt; Point C[n][2];void init () {Cnt=0;memset (son,0,sizeof (son));} int NewNode (point &p) {Cnt++,c[cnt][0]=c[cnt][1]=p;return cnt;} void update (int f) {int i,s;if (son[f][0]) {s=son[f][0];for (i=0;i<m;i++) c[f][0].c[i]=min (c[f][0].c[i],c[s][0].c[i ]), C[f][1].c[i]=max (C[f][1].c[i],c[s][1].c[i]);} if (Son[f][1]) {s=son[f][1];for (i=0;i<m;i++) c[f][0].c[i]=min (C[f][0].c[i],c[s][0].c[i]), C[f][1].c[i]=max (C[f] [1].c[i],c[s][1].c[i]);}} int Mindis (int f,point &p) {int ret=0,i;for (i=0;i<m;i++) {if (p.c[I]<c[f][0].c[i]) ret+= (C[f][0].c[i]-p.c[i]) * (C[f][0].c[i]-p.c[i]), else if (C[f][1].c[i]<p.c[i]) ret+= (c[f][ 1].c[i]-p.c[i]) * (C[f][1].c[i]-p.c[i]);} return ret;} int build (int l,int r,int jd=0) {int mid=l+r>>1,i;judge=jd;jd=jd+1>=m?jd-m+1:jd+1;nth_element (P+l,P+mid,P+r +1) int t=0;if (L&LT;MID) t=build (L,MID-1,JD); int X=newnode (P[mid]); Son[x][0]=t;if (Mid<r) son[x][1]=build (mid+1, R,JD); update (x); return x;} void query (int f,point &p,int rank) {int dis[3],i;dis[2]=p[f].d (p); if (ans[rank-1].x>dis[2]) ans[rank-1].x=dis[ 2],ans[rank-1].p=p[f],sort (Ans,ans+rank);D is[0]=dis[1]=0;if (son[f][0]) Dis[0]=mindis (son[f][0],p); if (son[f][1]) Dis[1]=mindis (son[f][1],p); int t=dis[0]>dis[1];if (son[f][t]&&dis[t]<ans[rank-1].x) query (Son[f][t] , P,rank); t^=1;if (son[f][t]&&dis[t]<ans[rank-1].x) query (Son[f][t],p,rank);}} Kdt;int Main () {//freopen ("test.in", "R", stdin); int i,j,k,root; Point X;while (~scanf ("%d%d", &n,&m)) {root=0;kdt.init (); for (i=1;i<=n;i++) for (j=0;j<m;j++) sCANF ("%d", &p[i].c[j]), if (n) root=kdt.build (1,n), for (scanf ("%d", &q), q--;) {for (i=0;i<m;i++) scanf ("%d", &x.c[i]); scanf ("%d", &k), for (i=0;i<15;i++) ans[i].x=inf;kdt.query (root,x,k);p rintf ("the closest%d Points are:\n ", k); for (i=0;i<k;i++) {for (j=0;j<m-1;j++) printf ("%d ", Ans[i].p.c[j]);p rintf ("%d\n ", ans[i].p.c[ M-1]);}} Puts ("");} return 0;}


Copy to Google TranslateTranslation Results

"BZOJ3053" the Closest M Points kdtree good template One "Array Edition!!! 】

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.