Bzoj2648 SJY摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 3128 Solved: 1067
Description
Input
Output
Sample Input
1 1
2 3
2 1 2
1 3 3
2 4 2
Sample Output
2
HINT
kdtree可以过
Source
K-Dtree模板题
似乎有种懂了的错觉
(錯覚?それでは、聞いてください....)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=1e9;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
int nowD;
struct node{
int min[],max[];
int d[];
int l,r;
}t[mxn<<];
int root,nct=;
int cmp(const node a,const node b){
if(a.d[nowD]==b.d[nowD])return a.d[nowD^]<b.d[nowD^];
return a.d[nowD]<b.d[nowD];
}
int qx,qy;//查询参数
void pushup(int rt,int c){//更新结点信息
t[rt].max[]=max(t[rt].max[],t[c].max[]);
t[rt].min[]=min(t[rt].min[],t[c].min[]);
t[rt].max[]=max(t[rt].max[],t[c].max[]);
t[rt].min[]=min(t[rt].min[],t[c].min[]);
return;
}
int Build(int l,int r,int D){
nowD=D;int mid=(l+r)>>;
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^);
if(t[mid].l)pushup(mid,t[mid].l);
if(t[mid].r)pushup(mid,t[mid].r);
return mid;
}
void insert(int x){//插入新结点
int now=root,D=;
while(){
pushup(now,x);
if(t[x].d[D]>=t[now].d[D]){
if(!t[now].r){t[now].r=x;return;}
else now=t[now].r;
}
else{
if(!t[now].l){t[now].l=x;return;}
else now=t[now].l;
}
D^=;
}
return;
}
int Dist(int k){//估价
int res=;
if(qx<t[k].min[])res+=t[k].min[]-qx;
if(qx>t[k].max[])res+=qx-t[k].max[];
if(qy<t[k].min[])res+=t[k].min[]-qy;
if(qy>t[k].max[])res+=qy-t[k].max[];
return res;
}
int ans;
void query(int rt){
int dx,dy,tmp;
// printf("rt:%d pos:%d %d ans:%d\n",rt,t[rt].d[0],t[rt].d[1],ans);
tmp=abs(t[rt].d[]-qx)+abs(t[rt].d[]-qy);
if(tmp<ans)ans=tmp;
if(t[rt].l) dx=Dist(t[rt].l);else dx=INF;
if(t[rt].r) dy=Dist(t[rt].r);else dy=INF;
if(dx<dy){
if(dx<ans) query(t[rt].l);
if(dy<ans) query(t[rt].r);
}
else{
if(dy<ans) query(t[rt].r);
if(dx<ans) query(t[rt].l);
}
return;
}
int n,m;
int main(){
int i,j;
n=read();m=read();
int T,x,y;
for(i=;i<=n;i++){
t[++nct].d[]=read();
t[nct].d[]=read();
}
root=Build(,n,);
while(m--){
T=read();x=read();y=read();
if(T==){
t[++nct].d[]=x; t[nct].d[]=y;
t[nct].max[]=t[nct].min[]=t[nct].d[];
t[nct].max[]=t[nct].min[]=t[nct].d[];
insert(nct);
}
else{
qx=x;qy=y;ans=INF;
query(root);
printf("%d\n",ans);
}
}
return ;
}
Bzoj2648 SJY摆棋子的更多相关文章
- BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶
BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...
- [BZOJ2648] SJY摆棋子 kd-tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 5421 Solved: 1910[Submit][Status][Disc ...
- BZOJ2648 SJY摆棋子(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 【kd-tree】bzoj2648 SJY摆棋子
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- 2019.01.14 bzoj2648: SJY摆棋子(kd-tree)
传送门 kd−treekd-treekd−tree模板题. 题意简述:支持在平面上插入一个点,求对于一个点的最近点对. 思路:cdqcdqcdq是一种很不错的分治方法 只是好像码量有点窒息 所以我用了 ...
- KDTree(Bzoj2648: SJY摆棋子)
题面 传送门 KDTree 大概就是一个分割\(k\)维空间的数据结构,二叉树 建立:每层选取一维为关键字,把中间的点拿出来,递归左右,有个\(STL\)函数nth_element可以用一下 维护:维 ...
- [bzoj2648]SJY摆棋子(带插入kd-tree)
解题关键:带插入kdtree模板题. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree
k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...
- 【BZOJ2648】SJY摆棋子(KD-Tree)
[BZOJ2648]SJY摆棋子(KD-Tree) 题面 BZOJ Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
随机推荐
- (原创)在Exchange 2007 server中使用实时黑名单服务(RBL)过滤垃圾邮件
近一段有同事抱怨公司的邮件系统垃圾邮件非常多,早上上班打开邮箱垃圾邮件竟然有几十封.然后赶紧打开Exchange工具“邮件跟踪”,发现有每隔1到2分钟,邮件系统便会收到来自不同IP的垃圾邮件,由于源I ...
- Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
- Laravel 下结合阿里云邮件推送服务
最近在学习laravel做项目开发,遇到注册用户推送邮件的问题,之前用java做的时候是自己代码写的,也就是用ECS推送邮件,但是现在转php的laravel了就打算用php的邮件发送功能来推送邮件, ...
- 采访ServiceStack的项目领导Demis Bellot——第1部分(网摘)
ServiceStack是一个开源的.支持.NET与Mono平台的REST Web Services框架.InfoQ有幸与Demis Bellot深入地讨论了这个项目.在这篇两部分报道的第1部分中,我 ...
- redis连接数
1.应用程序会发起多少个请求连接?1)对于php程序,以短连接为主.redis的连接数等于:所有web server接口并发请求数/redis分片的个数.2)对于java应用程序,一般使用JedisP ...
- 基于FPGA的电压表与串口通信(下)
实验操作 上电 接入5V电源,用配套的线,USB那端接电脑即可: 电源开关 按下电源开关 接串口线 接下载线 现在电脑装串口线驱动 R340qd.zip 双击进行安装 设置串口调试助手 Com1要根据 ...
- Simple File System
This is my operating system class design. scanner.h #include<string> using namespace std; #pra ...
- python2.7到python3代码转换脚本2to3的一些介绍
你的位置: Home ‣ Dive Into Python 3 ‣ 难度等级: ♦♦♦♦♦ 使用2to3将代码移植到Python 3 ❝ Life is pleasant. Death is ...
- Python基础-字符串格式化_百分号方式_format方式
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...