【BZOJ】【2648】SJY摆棋子&【BZOJ】【2716】【Violet 3】天使玩偶
KD-Tree
传说中的kd树。。。前去膜拜了一下……写道模板题>_<
写kdtree的一些感想:
插入的时候是像可持久化线段树一样直接在最后开新节点,然后更新它所在的块。。
然而其实也是用原来的叶子再分割一次这块区域?
/**************************************************************
Problem: 2716
User: Tunix
Language: C++
Result: Accepted
Time:16492 ms
Memory:26280 kb
****************************************************************/ //BZOJ 2716
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
return r*v;
}
const int N=,INF=1e9;
/*******************template********************/ struct node{
int d[],mn[],mx[],l,r;
int& operator [] (int x){return d[x];}
void read(){d[]=getint(); d[]=getint();}
}t[N],tmp;
int n,m,D,root,ans,cnt; inline int dis(node a,node b){return abs(a[]-b[])+abs(a[]-b[]);}
bool operator < (node a,node b){return a[D]<b[D] || (a[D]==b[D] && a[!D]<b[!D]);} #define L t[o].l
#define R t[o].r
#define mid (l+r>>1)
void Push_up(int o){
F(i,,){
if (L){ t[o].mn[i]=min(t[o].mn[i],t[L].mn[i]); t[o].mx[i]=max(t[o].mx[i],t[L].mx[i]);}
if (R){ t[o].mn[i]=min(t[o].mn[i],t[R].mn[i]); t[o].mx[i]=max(t[o].mx[i],t[R].mx[i]);}
}
} int build(int l,int r,int dir){
D=dir;
nth_element(t+l,t+mid,t+r+);
F(i,,) t[mid].mn[i]=t[mid].mx[i]=t[mid][i];
if (l<mid) t[mid].l=build(l,mid-,dir^);
if (r>mid) t[mid].r=build(mid+,r,dir^);
Push_up(mid);
return mid;
} inline void Insert(int &o,int dir){
if (!o){
o=++cnt; t[o]=tmp;
F(i,,) t[o].mn[i]=t[o].mx[i]=t[o][i];
return;
}
if (tmp[dir]<t[o][dir]) Insert(L,dir^);
else Insert(R,dir^);
Push_up(o);
} inline int getdis(int o){
if (!o) return INF;
int ans=;
F(i,,) ans+=max(,t[o].mn[i]-tmp[i]);
F(i,,) ans+=max(,tmp[i]-t[o].mx[i]);
return ans;
} inline void query(int o){
int dl=getdis(L),dr=getdis(R),d0=dis(t[o],tmp);
ans=min(ans,d0);
if (dl<dr){
if (dl<ans) query(L);
if (dr<ans) query(R);
}else{
if (dr<ans) query(R);
if (dl<ans) query(L);
}
} int main(){
#ifndef ONLINE_JUDGE
freopen("2716.in","r",stdin);
freopen("2716.out","w",stdout);
#endif
cnt=n=getint(); m=getint();
F(i,,n) t[i].read();
root=build(,n,);
F(i,,m){
int t=getint(); tmp.read();
if (t==) Insert(root,);
else{
ans=INF;
query(root);
printf("%d\n",ans);
}
}
return ;
}
2648: SJY摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 1090 Solved: 359
[Submit][Status][Discuss]
Description
天,SJY显得无聊。在家自己玩。在一个棋盘上,有N个黑色棋子。他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距
离这个白色棋子最近的黑色棋子。此处的距离是 曼哈顿距离 即(|x1-x2|+|y1-y2|)
。现在给出N<=500000个初始棋子。和M<=500000个操作。对于每个白色棋子,输出距离这个白色棋子最近的黑色棋子的距离。同一
个格子可能有多个棋子。
Input
Output
Sample Input
1 1
2 3
2 1 2
1 3 3
2 4 2
Sample Output
2
HINT
kdtree可以过
Source
【BZOJ】【2648】SJY摆棋子&【BZOJ】【2716】【Violet 3】天使玩偶的更多相关文章
- 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摆棋子
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摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- bzoj 2648 SJY摆棋子 kd树
题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...
- BZOJ 2648 SJY摆棋子(KD Tree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...
- BZOJ 2648 SJY摆棋子(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...
- bzoj 2648 SJY摆棋子——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...
- bzoj 2648 SJY摆棋子 —— K-D树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...
- BZOJ 2648 SJY摆棋子 ——KD-Tree
[题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...
随机推荐
- Vue.js常见问题
1.Vuejs组件 vuejs构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue.c ...
- js-布尔值
1.任何JavaScript的值都可以转换为布尔值 下面这些将会转换为false(假值): undefined null 0 -0 NaN "" //空字符串 所有其他值,包括所有 ...
- 错记-checkbox radio
很多时候我想会用到浏览器默认的单选按钮或者复选框,比如说偷懒的时候或者心情不好的时候╮(╯﹏╰)╭, 在html结构里我想实现点击文字旁边的单选按钮就跟着选中或反之,像这样:
- 在Spark中使用Kryo序列化
spark序列化 对于优化<网络性能>极为重要,将RDD以序列化格式来保存减少内存占用. spark.serializer=org.apache.spark.serializer.Jav ...
- JQ定义
什么是jQuery对象 通过$(‘选择器’)获取到都是jQuery对象 什么是DOM对象 通过getElementById或者 getElementsByTagName或者getElementsByN ...
- 【积硅计划】http协议基础
http:超文本传输协议,它允许将超文本标记(html)文档从web服务器传送到浏览器.目前版本HTTP/1.1 http请求过程: proxy:代理服务器,网络信息的中转站.功能如下: ...
- 自定义Toast的显示效果
Activity: package com.example.editortoast; import android.app.Activity; import android.os.Bundle; im ...
- 重定向语句Response.Redirect()方法与Response.RedirectPermanent()对搜索引擎页面排名的影响
在ASP.NET中,开发人员经常使用Response.Redirect()方法,用编程的手法,将对老的URL的请求转到新的URL上.但许多开发人员没有意识到的是,Response.Redirect() ...
- opensuse 安装 Anaconda3 之后出现Could not start d-bus. Can you call qdbus?
最近在安装了opensue Leap42.1之后,想要学习一下python,就安装了Anaconda3,并且将Anaconda3的安装路径添加到了PATH里,但是在重新启动系统后,出现了"C ...
- 10 款提高开发效率的 jQuery/CSS3 组件
前端开发是一项十分繁琐而又耗体力的工作,如何更有效率的开发我们的应用,很多人会选择适当地使用一些jQuery插件.今天就要给大家分享10款可以提高开发效率的jQuery/CSS3组件.部分插件可以下载 ...