BZOJ 2648 SJY摆棋子(KD Tree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2648
题意:
思路:
KDtree模板题。
参考自http://www.cnblogs.com/rayrayrainrain/p/6349899.html。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int x,y;
int n,m;
int ans;
int cmp_d,root; struct node
{
int d[],MAX[],MIN[];
int l,r;
}t[]; bool cmp(node a, node b)
{
return a.d[cmp_d]<b.d[cmp_d]||a.d[cmp_d]==b.d[cmp_d] && a.d[cmp_d^] < b.d[cmp_d^];
} void PushUp(int p,int k)
{
t[p].MAX[]=max(t[p].MAX[],t[k].MAX[]);
t[p].MAX[]=max(t[p].MAX[],t[k].MAX[]);
t[p].MIN[]=min(t[p].MIN[],t[k].MIN[]);
t[p].MIN[]=min(t[p].MIN[],t[k].MIN[]);
} int build(int l,int r, int D)
{
int mid = (l+r) >> ;
cmp_d = D;
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^) ;
else t[mid].l = ;
if(r!=mid) t[mid].r = build(mid+,r,D^);
else t[mid].r = ;
if(t[mid].l) PushUp(mid,t[mid].l);
if(t[mid].r) PushUp(mid,t[mid].r);
return mid ;
} void update(int k)
{
int p = root ;
int D = ;
while(true)
{
PushUp(p,k);
if(t[k].d[D] <= t[p].d[D])
{
if(!t[p].l)
{
t[p].l = k ;
return;
}
p = t[p].l ;
}
else
{
if(!t[p].r){
t[p].r = k ;
return;
}
p = t[p].r ;
}
D ^= ;
}
} int getdis(int p,int x,int y)
{
int res = ;
if(x > t[p].MAX[])res += x - t[p].MAX[];
if(x < t[p].MIN[])res += t[p].MIN[] - x;
if(y > t[p].MAX[])res += y - t[p].MAX[];
if(y < t[p].MIN[])res += t[p].MIN[] - y;
return res ;
} void query(int p)
{
int d0 = abs(x - t[p].d[]) + abs(y - t[p].d[]) ;
if(d0<ans) ans = d0 ;
int dl , dr ;
if(t[p].l) dl=getdis(t[p].l,x,y) ; else dl = INF ;
if(t[p].r) dr=getdis(t[p].r,x,y) ; else dr = INF ;
if(dl < dr)
{
if(dl < ans) query(t[p].l) ;
if(dr < ans) query(t[p].r) ;
}
else
{
if(dr < ans) query(t[p].r) ;
if(dl < ans) query(t[p].l) ;
}
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i = ; i <= n ; i ++ )
scanf("%d%d",&t[i].d[] , &t[i].d[]);
if(n) root = build(,n,) ;
for(int i = ; i <= m ; i ++ )
{
int q; scanf("%d%d%d",&q,&x,&y);
if(q == )
{
n++ ;
t[n].d[]=t[n].MAX[]=t[n].MIN[]=x;
t[n].d[]=t[n].MAX[]=t[n].MIN[]=y;
if(n>) update(n);
else root = build(,n,);
}
else
{
ans = INF;
query(root);
printf("%d\n",ans);
}
}
return ;
}
BZOJ 2648 SJY摆棋子(KD Tree)的更多相关文章
- 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摆棋子 —— K-D树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...
- 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摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec Memory Limit: 128 MB Description 这天,S ...
- luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree
k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...
- 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 ...
随机推荐
- 谷歌重磅开源强化学习框架Dopamine吊打OpenAI
谷歌重磅开源强化学习框架Dopamine吊打OpenAI 近日OpenAI在Dota 2上的表现,让强化学习又火了一把,但是 OpenAI 的强化学习训练环境 OpenAI Gym 却屡遭抱怨,比如不 ...
- 今天2.4寸tft触摸屏到手--刷屏驱动小结
2010-04-29 21:28:00 根据给的51程序改成了iccavr,结果改错了2处.导致我找原因找了n久.不过也是一件好事,让我对80i更加熟悉了. 通过protues的逻辑分析仪,找到了问题 ...
- bzoj1606
题目链接 反着循环就是每个东西只能选一次 #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- js关于移入移出延迟提示框效果处理
html部分 <div id="div1">我是导航君</div> <div id="div2" style="disp ...
- EditPlus配置GCC
--GCC Compile-- 命令:D:\GCC\MinGW_RP_Green\bin\gcc.exe 参数:$(FileName) -o $(FileNameNoExt).exe 初始目录:$(F ...
- MySQL备份与恢复-mydumper
上一片博文中,我们说明了mysqldump的备份与恢复.因为mysqldump是单线程导出,单线程恢复的,因此备份与恢复的时间比较长! 首先来安装mydumper: 下载源码:https://gith ...
- 关于functools模块的wraps装饰器用途
测试环境:Python3.6.2 + win10 + Pycharm2017.3 装饰器之functools模块的wraps的用途: 首先我们先写一个装饰器 # 探索functools模块wraps ...
- Linux 系统下安装 python-skimage
Linux 系统下安装 python-skimage 安装必须的依赖 // python-mumpy // python-scipy // python-matplotlib $ sudo apt-g ...
- linux 安装二进制包程序一般步骤
参考:https://blog.csdn.net/linzhiji/article/details/6774410 configure/make/make install的作用 这些都是典型的使用GN ...
- py4CV例子2汽车检测和svm算法
1.什么是汽车检测数据集: ) pos, neg = , ) matcher = cv2.FlannBasedMatcher(flann_params, {}) bow_kmeans_trainer ...