题目链接

初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子。 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少。

写了指针版本的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树的更多相关文章

  1. bzoj 2648 SJY摆棋子 —— K-D树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...

  2. BZOJ 2648: SJY摆棋子(K-D Tree)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 6051  Solved: 2113[Submit][Status][Discuss] Descript ...

  3. BZOJ 2648: SJY摆棋子

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2968  Solved: 1011[Submit][Status][Disc ...

  4. BZOJ 2648: SJY摆棋子 kdtree

    2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...

  5. bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree

    2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec  Memory Limit: 128 MB Description 这天,S ...

  6. BZOJ 2648 SJY摆棋子(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...

  7. BZOJ 2648 SJY摆棋子(KD Tree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...

  8. bzoj 2648 SJY摆棋子——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...

  9. bzoj 2648: SJY摆棋子【KD-tree】

    其实理论上cdq更优 核心是依次取x值.y值的mid作为当前节点,向两边递归建立二叉树,树上维护size:子树大小:mx[0/1]:子树内最大x/y:mn[0/1]:子树内最小x/y:d[0/1]:这 ...

随机推荐

  1. JQuery中的省市联动

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. shell练习--批量创建账号

    #!/bin/bash #By spinestars #-- #cksum5位数获取方法,可能有重复 #pd="user`head -200 /dev/urandom | cksum | h ...

  3. dojo Tree 添加、删除节点

    var tree=this.tree; var store=tree.model.store; if(this.node){ console.log(this.node) var children=t ...

  4. Ancient Printer(tire树)

    Ancient Printer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  5. Bootstrap 模态对话框只加载一次 remote 数据的解决办法

    原文: https://my.oschina.net/qczhang/blog/190215?p=1

  6. C指针数组

    #include<stdio.h> #include<stdlib.h> int main(void) { char const *str[] = { "this i ...

  7. USB的VID和PID,以及分类(Class,SubClass,Protocol)

    USB(Universal Serial BUS,通用串行总线)协议规定,所有的USB设备都有VID(Vendor ID,供应商识别码)和PID(Product ID,产品识别码).VID由供应商向U ...

  8. QWidget可以设置QStyle,它可以绘制很多东西(具体内容没研究,待续)

    QStyle * QWidget::style() const See also QWidget::setStyle(), QApplication::setStyle(), and QApplica ...

  9. 三星S5360(GALAXY Y)首次刷机尝试~

    刷机包下载: http://www.romjd.com/Device/samsung-s5360 http://www.shuaji.com/rom/2033.htm#down http://www. ...

  10. javascript之String

    一.构造函数 new String(value) //构造函数 function String(value) //转换函数 二.属性 length 该字符串中的字符数 var str = new St ...