【BZOJ2648】【kd_tree】SJY摆棋子
/*
唐代李白
《江夏别宋之悌》
楚水清若空,遥将碧海通。人分千里外,兴在一杯中。
谷鸟吟晴日,江猿啸晚风。平生不下泪,于此泣无穷.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <set>
#define LOCAL
const int INF = 0x7fffffff;
const int MAXN = + ;
const int maxnode = * + * ;
const int MAXM = + ;
const int MAX = ;
using namespace std;
struct Node{//kd_tree
int d[], l, r;
int Max[], Min[];
}t[ + ],tmp; int n,m,root,cmp_d;
int k1, k2, k3, Ans; bool cmp(Node a, Node b){return (a.d[cmp_d]<b.d[cmp_d]) || ((a.d[cmp_d] == b.d[cmp_d]) && (a.d[!cmp_d] < b.d[!cmp_d]));}
void update(int p){
if (t[p].l){
//左边最大的横坐标值
t[p].Max[] = max(t[p].Max[], t[t[p].l].Max[]);
t[p].Min[] = min(t[p].Min[], t[t[p].l].Min[]);
t[p].Max[] = max(t[p].Max[], t[t[p].l].Max[]);
t[p].Min[] = min(t[p].Min[], t[t[p].l].Min[]);
}
if (t[p].r){
t[p].Max[] = max(t[p].Max[], t[t[p].r].Max[]);
t[p].Min[] = min(t[p].Min[], t[t[p].r].Min[]);
t[p].Max[] = max(t[p].Max[], t[t[p].r].Max[]);
t[p].Min[] = min(t[p].Min[], t[t[p].r].Min[]);
}
return;
}
//d是横竖切..
int build(int l, int r, int D){
int mid = (l + r) / ;
cmp_d = D;
//按照cmp的比较顺序在l到r中找到第mid大的元素
nth_element(t + l + , t + mid + , t + r + , cmp);
t[mid].Max[] = t[mid].Min[] = t[mid].d[];
t[mid].Max[] = t[mid].Min[] = t[mid].d[];
//递归建树
if (l != mid) t[mid].l = build(l, mid - , D ^ );
if (r != mid) t[mid].r = build(mid + , r, D ^ );
update(mid);
return mid;
}
void insert(int now){
int D = , p = root;//D还是表示方向
while (){
//边下传边更新
t[p].Max[] = max(t[p].Max[], t[now].Max[]);
t[p].Min[] = min(t[p].Min[], t[now].Min[]);
t[p].Max[] = max(t[p].Max[], t[now].Max[]);
t[p].Min[] = min(t[p].Min[], t[now].Min[]);
//有没有点线段树的感觉..
if (t[now].d[D] >= t[p].d[D]){
if (t[p].r == ){
t[p].r = now;
return;
}else p = t[p].r;
}else{
if (t[p].l == ){
t[p].l = now;
return;
}else p = t[p].l;
}
D = D ^ ;
}
return;
}
int ABS(int x) {return x < ? -x : x;}
//dist越小代表越趋近?
int dist(int p1, int px, int py){
int dist = ;
if (px < t[p1].Min[]) dist += t[p1].Min[] - px;
if (px > t[p1].Max[]) dist += px - t[p1].Max[];
if (py < t[p1].Min[]) dist += t[p1].Min[] - py;
if (py > t[p1].Max[]) dist += py - t[p1].Max[];
return dist;
}
void ask(int p){
int dl, dr, d0;
//哈密顿距离
d0=ABS(t[p].d[] - k2) + ABS(t[p].d[] - k3);
if(d0 < Ans) Ans = d0;
if(t[p].l) dl = dist(t[p].l, k2, k3); else dl = 0x7f7f7f7f;
if(t[p].r) dr = dist(t[p].r, k2, k3); else dr = 0x7f7f7f7f;
//应该是一个启发式的过程。
if(dl < dr){
if(dl < Ans) ask(t[p].l);
if(dr < Ans) ask(t[p].r);
}else{
if(dr < Ans) ask(t[p].r);
if(dl < Ans) ask(t[p].l);
}
} void init(){
//假设0为横坐标
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
scanf("%d%d", &t[i].d[], &t[i].d[]);
root = build(, n, );
}
void work(){
for (int i = ; i <= m ;i++){
scanf("%d%d%d", &k1, &k2, &k3);
if (k1 == ){//黑棋
++n;
t[n].Max[] = t[n].Min[] = t[n].d[] = k2;
t[n].Max[] = t[n].Min[] = t[n].d[] = k3;
insert(n);
}else{
Ans = 0x7f7f7f7f;
ask(root);
printf("%d\n", Ans);
}
}
} int main(){ init();
work();
return ;
}
【BZOJ2648】【kd_tree】SJY摆棋子的更多相关文章
- 【BZOJ2648】SJY摆棋子(KD-Tree)
[BZOJ2648]SJY摆棋子(KD-Tree) 题面 BZOJ Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
- 【BZOJ2648】SJY摆棋子 KDtree
[BZOJ2648]SJY摆棋子 Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找 ...
- 【BZOJ2648】SJY摆棋子 [KD-tree]
SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 这天,SJY显得无聊.在家自己 ...
- [bzoj2648/2716]SJY摆棋子_KD-Tree
SJY摆旗子 bzoj-2648 题目大意:平面上有n个黑子.有m个操作,可以下一颗白子,查询与曼哈顿距离下最近黑子之间的曼哈顿距离,或者下一颗黑子. 注释:$1\le n,m\le 5\cdot 1 ...
- BZOJ2648/2716:SJY摆棋子/[Violet]天使玩偶(K-D Tree)
Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子. ...
- 【bzoj2648】 SJY摆棋子
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 (题目链接) 题意 动态维护二维平面上的点的插入以及最邻近域搜索. Solution KDtr ...
- [bzoj2648/2716]SJY摆棋子
平面上有n个点,要求支持插入一个点和查询一个点的最近点距离 n,m<=500000 用kdtree实现,但是复杂度貌似没法保证.....(莫名加了替罪羊重建更慢了...) #include< ...
- BZOJ2648:SJY摆棋子
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- 【BZOJ2648】SJY摆棋子
题目大意:维护一个二维平面,平面上初始有 N 个点,支持两种操作:平面加点.查询距离某个指定点的最小哈密顿距离. 题解:学习到了 kd-tree 数据结构. kd-tree 类似于平衡树,即:每个节点 ...
- 【bzoj2648】SJY摆棋子(kdtree)
传送门 题意: 二维平面上有若干个点. 现在要维护一种数据结构,支持插入一个点以及询问其余点到某个点的最小曼哈顿距离. 思路: 这是个\(kdtree\)模板题. \(kdtree\)是一种可以高效处 ...
随机推荐
- ☀【SeaJS】SeaJS Grunt构建
如何使用Grunt构建一个中型项目?https://github.com/twinstony/seajs-grunt-build spmjshttp://docs.spmjs.org/doc/inde ...
- DIP开放计算平台介绍
随着平台业务的发展,依赖于Portal(Web)构建的服务架构已逐渐不能满足现有的一些复杂需求(如:使用Hive SQL无法完成计算逻辑),而且对于一些具备编程能力的程序员或数据分析师而言,能够自主控 ...
- 获取css的属性值
# -*- coding:utf-8 -*- """ 在元素上执行双击操作 """ from selenium import webdriv ...
- 《JavaScript核心概念》基础部分重点摘录
注:<JavaScript核心概念>适合深入了解JavaScript,比我买的<JavaScript框架设计>语言和内容丰富多了(可能是我水平尚浅吧). 1. 作用域 var ...
- Unity之Avatar原理
今天花了一些时间理了理Unity的动画系统. 之前给不同模型配动画时没怎么在意,只知道用Avatar可以让一个模型使用另一个模型的动画.由于用的基本上都是人物模型,基本上没出现什么错误. 不过在用到异 ...
- 如何理解这段代码:void (*signal (int sinno,void(*func)(int)))(int)
void (*signal (int sinno,void(*func)(int)))(int) 先来看void(*func)(int) 这里的意思是声明一个函数指针func,它的参数类型为int ...
- KiCad中层定义
5.2.1. Paired layers The Adhesives layers (Copper and Component): These are used in the applicati ...
- C# WinForm 透明控件 PictureBox透明 分类: WinForm 2014-07-30 13:27 591人阅读 评论(0) 收藏
1.要实现C# WinForm中的控件与背景的透明,可以通过设置控件的BackColor属性为Transparent,同时设置其父控件.因为在C#中,控件的透明指对父窗体透明.如果不设置Parent属 ...
- SQL ID自增列从1开始重新排序 分类: SQL Server 2014-05-19 14:46 652人阅读 评论(0) 收藏
数据库中把ID自增长重置成1: 一般做法:(太麻烦) 复制表数据->删除原表.新建一张表->粘贴: 新方法: 数据库中:新建查询->复制.粘贴一下代码->修改表名,执行即可(先 ...
- JBoss 系列十一:JBoss Cluster Framework Demo 介绍
内容概要 JBoss Cluster Framework Demo包括JGruops.JBossCache.Infinispan,我们在随后的系列中会使用和运行这些示例来说明JGroups.JBoss ...