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. java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况

    java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况 邮件短信微服务 spring boot 微服务 接收json格式参数 验证参数合 ...

  2. maven 入门 (一)

    纠结了好久,到底要不要写一份maven入门的所谓“教程”,有好几次想写一下,但是都放弃了,因为网上的太多了,而且都很好,但是现在决定了,还是要写出来,因为者有我自己的理解.所以我想写一份教程出来. 首 ...

  3. Django 安装 创建项目 运行项目

    Django基础 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的We ...

  4. kivy 滑动

    from kivy.uix.gridlayout import GridLayout from kivy.app import App from kivy.lang.builder import Bu ...

  5. self asyncio

    import asyncio from threading import Thread import time print('main start:',time.time()) async def d ...

  6. [重要] Django 多条件多表查询实例问题

    当时想做一个多条件查询,但是对于要查询的信息,是分布在不同的表里,这就涉及到了多表查询问题. DjangoBook里提到了一些查询的方式,但是不够全面,就去百度搜了下. 当去网上百度搜多表查询,或多条 ...

  7. PHP结合Vue实现上拉分页

    效果图: <?php if(isset($_GET['data'])){ $data = [ [ 'title'=>1], [ 'title'=>2], [ 'title'=> ...

  8. bzoj1227 P2154 [SDOI2009]虔诚的墓主人

    P2154 [SDOI2009]虔诚的墓主人 组合数学+离散化+树状数组 先看题,结合样例分析,易得每个墓地的虔诚度=C(正左几棵,k)*C(正右几棵,k)*C(正上几棵,k)*C(正下几棵,k),如 ...

  9. Java线程同步与锁

    一.synchronized synchronized锁什么?锁对象.可能锁对象包括: this, 临界资源对象,Class类对象. 1,同步方法 synchronized T methodName( ...

  10. 转:网卡、光纤网卡、ISCSI卡有什么区别?

    网卡.光纤网卡.ISCSI卡有什么区别? 感谢: http://www.it.com.cn/f/server/063/6/241650.htm 在讨论这个问题的时候,需要先说清楚一个问题:我们知道,在 ...