【牛客网】Finding Hotel
【牛客网】Finding Hotel
忘记K远点对的剪枝的我有点自闭
事实上我们只要先建一棵KD树出来,维护一下所在的矩形,和子树里的最小值
每次查询的时候如果最小值比查询的值要大的话就退出
当前的答案构成了一个圆,若圆和矩形没有交就退出(不一定很严格,可以认为是以圆心为中心向上下左右延伸半径长度的一个正方形和矩形有交)
然后看当前点在哪个子树的矩形里,先搜那个子树,如果回来后在另一个子树里可能达到的最小值都没有答案大就不搜索另一棵子树
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,dimension,M,rt,Ncnt;
struct node {
int d[2],c,id;
}p[MAXN];
bool cmp(node a,node b) {
return a.d[dimension] < b.d[dimension];
}
struct KD {
node p;
int r[4],lc,rc,mc;
}tr[MAXN];
#define lc(u) tr[u].lc
#define rc(u) tr[u].rc
void build(int &u,int l,int r,int d) {
u = 0;
if(l > r) return;
u = ++Ncnt;
dimension = d;
int mid = (l + r) >> 1;
nth_element(p + l,p + mid,p + r + 1,cmp);
tr[u].p = p[mid];
for(int i = 0 ; i < 4 ; ++i) tr[u].r[i] = tr[u].p.d[i & 1];
build(lc(u),l,mid - 1,d ^ 1);build(rc(u),mid + 1,r,d ^ 1);
tr[u].mc = min(p[mid].c,min(tr[lc(u)].mc,tr[rc(u)].mc));
for(int i = 0 ; i < 2 ; ++i) tr[u].r[i] = min(tr[u].r[i],min(tr[lc(u)].r[i],tr[rc(u)].r[i]));
for(int i = 2 ; i < 4 ; ++i) tr[u].r[i] = max(tr[u].r[i],max(tr[lc(u)].r[i],tr[rc(u)].r[i]));
}
node ans;
int64 o(int64 x) {return x * x;}
int64 dis(node a,node b) {
return o(a.d[0] - b.d[0]) + o(a.d[1] - b.d[1]);
}
bool checkin(node x,int u) {
for(int i = 0 ; i <= 1 ; ++i) {
if(x.d[i] < tr[u].r[i] || x.d[i] > tr[u].r[i | 2]) return false;
}
return true;
}
int64 min_possible(node x,int u) {
for(int i = 0 ; i <= 1 ; ++i) {
if(x.d[i] < tr[u].r[i] || x.d[i] > tr[u].r[i | 2]) {
return min(o(tr[u].r[i] - x.d[i]),o(tr[u].r[i | 2] - x.d[i]));
}
}
return 1e18;
}
void Query(int u,node pos) {
if(!u) return;
if(tr[u].mc > pos.c) return;
if(tr[u].p.c <= pos.c) {
if(dis(tr[u].p,pos) < dis(ans,pos) || (dis(tr[u].p,pos) == dis(ans,pos) && tr[u].p.id < ans.id)) ans = tr[u].p;
}
int64 d = dis(ans,pos);
for(int i = 0 ; i <= 1 ; ++i) {
if(pos.d[i] < tr[u].r[i] && 1LL * (tr[u].r[i] - pos.d[i]) * (tr[u].r[i] - pos.d[i]) > d) return;
if(pos.d[i] > tr[u].r[i | 2] && 1LL * (tr[u].r[i | 2] - pos.d[i]) * (tr[u].r[i | 2] - pos.d[i]) > d) return;
}
int s = checkin(pos,lc(u)) ? lc(u) : rc(u);
int t = s == lc(u) ? rc(u) : lc(u);
Query(s,pos);
if(min_possible(pos,t) <= dis(pos,ans)) Query(t,pos);
}
void Solve() {
rt = 0;Ncnt = 0;
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) {
read(p[i].d[0]);read(p[i].d[1]);read(p[i].c);
p[i].id = i;
}
tr[0].mc = 1e9;
tr[0].r[0] = tr[0].r[1] = 1e9;
tr[0].r[2] = tr[0].r[3] = -1;
build(rt,1,N,0);
node pos;
for(int i = 1 ; i <= M ; ++i) {
read(pos.d[0]);read(pos.d[1]);read(pos.c);
ans.d[0] = 1e9;ans.d[1] = 1e9;ans.c = 0;ans.id = 1e9;
Query(1,pos);
out(ans.d[0]);space;out(ans.d[1]);space;out(ans.c);enter;
}
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
int T;
read(T);
while(T--) Solve();
return 0;
}
【牛客网】Finding Hotel的更多相关文章
- 牛客网暑期ACM多校训练营(第三场) J Distance to Work 计算几何求圆与多边形相交面积模板
链接:https://www.nowcoder.com/acm/contest/141/J来源:牛客网 Eddy has graduated from college. Currently, he i ...
- 牛客网 --java问答题
http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...
- 牛客网《BAT面试算法精品课》学习笔记
目录 牛客网<BAT面试算法精品课>学习笔记 牛客网<BAT面试算法精品课>笔记一:排序 牛客网<BAT面试算法精品课>笔记二:字符串 牛客网<BAT面试算法 ...
- C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...
- 牛客网第9场多校E(思维求期望)
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- Beautiful Numbers(牛客网)
链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...
- 牛客网华为机试题之Python解法
牛客网华为机试题之Python解法 第1题 字符串最后一个单词的长度 a = input().split(" ") print(len(a[-1])) 第2题 计算字符个数 a = ...
- 牛客网Wannafly挑战赛25A 因子(数论 素因子分解)
链接:https://www.nowcoder.com/acm/contest/197/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
随机推荐
- P1966 火柴排队——逆序对(归并,树状数组)
P1966 火柴排队 很好的逆序对板子题: 求的是(x1-x2)*(x1-x2)的最小值: x1*x1+x2*x2-2*x1*x2 让x1*x2最大即可: 可以证明将b,c数组排序后,一一对应的状态是 ...
- C++标准库分析总结(七)——<Hashtable、Hash_set、Hash_multiset、unordered容器设计原则>
编译器对关联容器的实现有两个版本,上一节总结了以红黑树做为基础的实现版本,这一节总结以哈希表(hash table,散列表)为底部结构的实现版本. 一.Hashtable简单介绍 Hashtable相 ...
- OpenJudge 1.5.36:计算多项式的值
描述 假定多项式的形式为xn+xn-1+…+x2+x+1,请计算给定单精度浮点数x和正整数n值的情况下这个多项式的值. 输入输入仅一行,包括x和n,用单个空格隔开.x在float范围内,n <= ...
- 配置了ssh免密登录还是提示权限不足怎么解决
通过 管理终端 进入系统.通过 cat 等指令查看 /etc/ssh/sshd_config 中是否包含类似如下配置: AllowUsers root test DenyUsers test Deny ...
- 简单理解Spring之IOC和AOP及代码示例
Spring是一个开源框架,主要实现两件事,IOC(控制反转)和AOP(面向切面编程). IOC 控制反转,也可以称为依赖倒置. 所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B, ...
- [转]Python3之max key参数学习记录
Python3之max key参数学习记录 转自https://www.cnblogs.com/zhangwei22/p/9892422.html 今天用Python写脚本,想要实现这样的功能:对于给 ...
- Publish site through visual studio
https://www.c-sharpcorner.com/UploadFile/4b0136/getting-started-with-iis-host-and-publish-in-mvc-5/ ...
- Python 自学笔记(八)
import math def A(a,b): print("第一个参数的值为"+str(a)) print("第一个参数的值为"+str(b)) a = 1 ...
- Python 自学笔记(七)
1.定义函数和调用函数 1-1.定义函数 定义函数的语法书写:def 函数名(参数名)(注:括号内可以为空,也可以为多个参数,多个参数间用逗号隔开即可) 由上可以看出,函数默认返回None 2.函数的 ...
- idea备忘
1.idea 最近打开的文件个数 File->Settings->Editor->General->Editor Tabs->Tab Closing Policy-> ...