BZOJ2648:SJY摆棋子
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=2648
\(K-D\) \(Tree\)最近点查询裸题,注意先把所有点建好再一个个激活。
时间复杂度:\(O(nlogn)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=5e5+5,inf=2e9;
int n,m,pps,ans,node[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct query {
int opt,x,y;
}q[maxn];
struct kd_tree {
int cnt,root;
int fa[maxn<<1];
struct point {
int c[2],mn[2],mx[2],id,ls,rs;
point() {}
point(int _x,int _y,int _id) {
c[0]=_x,c[1]=_y,id=_id;
if(id<=n)mn[0]=mx[0]=c[0],mn[1]=mx[1]=c[1];
else mn[0]=mn[1]=inf,mx[0]=mx[1]=-inf;
ls=rs=0;
}
bool operator<(const point &a)const {
return c[pps]<a.c[pps];
}
}p[maxn<<1];
void update(int u) {
int ls=p[u].ls,rs=p[u].rs;
for(int i=0;i<2;i++) {
int mn=min(p[ls].mn[i],p[rs].mn[i]);
p[u].mn[i]=min(p[u].mn[i],mn);
int mx=max(p[ls].mx[i],p[rs].mx[i]);
p[u].mx[i]=max(p[u].mx[i],mx);
}
}
int build(int l,int r,int cmp) {
int mid=(l+r)>>1,u=mid;pps=cmp;
nth_element(p+l,p+mid,p+r+1);
if(p[u].id>n)node[p[u].id-n]=u;
if(l<mid)fa[p[u].ls=build(l,mid-1,cmp^1)]=u;
if(r>mid)fa[p[u].rs=build(mid+1,r,cmp^1)]=u;
update(u);
return u;
}
void prepare() {
p[0]=point(0,0,2e9);
for(int i=1;i<=n;i++) {
int x=read(),y=read();
p[i]=point(x,y,i);
}
for(int i=1;i<=m;i++) {
int opt=read(),x=read(),y=read();
q[i].opt=opt,q[i].x=x,q[i].y=y;
if(opt==1)p[++cnt]=point(x,y,n+i);
}
root=build(1,cnt,0);
}
void arouse(int u) {
p[u].mn[0]=p[u].mx[0]=p[u].c[0];
p[u].mn[1]=p[u].mx[1]=p[u].c[1];
while(fa[u])update(u),u=fa[u];
update(u);
}
int dis(int id,int x,int y) {
int res=0;
if(x<p[id].mn[0])res+=p[id].mn[0]-x;
if(x>p[id].mx[0])res+=x-p[id].mx[0];
if(y<p[id].mn[1])res+=p[id].mn[1]-y;
if(y>p[id].mx[1])res+=y-p[id].mx[1];
return res;
}
void query(int u,int x,int y,int id) {
if(p[u].id<id)ans=min(ans,abs(x-p[u].c[0])+abs(y-p[u].c[1]));
int dl=p[u].ls?dis(p[u].ls,x,y):inf;
int dr=p[u].rs?dis(p[u].rs,x,y):inf;
if(dl<dr) {
if(dl<ans)query(p[u].ls,x,y,id);
if(dr<ans)query(p[u].rs,x,y,id);
}
else {
if(dr<ans)query(p[u].rs,x,y,id);
if(dl<ans)query(p[u].ls,x,y,id);
}
}
}T;
int main() {
T.cnt=n=read(),m=read();
T.prepare();
for(int i=1;i<=m;i++)
if(q[i].opt==1)T.arouse(node[i]);
else {
ans=2e9;
T.query(T.root,q[i].x,q[i].y,n+i);
printf("%d\n",ans);
}
return 0;
}
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摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB Submit: 3128 Solved: 1067 Description 这天,SJY显得无聊.在家自己玩.在一个 ...
- 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个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...
随机推荐
- 【leetcode刷题笔记】Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 【转载】linux下使用 TC 对服务器进行流量控制
tc 介绍 在linux中,tc 有二种控制方法 CBQ 和 HTB.HTB 是设计用来替换 CBQ 的.HTB比CBQ更加灵活,但是CPU 开销也更大,通常高速的链路会使用CBQ,一般而言HTB使用 ...
- poj 2406 Power Strings【字符串+最小循环节的个数】
Po ...
- Bluetooth Profile for iPhone from the functional perspectives
Classic Bluetooth Profile for iPhone from the functional perspectives Function Description BT Profil ...
- JavaWeb -- http-equiv=refresh跳转的时候出现Session 丢失, 解决办法。。
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- keystone uwsgi failed
~$ /usr/local/bin/uwsgi /etc/keystone/keystone-uwsgi-public.ini[uWSGI] getting INI configuration fro ...
- nova rebuild
nova rebuild¶ usage: nova rebuild [--rebuild-password <rebuild-password>] [--poll] [--minimal] ...
- 二叉查找树--java
package com.test.tree; public class BinarySearchTree<T extends Comparable<? super T>> { ...
- zoj 3963 Heap Partition(并查集,贪心,二分)
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- 用node.js可以开启静态服务 不需要借助apache 或者xampl
安装好了Node以及express,然后用express命令生成express架构, 目录结构下面有一个public页面, 把你的静态页面放到这个文件夹下, 通过npm start,开启服务就可以在浏 ...