bzoj2648
http://www.lydsy.com/JudgeOnline/problem.php?id=2648
kdtree裸题。。。
抄板子一边抄对了。。。
挺好理解的,就是说我们先找出中间的元素,然后小的放左边,大的放右边,这样我们就确定了这个点,然后维度换一下,递归建树,很像splay,其实就是把平面每次一分为二,先横着再竖着。。。
然后那个get挺神奇的,大概就是估计一下现在这个点距离下一个区域的距离?其实就是优化枚举,减少了很多重复状态。。。
#include<bits/stdc++.h>
using namespace std;
const int N = , inf = << ;
struct data {
int l, r, d[], min[], max[];
} t[N];
int n, m, root, ans, d;
bool cp(data x, data y)
{
if(x.d[d] != y.d[d]) return x.d[d] < y.d[d];
return x.d[d ^ ] < y.d[d ^ ];
}
void update(int k)
{
for(int i = ; i < ; ++i)
{
if(t[k].l) t[k].min[i] = min(t[k].min[i], t[t[k].l].min[i]), t[k].max[i] = max(t[k].max[i], t[t[k].l].max[i]);
if(t[k].r) t[k].min[i] = min(t[k].min[i], t[t[k].r].min[i]), t[k].max[i] = max(t[k].max[i], t[t[k].r].max[i]);
}
}
int get(int k, data x)
{
int ret = ;
for(int i = ; i < ; ++i) ret += max(t[k].min[i] - x.d[i], ) + max(x.d[i] - t[k].max[i], );
return ret;
}
int build(int l, int r, int now)
{
d = now;
int mid = (l + r) >> ;
nth_element(t + l + , t + mid + , t + r + , cp);
for(int i = ; i < ; ++i) t[mid].min[i] = t[mid].max[i] = t[mid].d[i];
if(l < mid) t[mid].l = build(l, mid - , now ^ );
if(r > mid) t[mid].r = build(mid + , r, now ^ );
update(mid); return mid;
}
void insert(int k, int now, data x)
{
if(x.d[now] < t[k].d[now])
{
if(t[k].l) insert(t[k].l, now ^ , x);
else
{
t[k].l = ++n;
for(int i = ; i < ; ++i) t[n].max[i] = t[n].min[i] = t[n].d[i] = x.d[i];
}
}
else
{
if(t[k].r) insert(t[k].r, now ^ , x);
else
{
t[k].r = ++n;
for(int i = ; i < ; ++i) t[n].max[i] = t[n].min[i] = t[n].d[i] = x.d[i];
}
}
update(k);
}
void query(int k, int now, data x)
{
int d = abs(t[k].d[] - x.d[]) + abs(t[k].d[] - x.d[]), dl = inf, dr = inf;
ans = min(ans, d);
if(t[k].l) dl = get(t[k].l, x);
if(t[k].r) dr = get(t[k].r, x);
if(dl < dr)
{
if(dl < ans) query(t[k].l, now ^ , x);
if(dr < ans) query(t[k].r, now ^ , x);
}
else
{
if(dr < ans) query(t[k].r, now ^ , x);
if(dl < ans) query(t[k].l, now ^ , x);
}
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%d%d", &t[i].d[], &t[i].d[]);
root = build(, n, );
while(m--)
{
int opt; data x; scanf("%d%d%d", &opt, &x.d[], &x.d[]);
if(opt == ) insert(root, , x);
if(opt == )
{
ans = inf;
query(root, , x);
printf("%d\n", ans);
}
}
return ;
}
bzoj2648的更多相关文章
- 【bzoj2648】 SJY摆棋子
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 (题目链接) 题意 动态维护二维平面上的点的插入以及最邻近域搜索. Solution KDtr ...
- BZOJ2648 SJY摆棋子(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 【BZOJ2648】SJY摆棋子(KD-Tree)
[BZOJ2648]SJY摆棋子(KD-Tree) 题面 BZOJ Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
- BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶
BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...
- 【BZOJ2648】SJY摆棋子 KDtree
[BZOJ2648]SJY摆棋子 Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找 ...
- KD-Tree复习笔记(BZOJ1941 & BZOJ2648 & BZOJ4066)
快一年了都没碰到什么必须用KDT的题目导致模板完全忘光了,重新复习了一下. K_Dimention_Tree是一种用来处理二维以上问题的数据结构(OI中一般都是二维),本质是二维启发式估价函数实现剪枝 ...
- [bzoj2648/2716]SJY摆棋子_KD-Tree
SJY摆旗子 bzoj-2648 题目大意:平面上有n个黑子.有m个操作,可以下一颗白子,查询与曼哈顿距离下最近黑子之间的曼哈顿距离,或者下一颗黑子. 注释:$1\le n,m\le 5\cdot 1 ...
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
- Bzoj2648 SJY摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB Submit: 3128 Solved: 1067 Description 这天,SJY显得无聊.在家自己玩.在一个 ...
- 【kd-tree】bzoj2648 SJY摆棋子
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
随机推荐
- vsCode scss安装
点击在settings.json中编辑写入代码: { /** Easy Sass 插件 **/ "easysass.formats": [ { "format" ...
- 用TWaver加载大型游戏场景一例
游戏中经常会出现一些大型的户外场景,例如一个小镇.一座古城等.通常这种场景中包含了较多的建筑.道路.桥梁等等元素,其3D模型比较大且复杂.在使用TWaver加载时,可使用一些技巧,让加载速度更快.显示 ...
- 【模板】51nod 1051 最大子矩阵和
[题解] 二重循环枚举起始列和终止列,竖着往下加,转化为一个最大子段和问题,逐行累加即可. #include<cstdio> #include<cstring> #includ ...
- BZOJ 3648 寝室管理
[题解] GDOI2016 Day2T3 如果给出的数据是一棵树那么皆大欢喜直接点分治就好了,用树状数组维护大于x的数的个数.如果是一棵基环树,我们先断掉环上的一条边,然后跑点分治:再加上经过这条边的 ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...
- codechef 写题计划
此后将查找各种codechef的脑洞题和好题写
- c# 缓存!
做项目的时候获取所有城市的时候,发现每次去获取都花费了很多时间,所以用缓存方法让效率更高! 这是我做的例子,如下: public class CacheGetCity { /// <summar ...
- List lambda 排序
Comparator<PromotionRule> comparator = Comparator.comparing(PromotionRule::getCreatedTime); pr ...
- log4j.properties配置内容的理解
一直知道log4j是用来记录日志的,但一直没去看log4j到底是怎么用的,这两天看了几个log4j.properties配置语句详解的帖子,在这里简陋地记录一下. 在完全不知道log4j怎么用的时候, ...
- 【Google Chrome】Google Chrome快捷键大全
相信很多朋友在使用过Google Chrome之后,就会不想回到原先使用的浏览器了,尤其是IE.没错Google Chrome的优点很多,已经获得了一大部分网友们的用户,软件志现在也是Firefox+ ...