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]:这 ...
随机推荐
- js经典代码技巧学习之一:使用三元运算符处理javascript兼容
window.Event = { add: function() { //使用条件表达式检测标准方法是否存在 return document.addEventListener ? function(a ...
- java.lang.ClassNotFoundException: org.apache.struts.action.ActionServlet
- windows下cmd导入与导出mysql 数据库
一.导出数据库 假设导到C:\ordersys.sql 1.CMD下:输入mysqldump -u 用户名 -p 导出的数据库名>c:\ordersys.sql 2.CMD会提示输入密码:输入密 ...
- Roads in the North(POJ 2631 DFS)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- How Many Tables(POJ 1213 求连通分量)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 多线程实际运用<第七篇>
1.单线程采集100个页面 class Program { static int i = 6991275; static void Main(string[] args) { Stopwatch sw ...
- 【转】android是32-bit系统还是64-bit系统
原文网址:http://www.cnblogs.com/pengwang/archive/2013/03/11/2954496.html 电脑CPU分32位和64位,这个我们都知道.用了这么长时间的a ...
- UESTC_Sea Base Exploration CDOJ 409
When the scientists explore the sea base, they use a kind of auto mobile robot, which has the missio ...
- canvas.js | CLiPS
canvas.js | CLiPS canvas.js The canvas.js module is a simple and robust JavaScript API for the HTML5 ...
- Unity 使用实体类
故事的由来: 正在开发打飞机的游戏,遇到这样的数据结构,游戏有很多关卡-> 每个关卡有几波怪物->每一波里面有怪物和数量 [] 关卡 { []波{ {怪物,数量},{怪物,数量},{怪物, ...