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)的更多相关文章

  1. BZOJ 2648: SJY摆棋子(K-D Tree)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 6051  Solved: 2113[Submit][Status][Discuss] Descript ...

  2. bzoj 2648 SJY摆棋子 kd树

    题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...

  3. bzoj 2648 SJY摆棋子 —— K-D树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...

  4. BZOJ 2648: SJY摆棋子

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2968  Solved: 1011[Submit][Status][Disc ...

  5. BZOJ 2648: SJY摆棋子 kdtree

    2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...

  6. bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree

    2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec  Memory Limit: 128 MB Description 这天,S ...

  7. luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree

    k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...

  8. BZOJ 2648 SJY摆棋子(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...

  9. bzoj 2648 SJY摆棋子——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...

随机推荐

  1. 使用Flask-CKEditor集成富文本编辑框

    使用Flask-CKEditor集成富文本编辑框 富文本编辑器即所见即所得编辑器,类似于文本编辑软件.它提供一系列按钮和下拉列表来为文本设置格式,编辑状态的文本样式即最终呈现出来的样式.在Web程序中 ...

  2. SpringMVC常用注解的规则(用法)

    SpringMVC注解 @RequestMapping用法:    a. 用在controller方法上:        标记url到请求方法的映射, 其实就是通过一段url地址, 找到对应需要执行的 ...

  3. Linux基础命令---显示登录用户logname

    logname 显示当前登录的用户名称.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法        logname   2 ...

  4. spring boot 概念

    最近新版本迭代,一直在弄框架替换和新技术实现的事儿. 本来想仔细介绍一下Spring Boot的各种东西,后来发现没啥写的,Spring Boot 说白了就是把你开发过程中用到的各种框架给你封装了一下 ...

  5. 那些年 Qzone

    那些年转在Qzone里的: 不喊痛,不一定没感觉.不要求,不一定没期待.不落泪,不一定没伤痕.不说话,不一定没心声.沉默,不代表自己没话说.离开,不代表自己很潇洒.快乐,不代表自己没伤心.幸福,不代表 ...

  6. 服务器网络地址 "TCP://XXX:5022" 无法访问或不存在。请检查网络地址

    把主机上数据库,完整备份一份库文件和事务日志文件,“with non recover”方式还原过去,再重新执行SQL指令.当跨IP段的时候就需要在主备机的 C:\Windows\System32\dr ...

  7. 前端框架VUE----面向对象

    JavaScript 语言中,生成实例对象的传统方法是通过构造函数. function Animal(name,age){ this.name = name; this.age = age; } An ...

  8. 利用yum搭建lamp环境并进一步创建博客

    用yum搭建lamp环境 第一.安装apache yum -y install httpd 第二.安装mariadb Yum -y mariadb mariadb-server 第三.安装php Yu ...

  9. [c/c++] programming之路(4)、常量和变量

    一.打开多个程序(温习) #include<stdio.h> #include<stdlib.h> void main(){ ; ]; scanf("%d" ...

  10. php 常用代码片断

    参考: https://www.jianshu.com/p/f5303225ef92 http://www.phpxs.com/code/php/