BZOJ 2648 SJY摆棋子(KD Tree)
2024-08-26 12:06:14
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 ;
}
最新文章
- 动态规划 Dynamic Programming
- javascript关闭页面
- ThreadLocal实现方式&;使用介绍—无锁化线程封闭
- Web应用程序系统的多用户权限控制设计及实现-栏目模块【8】
- 使用cookie保存页面登录信息
- HTTP POST GET 本质区别详解
- oc-13-多文件
- maven的pom报plugins却是的解决方法2
- BeagleBone Black Linux驱动程序开发入门(1): LED驱动程序
- 单元测试利器JUnit4
- angular : direative : scope | 指令scope里的符号@,=
- bzoj 1899: [Zjoi2004]Lunch 午餐
- Web资源认证原理
- 原 线程池中shutdown()和shutdownNow()方法的区别
- vue项目实战中的增、删、改、查
- linux下安装python,Django,虚拟环境
- Docker镜像推送(push)到Docker Hub
- js并归排序的思路
- Django中HtttpRequest请求
- Spring MVC 请求处理方法
热门文章
- flask 自定义验证器(行内验证器、全局验证器)
- 【Alpha版本】冲刺阶段——Day6
- bootsrtap h5 移动版页面 在苹果手机ios滑动上下拉动滚动卡顿问题解决方法
- div居中与div内容居中,不一样
- C调用java JNI_CreateJavaVM只能调用成功一次
- 05: 使用axios/vue-resource发送HTTP请求
- android官方开发教程解释(一)
- 在用网站ICP备案主体变更导致网站无法访问问题解决
- 2018-2019-1 20189206 vim.c插件安装
- tp5 中使用自定义扩展类和函数