bzoj 2648 SJY摆棋子 kd树
初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子。 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少。
写了指针版本的kd树, 感觉这个版本很好理解。
#include <bits/stdc++.h>
using namespace std;
#define mk(x, y) make_pair(x, y)
#define mem(a) memset(a, 0, sizeof(a))
#define fi first
#define se second
typedef pair<int, int> pll;
const int inf = 2e9;
int cmpflag;
pll a[];
struct kdTree
{
pll point;
kdTree *l, *r;
int x[], y[];
kdTree(){};
kdTree(const pll& par): point(par)
{
x[] = x[] = par.fi;
y[] = y[] = par.se;
l = r = NULL;
}
int getMin(const pll& par)
{
int ret = ;
if(par.fi < x[])
ret += x[] - par.fi;
if(par.fi > x[])
ret += par.fi - x[];
if(par.se < y[])
ret += y[] - par.se;
if(par.se > y[])
ret += par.se - y[];
return ret;
}
void pushUp(const kdTree* par)
{
x[] = min(x[], par->x[]);
x[] = max(x[], par->x[]);
y[] = min(y[], par->y[]);
y[] = max(y[], par->y[]);
}
};
bool cmp(const pll& lhs, const pll& rhs)
{
if(cmpflag)
return lhs.se < rhs.se;
return lhs.fi < rhs.fi;
}
int getDistance(const pll& lhs, const pll& rhs)
{
return abs(lhs.fi-rhs.fi)+abs(lhs.se-rhs.se);
}
void build(kdTree*& p, int l, int r, int w)
{
if(l > r)
return ;
int mid = l + r >> ;
cmpflag = w;
nth_element(a+l, a+mid, a+r+, cmp);
p = new kdTree(a[mid]);
build(p->l, l, mid-, w^);
build(p->r, mid+, r, w^);
if(p->l)
p->pushUp(p->l);
if(p->r)
p->pushUp(p->r);
}
void add(kdTree*& p, const pll& q, int w)
{
if(!p) {
p = new kdTree(q);
return ;
}
cmpflag = w;
if(cmp(q, p->point)) {
add(p->l, q, w^);
p->pushUp(p->l);
} else {
add(p->r, q, w^);
p->pushUp(p->r);
}
}
void query(kdTree* p, const pll& q, int& ans)
{
ans = min(ans, getDistance(q, p->point));
int lDis = p->l?p->l->getMin(q):inf;
int rDis = p->r?p->r->getMin(q):inf;
if(lDis < rDis) {
if(lDis < ans)
query(p->l, q, ans);
if(ans > rDis)
query(p->r, q, ans);
} else {
if(rDis < ans) {
query(p->r, q, ans);
}
if(ans > lDis)
query(p->l, q, ans);
}
}
int main()
{
int n, m, x, y, sign;
cin>>n>>m;
for(int i = ; i <= n; i++) {
scanf("%d%d", &a[i].fi, &a[i].se);
}
kdTree *root = new kdTree();
build(root, , n, );
while(m--) {
scanf("%d%d%d", &sign, &x, &y);
if(sign == ) {
add(root, mk(x, y), );
} else {
int ans = inf;
query(root, mk(x, y), ans);
printf("%d\n", ans);
}
}
return ;
}
bzoj 2648 SJY摆棋子 kd树的更多相关文章
- bzoj 2648 SJY摆棋子 —— K-D树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- BZOJ 2648: SJY摆棋子
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2968 Solved: 1011[Submit][Status][Disc ...
- BZOJ 2648: SJY摆棋子 kdtree
2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...
- bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec Memory Limit: 128 MB Description 这天,S ...
- BZOJ 2648 SJY摆棋子(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...
- BZOJ 2648 SJY摆棋子(KD Tree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...
- bzoj 2648 SJY摆棋子——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...
- bzoj 2648: SJY摆棋子【KD-tree】
其实理论上cdq更优 核心是依次取x值.y值的mid作为当前节点,向两边递归建立二叉树,树上维护size:子树大小:mx[0/1]:子树内最大x/y:mn[0/1]:子树内最小x/y:d[0/1]:这 ...
随机推荐
- JavaScript this 局部变量全局变量 作用域 作用域链 闭包
从阮老师博客的一道测试题说起: 代码段一: var name = "The Window"; var object = { name : "My Object" ...
- Cleaning Shifts(POJ 2376 贪心)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15143 Accepted: 3875 ...
- HDU 1172 猜数字(DFS)
猜数字 Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- Typecho 代码阅读笔记(一) - 页面渲染及路由机制
转载请注明出处:http://blog.csdn.net/jh_zzz 从 index.php 开始看, /** 初始化组件 */ Typecho_Widget:: widget('Widget_In ...
- 有意思的GacUI
所有方法,无论是你写还是工具来codegen还是用宏,最终都指向把这些名字和对应的指针存在一个map里.C++是不提供这个功能的,我也没仔细研究过qt怎么做,不过我在我自己的gacui里面实现了类似的 ...
- Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008
Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...
- magento添加系统sections配置时应注意的事项
(1)只有在新增sections是需要增加对应的acl配置,这个配置可以放在config.xml中或者放在adminhtml.xml中 <adminhtml> <acl> &l ...
- Hibernate框架(一)——总体介绍
作为SSH三大框架之一的Hibernate,是用来把程序的Dao层和数据库打交道用的,它封装了JDBC的步骤,是我们对数据库的操作更加简单,更加快捷.利用Hibernate框架我们就可以不再编写重复的 ...
- paip.c++ qt 目录遍历以及文件操作
paip.c++ qt 目录遍历以及文件操作 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/a ...
- Java正則表達式
近期工作中常常要用到正則表達式,不得不花点时间对其进行一定的学习. JDK中提供了2个类来支持正則表達式,各自是java.util.regex.Pattern和java.util.regex.Ma ...