题目大意:

平面上n个点,每次给出一个点,求这个点的k远点

题解:

什么叫做k远点呢。。。

1 2 3 4 5中5是第一远,4是第二远...

看来我语文学的不好

那么我们直接上k-D Tree求k邻近的方式求k远离即可

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
const ll maxn = 100010;
const ll dem = 2;
struct Node{
Node *ch[2];
ll pos[2];ll id;
ll minn[2],maxx[2];
void update(){
for(ll d=0;d<dem;++d){
minn[d] = min(pos[d],ch[0]->minn[d],ch[1]->minn[d]);
maxx[d] = max(pos[d],ch[0]->maxx[d],ch[1]->maxx[d]);
}
}
}*null;
Node akdjalskjalksdlkasdlkjlkjflsgdjasdf;
Node T[maxn];
inline void init(){
null = &akdjalskjalksdlkasdlkjlkjflsgdjasdf;
null->ch[0] = null->ch[1] = null;
for(ll d=0;d<dem;++d){
null->pos[d] = 0;
null->minn[d] = 0x3f3f3f3f;
null->maxx[d] = -0x3f3f3f3f;
}
}
ll now,split[maxn];
inline bool cmp(const Node &a,const Node &b){
return a.pos[split[now]] < b.pos[split[now]];
}
Node* build(ll l,ll r,ll s){
if(l > r) return null;
ll mid = (l+r) >> 1;
split[now = mid] = s % dem;
nth_element(T+l,T+mid,T+r+1,cmp);
Node *p = &T[mid];
p->ch[0] = build(l,mid-1,s+1);
p->ch[1] = build(mid+1,r,s+1);
p->update();return p;
}
struct Data{
ll dis;ll id;
bool operator < (const Data &a)const{
if(dis != a.dis) return dis > a.dis;
return id < a.id;
}
Data(){}
Data(ll a,ll b){dis=a;id=b;}
};
priority_queue<Data>q;
inline ll sqr(ll x){return x*x;}
Node op;ll k;
inline ll md(Node *p){
ll ret = .0;
ret = max(ret,sqr(p->minn[0] - op.pos[0]) + sqr(p->minn[1] - op.pos[1]));
ret = max(ret,sqr(p->minn[0] - op.pos[0]) + sqr(p->maxx[1] - op.pos[1]));
ret = max(ret,sqr(p->maxx[0] - op.pos[0]) + sqr(p->minn[1] - op.pos[1]));
ret = max(ret,sqr(p->maxx[0] - op.pos[0]) + sqr(p->maxx[1] - op.pos[1]));
return ret;
}
void query(Node *p){
if(p == null) return;
ll dis = 0;
for(ll d=0;d<dem;++d) dis += sqr(op.pos[d] - p->pos[d]);
if(q.size() < k){
q.push(Data(dis,p->id));
}else if(Data(dis,p->id) < q.top()){
q.pop();q.push(Data(dis,p->id));
}
if(md(p->ch[0]) < md(p->ch[1])) swap(p->ch[0],p->ch[1]); if(p->ch[0] != null && ((q.size() < k) || (md(p->ch[0]) >= q.top().dis))) query(p->ch[0]);
if(p->ch[1] != null && ((q.size() < k) || (md(p->ch[1]) >= q.top().dis))) query(p->ch[1]);
}
int main(){
init();
ll n;read(n);
for(ll i=1;i<=n;++i){
for(ll d=0;d<dem;++d){
read(T[i].pos[d]);
}
T[i].ch[0] = T[i].ch[1] = null;
T[i].id = i;T[i].update();
}
Node *root = build(1,n,1);
ll m;read(m);
while(m--){
for(ll d=0;d<dem;++d) read(op.pos[d]);
read(k);
while(!q.empty()) q.pop();
query(root);
printf("%lld\n",q.top().id);
}
getchar();getchar();
return 0;
}

最新文章

  1. ubuntu16.04装MatConvNet
  2. 19、lambda表达式树
  3. Light OJ 1032
  4. UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
  5. awk的使用备忘
  6. 【GPS】 数据围栏
  7. Ubuntu安装sar出错Please check if data collecting is enabled in /etc/default/sysstat
  8. sql语句select group by order by where一般先后顺序 转载
  9. TCP\UDP链接的异同
  10. 洛谷-三连击(升级版)-BOSS战-入门综合练习1
  11. 用OpenSSL生成自签名证书在IIS上搭建Https站点(用于iOS的https访问)
  12. hibernate从数据库中自动生成
  13. python利用socketserver实现并发套接字功能
  14. 用foobar进行码率转换 适用与sacd-r转成低码率
  15. python 解除装饰器,调用原本函数。
  16. 使用虚拟环境virtualenv/Virtualenvwrapper隔离多个python
  17. (5)MySQL的查询:模糊查询(通配符查询like)、限制符查询(limit)、排序查询(order by)、分组查询(group by)、(子查询)
  18. Python 多继承与MRO-C3算法
  19. DDL和DML的区别
  20. 团队作业7——第二次项目冲刺(Beta版本)day2

热门文章

  1. QTreeWidget 的用法
  2. Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)
  3. android开发系列之ContentObserver
  4. 迁移,移动.vagrant.d目录
  5. Oracle JDBC 连接卡死后 Connection Reset解决过程
  6. access变转换为mysql表工具
  7. pjax简单实例
  8. Android API Guides---Storage Access Framework
  9. python 基础 7.0 import 导入
  10. Vue中表单校验