【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 ...
随机推荐
- 读取Properties配置文件, 四种方式都可以得到webroot/WEB-INF/classes这个路径
下面四种方式都可以得到webroot/WEB-INF/classes这个路径,有什么区别,哪种方式最好? String path = this.getClass().getResource(" ...
- Mycat探索之旅(5)----常用的分片规则
分片枚举 通过在配置文件中配置可能的枚举id,自己配置分片,本规则适用于特定的场景,比如有些业务需要按照省份或区县来做保存, 而全国省份区县固定的,这类业务使用本条规则,配置如下: <table ...
- Rational Performance Tester(RPTv8.6) 在launch Schedule 时一直卡在 29%
solution:Rational Performance Tester(RPTv8.6) 在launch Schedule 时一直卡在 29% 打开Task Manager(Windows serv ...
- 理解shell中的atime,mtime,ctime
所有文件都有3个时间信息,保存在文件系统中 atime (Access time)是文件最后一此读的时间 或者执行文件的时间 mtime (Modified time)是文件最后一次写的时间(是在写入 ...
- applicationContext.xml 文件头报错Referenced file contains errors
问题如下:原先运行正常的项目,突然在applicationContext.xml 文件头报错 内容:Referenced file contains errors (http://www.spring ...
- Cannot find module 'webpack'
执行webpack命令报错 Error: Cannot find module 'webpack' at Function.Module._resolveFilename (module.js:325 ...
- sql DATEPART() MONTH() convert() cast() dateadd() DATEDIFF() with(nolock)
DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 DATEPART(datepart,date) date 参数是合法的日期表达式.datepart 参数 ...
- CentOS 6.5 X64 U盘启动盘制作
本教程是在Linux系统下制件,可以使用虚拟机安装Linux 1.准备一个8G的U盘,在Linux下分成Fat32---500M----root, 剩下的分成Ext3 ----data,并格式化. 2 ...
- MySql(一):linux 安装mysql数据库——yum安装法
mysql数据库有多种安装方式,本文只介绍在Linux服务器上最实用.最快捷的mysql server安装方法.一.Linux服务器yum安装(CentOS6.3 64位)所有在服务器上执行的命令,都 ...
- nginx正则说明
nginx正则说明 分类: nginx -- : 11758人阅读 评论() 收藏 举报 nginx正则表达式firefox ^~ 标识符后面跟一个字符串.Nginx将在这个字符串匹配后停止进行正则表 ...