【BZOJ】【3053】The Closest M Points

KD-Tree



  题目大意:K维空间内,与给定点欧几里得距离最近的 m 个点。

  KD树啊……还能怎样啊……然而扩展到k维其实并没多么复杂?除了我已经脑补不出建树过程……不过代码好像变化不大>_>

  然而我WA了。。。为什么呢。。。我也不知道……

  一开始我的Push_up是这么写的:

inline void Push_up(int o){
    rep(i,k){
        if (L) t[o].mn[i]=min(t[o].mn[i],t[L].mn[i]),t[o].mx[i]=max(t[o].mx[i],t[L].mx[i]);
        if (R) t[o].mx[i]=min(t[o].mn[i],t[R].mn[i]),t[o].mx[i]=max(t[o].mx[i],t[R].mx[i]);
    }
}

  就是如果没有右儿子,就不用它更新了……

  然而我改成zyf的这样:(t[0]初始化一下,mn都置为INF,mx都置为-INF)

inline void Push_up(int o){
    rep(i,k){
        t[o].mn[i]=min(t[o].mn[i],min(t[L].mn[i],t[R].mn[i]));
        t[o].mx[i]=max(t[o].mx[i],max(t[L].mx[i],t[R].mx[i]));
    }
}

  就过了……………… 

P.S.多组数据的题目,KD-Tree一定要记得清空 l 和 r ……要不然叶子节点会有一些奇怪的问题……

获得称号:

  1 /**************************************************************
  2     Problem: 3053
  3     User: Tunix
  4     Language: C++
  5     Result: Accepted
  6     Time:1364 ms
  7     Memory:9088 kb
  8 ****************************************************************/
  9
 10 //BZOJ 3053
 11 #include<queue>
 12 #include<cmath>
 13 #include<cstdio>
 14 #include<cstring>
 15 #include<cstdlib>
 16 #include<iostream>
 17 #include<algorithm>
 18 #define rep(i,n) for(int i=0;i<n;++i)
 19 #define F(i,j,n) for(int i=j;i<=n;++i)
 20 #define D(i,j,n) for(int i=j;i>=n;--i)
 21 #define pb push_back
 22 #define sqr(x) ((x)*(x))
 23 using namespace std;
 24 typedef long long LL;
 25 inline int getint(){
 26     int r=1,v=0; char ch=getchar();
 27     for(;!isdigit(ch);ch=getchar()) if (ch==‘-‘) r=-1;
 28     for(; isdigit(ch);ch=getchar()) v=v*10-‘0‘+ch;
 29     return r*v;
 30 }
 31 const int N=100010,INF=1e9;
 32 /*******************template********************/
 33
 34 int n,k,D,root,ans[15];
 35 struct node{
 36     int d[6],mn[6],mx[6],l,r;
 37     int& operator [] (int x){return d[x];}
 38     void read(){rep(i,k) d[i]=getint();}
 39 }t[N],tmp;
 40 bool operator < (node a,node b){return a[D]<b[D];}
 41 #define L t[o].l
 42 #define R t[o].r
 43 #define mid (l+r>>1)
 44 inline void Push_up(int o){
 45     rep(i,k){
 46         t[o].mn[i]=min(t[o].mn[i],min(t[L].mn[i],t[R].mn[i]));
 47         t[o].mx[i]=max(t[o].mx[i],max(t[L].mx[i],t[R].mx[i]));
 48     }
 49 }
 50 int build(int l,int r,int dir){
 51     D=dir;
 52     nth_element(t+l,t+mid,t+r+1);
 53     rep(i,k) t[mid].mn[i]=t[mid].mx[i]=t[mid][i];
 54     t[mid].l=l<mid ? build(l,mid-1,(dir+1)%k) : 0;
 55     t[mid].r=mid<r ? build(mid+1,r,(dir+1)%k) : 0;
 56     Push_up(mid);
 57     return mid;
 58 }
 59 inline int getdis(int o){
 60     if (!o) return INF;
 61     int ans=0;
 62     rep(i,k) if (tmp[i]<t[o].mn[i]) ans+=sqr(t[o].mn[i]-tmp[i]);
 63     rep(i,k) if (tmp[i]>t[o].mx[i]) ans+=sqr(tmp[i]-t[o].mx[i]);
 64     return ans;
 65 }
 66 inline int dis(node a,node b){
 67     int ans=0;
 68     rep(i,k) ans+=sqr(a[i]-b[i]);
 69     return ans;
 70 }
 71 typedef pair<int,int> pii;
 72 priority_queue<pii>Q;
 73 #define mp make_pair
 74 #define X first
 75 #define Y second
 76 void query(int o){
 77     int dl=getdis(L),dr=getdis(R),d0=dis(t[o],tmp);
 78     if (d0<Q.top().X){Q.pop(); Q.push(mp(d0,o));}
 79     if (dl<dr){
 80         if (dl<Q.top().X) query(L);
 81         if (dr<Q.top().X) query(R);
 82     }else{
 83         if (dr<Q.top().X) query(R);
 84         if (dl<Q.top().X) query(L);
 85     }
 86 }
 87 int main(){
 88 #ifndef ONLINE_JUDGE
 89     freopen("3053.in","r",stdin);
 90     freopen("3053.out","w",stdout);
 91 #endif
 92     rep(i,5) t[0].mn[i]=INF,t[0].mx[i]=-INF;
 93     while(scanf("%d%d",&n,&k)!=EOF){
 94         F(i,1,n) rep(j,k) t[i][j]=getint();
 95         root=build(1,n,0);
 96         int T=getint();
 97         while(T--){
 98             rep(j,k) tmp[j]=getint();
 99             n=getint();
100             printf("the closest %d points are:\n",n);
101             F(i,1,n) Q.push(mp(INF,0));
102             query(root);
103             D(i,n,1) ans[i]=Q.top().Y,Q.pop();
104             F(i,1,n) rep(j,k)
105                 printf("%d%c",t[ans[i]][j],j!=k-1?‘ ‘:‘\n‘);
106         }
107     }
108     return 0;
109 }

3053: The Closest M Points

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 383  Solved: 147
[Submit][Status][Discuss]

Description

The
course of Software Design and Development Practice is objectionable.
ZLC is facing a serious problem .There are many points in K-dimensional
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 by:
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?

软工学院的课程很讨厌!ZLC同志遇到了一个头疼的问题:在K维空间里面有许多的点,对于某些给定的点,ZLC需要找到和它最近的m个点。

(这里的距离指的是欧几里得距离:D(p, q) = D(q, p) =  sqrt((q1 - p1) ^ 2 + (q2 - p2) ^ 2 + (q3 - p3) ^ 2 + ... + (qn - pn) ^ 2)

ZLC要去打Dota,所以就麻烦你帮忙解决一下了……

【Input】

第一行,两个非负整数:点数n(1 <= n <= 50000),和维度数k(1 <= k <= 5)。
接下来的n行,每行k个整数,代表一个点的坐标。
接下来一个正整数:给定的询问数量t(1 <= t <= 10000)
下面2*t行:
  第一行,k个整数:给定点的坐标
  第二行:查询最近的m个点(1 <= m <= 10)

所有坐标的绝对值不超过10000。
有多组数据!

【Output】

对于每个询问,输出m+1行:
第一行:"the closest m points are:" m为查询中的m
接下来m行每行代表一个点,按照从近到远排序。

保证方案唯一,下面这种情况不会出现:
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 should 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 Input

3 2
1 1
1 3
3 4
2
2 3
2
2 3
1

Sample Output

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

HINT

Source

k-d tree

[Submit][Status][Discuss]

时间: 2024-08-05 02:24:34

【BZOJ】【3053】The Closest M Points的相关文章

P2433 - 【BZOJ 3262三维偏序】陌上花开------三维偏序

P2433 - [BZOJ 3262三维偏序]陌上花开 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美丽,当且仅当Sa>=Sb,Ca>=Cb,Ma>=Mb.显然,两朵花可能有同样的属性.需要统计出评出每个等级的花的数量. Input 第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,

【BZOJ做题记录】07.07~?

在NOI一周前重开一个坑 最后更新时间:7.07 11:26 7.06 下午做的几道CQOI题: BZOJ1257: [CQOI2007]余数之和sum:把k mod i写成k-k/i*i然后分段求后面的部分就好了 BZOJ1258: [CQOI2007]三角形tri:在草稿纸上按照位置和边找一下规律就好了 BZOJ1260: [CQOI2007]涂色paint:简单的区间DP BZOJ1303: [CQOI2009]中位数图:小于中位数的改为-1大于的改为1,算一算前缀和然后哈希一下乘一乘就好

【BZOJ 3053】 The Closest M Points

3053: The Closest M Points Time Limit: 10 Sec Memory Limit: 128 MB Submit: 376 Solved: 146 [Submit][Status][Discuss] Description The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem .There are many

【BZOJ】3053: The Closest M Points(kdtree)

http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!!! 本题裸的kdtree,比上一题还要简单...................................... 对于当前点,判断进入左或右子树,然后看答案是否能过分割线..如果能,进入右或左子树.........并且如果答案个数小于k,也要进入.. 然后就浪吧........... #inc

【BZOJ 3053】The Closest M Points

KDTree模板,在m维空间中找最近的k个点,用的是欧几里德距离. 理解了好久,昨晚始终不明白那些“估价函数”,后来才知道分情况讨论,≤k还是=k,在当前这一维度距离过线还是不过线,过线则要继续搜索另一个子树.还有别忘了当前这个节点! #include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<algorithm> #define read(x) x=ge

【BZOJ 2820】 YY的GCD

2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 807  Solved: 404 [Submit][Status] Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 kAc这种傻×必然不会了,于是向你来请教-- 多组输入 Input 第一行一个整数T 表述数据组数 接下来T行,每行两个正整数,表示

【BZOJ】3319: 黑白树

http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种:1.查询u到根的第一条黑边的编号.2.将u到v的路径全部染成黑色 #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream>

【bzoj】4538: [Hnoi2016]网络

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4538 维护一个数据结构支持对于一颗树的操作,需要支持: 1.对于树上的一条路径上的每个点上放一个值. 2.撤销某次操作的路劲放. 3.查询除了经过这个点的路径的最大值. 往一个路径上丢值相当于往不经过条路径的所有点上丢值. 用一个树链剖分即可维护,对于操作区间取反. 直接查询单点最大值即可. 为了维护单点最大值,线段树中的每一个点对应两个堆,用于维护插入誉删除. 防止爆空间,所以标记永久

【BZOJ】【1089】【SCOI2003】严格n元树

高精度/递推 Orz Hzwer…… 然而我想多了…… 理解以后感觉黄学长的递推好精妙啊 顺便学到了一份高精度的板子= =233 引用下题解: f[i]=f[i-1]^n+1 ans=f[d]-f[d-1] 然后加个高精度... 话说这个数据范围是虚的吧... 极限数据根本不会做.. 1 /************************************************************** 2 Problem: 1089 3 User: Tunix 4 Language: