【BZOJ2648】SJY摆棋子 KDtree
【BZOJ2648】SJY摆棋子
Description
Input
Output
Sample Input
1 1
2 3
2 1 2
1 3 3
2 4 2
Sample Output
2
题解:初学KDtree,感兴趣的可以看一下
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define rep for(i=0;i<=1;i++)
using namespace std;
struct KD
{
int ls,rs,v[2],sn[2],sm[2];
KD (int a,int b){ls=rs=0,sn[0]=sm[0]=v[0]=a,sn[1]=sm[1]=v[1]=b;}
KD (){}
}t[1000010];
int D,n,m,tot,ans,root;
bool cmp(KD a,KD b)
{
if(a.v[D]==b.v[D]) return a.v[D^1]<b.v[D^1];
return a.v[D]<b.v[D];
}
void pushup(int x,int y)
{
int i;
rep t[x].sn[i]=min(t[x].sn[i],t[y].sn[i]),t[x].sm[i]=max(t[x].sm[i],t[y].sm[i]);
}
int getdis(int x,int y)
{
int ret=0,i;
rep ret+=max(t[x].sn[i]-t[y].v[i],0)+max(t[y].v[i]-t[x].sm[i],0);
return ret;
}
int build(int l,int r,int d)
{
if(l>r) return 0;
D=d;
int mid=l+r>>1;
nth_element(t+l,t+mid,t+r+1,cmp);
t[mid].ls=build(l,mid-1,d^1);
t[mid].rs=build(mid+1,r,d^1);
if(t[mid].ls) pushup(mid,t[mid].ls);
if(t[mid].rs) pushup(mid,t[mid].rs);
return mid;
}
void insert(int y)
{
int x=root;
D=0;
while(1)
{
pushup(x,y);
if(t[y].v[D]<t[x].v[D])
{
if(t[x].ls) x=t[x].ls;
else {t[x].ls=y,pushup(x,y); break;}
}
else
{
if(t[x].rs) x=t[x].rs;
else {t[x].rs=y,pushup(x,y); break;}
}
D^=1;
}
}
void query(int x,int y)
{
if(!x||getdis(x,y)>ans) return ;
ans=min(ans,abs(t[x].v[0]-t[y].v[0])+abs(t[x].v[1]-t[y].v[1]));
if(!(t[x].ls*t[x].rs))
{
query(t[x].ls^t[x].rs,y);
return ;
}
int dl=getdis(t[x].ls,y),dr=getdis(t[x].rs,y);
if(dl<dr) query(t[x].ls,y),query(t[x].rs,y);
else query(t[x].rs,y),query(t[x].ls,y);
}
int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int main()
{
n=rd(),m=rd();
int i,a,b,c;
for(i=1;i<=n;i++) a=rd(),b=rd(),t[i]=KD(a,b);
root=build(1,n,0);
for(i=1;i<=m;i++)
{
a=rd(),b=rd(),c=rd(),t[i+n]=KD(b,c);
if(a==1) insert(i+n);
else ans=1<<30,query(root,i+n),printf("%d\n",ans);
}
return 0;
}
【BZOJ2648】SJY摆棋子 KDtree的更多相关文章
- [BZOJ2648] SJY摆棋子 kd-tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 5421 Solved: 1910[Submit][Status][Disc ...
- BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶
BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...
- 【BZOJ2648】SJY摆棋子 [KD-tree]
SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 这天,SJY显得无聊.在家自己 ...
- BZOJ 2648: SJY摆棋子 kdtree
2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...
- luoguP4169 [Violet]天使玩偶/SJY摆棋子 K-Dtree
P4169 [Violet]天使玩偶/SJY摆棋子 链接 luogu 思路 luogu以前用CDQ一直过不去. bzoj还是卡时过去的. 今天终于用k-dtree给过了. 代码 #include &l ...
- BZOJ2648 SJY摆棋子(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- KDTree(Bzoj2648: SJY摆棋子)
题面 传送门 KDTree 大概就是一个分割\(k\)维空间的数据结构,二叉树 建立:每层选取一维为关键字,把中间的点拿出来,递归左右,有个\(STL\)函数nth_element可以用一下 维护:维 ...
- [bzoj2648]SJY摆棋子(带插入kd-tree)
解题关键:带插入kdtree模板题. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- 【kd-tree】bzoj2648 SJY摆棋子
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
随机推荐
- spring 动态定时任务
功能介绍:商品自动上架.按修改或添加时设置的自动上架时间而启动定时任务 更改商品状态为上架. spring 中配置文件 <?xml version="1.0" encodin ...
- 模式识别之Earley算法入门详讲
引言:刚学习模式识别时,读Earley算法有些晦涩,可能是自己太笨.看了网上各种资料,还是似懂非懂,后来明白了,是网上的前辈们境界太高,写的最基本的东西还是非常抽象,我都领悟不了,所以决定写个白痴版的 ...
- 阿里云创建RAM子账号
操作步骤 创建子账号 主账号导航至 访问控制 > 用户管理 页面.如下图所示: 单击右上角的 新建用户,如下图所示: 填写弹出框的各配置项,如下图所示: 单击 确定,即可创建子账号. 允许子账号 ...
- 转FTP协议详解
转自:http://www.cnblogs.com/li0803/archive/2010/11/16/1878833.html FTP 是File Transfer Protocol(文件传输协议) ...
- Linux LVM逻辑卷配置过程详解(创建、扩展、缩减、删除、卸载、快照创建)(未完)
转:http://blog.csdn.net/xuanfeng407/article/details/51465472
- Redis之最大内存置换策略
0.前言 Redis默认最大内存大小是应用程序可访问的内存大小, 32位windows下是2GB, linux下是3GB. 64位下可以访问的内存为2^64字节, Redis提供了maxmemory字 ...
- core image几个滤镜样例 oc版本号和swift版本号
oc版本号 //万花筒模式 + (CGImageRef) getKaleidoscope:(CIContext *)context { CIImage * image = [CIImage image ...
- Flex colorTranfrom使用说明
这次使用colorTranfrom主要用来将一个已有的过渡颜色映射到其他颜色条.发现这个colorTranfrom很好使用,于是简单研究了下 文档有说明: 使用 ColorTransform 类调整显 ...
- jquery的post()
jQuery ajax - post() 方法 jQuery Ajax 参考手册 实例 请求 test.php 网页,忽略返回值: $.post("test.php"); TIY ...
- AndroidManifest.xml配置
AndroidManifest.xml配置文件对于Android应用开发来说是非常重要的基础知识,本文旨在总结该配置文件中重点的用法,以便日后查阅.下面是一个标准的AndroidManifest.xm ...