KDTree模板,双倍经验啦啦啦~

#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x=getint()
using namespace std;
const int N = 500003;
const int inf = 0x7fffffff;
int getint() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - '0';
return k * fh;
}
bool D;
int n, t, root, ans;
struct KDT {
int d[2], ch[2], minn[2], maxn[2];
bool operator < (const KDT &a) const {return d[D] < a.d[D];}
void init() {for(int i = 0; i < 2; ++i) minn[i] = maxn[i] = d[i];}
} T[N << 1], QQ;
void pushup(int rt) {
for(int i = 0; i < 2; ++i)
if (T[rt].ch[i]) {
int x = T[rt].ch[i];
for(int j = 0; j < 2; ++j)
T[rt].minn[j] = min(T[rt].minn[j], T[x].minn[j]),
T[rt].maxn[j] = max(T[rt].maxn[j], T[x].maxn[j]);
}
}
int Build(int l = 1, int r = n, bool d = 0) {
D = d; int mid = (l + r) >> 1; nth_element(T + l, T + mid, T + r + 1);
T[mid].init();
if (l != mid) T[mid].ch[0] = Build(l, mid - 1, !d);
if (r != mid) T[mid].ch[1] = Build(mid + 1, r, !d);
pushup(mid);
return mid;
}
int ask(int rt, KDT p) {
int ret = 0;
for(int i = 0; i < 2; ++i)
ret += max(0, T[rt].minn[i] - p.d[i]), ret += max(0, p.d[i] - T[rt].maxn[i]);
return ret;
}
void insert(int rt = root, bool d = 0) {
bool flag = T[n].d[d] > T[rt].d[d];
if (T[rt].ch[flag]) insert(T[rt].ch[flag], !d);
else T[rt].ch[flag] = n;
pushup(rt);
}
int dis(KDT a, KDT b) {
return abs(a.d[0] - b.d[0]) + abs(a.d[1] - b.d[1]);
}
void Query(int rt = root, bool d = 0) {
int Dis = dis(T[rt], QQ), dl = inf, dr = inf;
ans = min(ans, Dis);
if (T[rt].ch[0]) dl = ask(T[rt].ch[0], QQ);
if (T[rt].ch[1]) dr = ask(T[rt].ch[1], QQ);
if (dl < dr) {
if (dl < ans) Query(T[rt].ch[0], !d);
if (dr < ans) Query(T[rt].ch[1], !d);
} else {
if (dr < ans) Query(T[rt].ch[1], !d);
if (dl < ans) Query(T[rt].ch[0], !d);
}
}
int main() {
read(n); read(t);
for(int i = 1; i <= n; ++i)
read(T[i].d[0]), read(T[i].d[1]);
int num;
for(root = Build(); t; --t) {
read(num); ans = inf;
if (num == 1) {
read(T[++n].d[0]); read(T[n].d[1]);
T[n].init();
insert();
} else {
read(QQ.d[0]); read(QQ.d[1]);
Query();
printf("%d\n", ans);
}
}
return 0;
}

现在学新东西感觉就是作死啊~

【BZOJ 2648】SJY摆棋子 & 【BZOJ 2716】【Violet 3】天使玩偶的更多相关文章

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

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

  2. BZOJ 2648: SJY摆棋子

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

  3. BZOJ 2648: SJY摆棋子 kdtree

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

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

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

  5. bzoj 2648 SJY摆棋子 kd树

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

  6. BZOJ 2648 SJY摆棋子(KD Tree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...

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

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

  8. bzoj 2648 SJY摆棋子——KDtree

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

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

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

  10. BZOJ 2648 SJY摆棋子 ——KD-Tree

    [题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...

随机推荐

  1. 【2016-10-11】【坚持学习】【Day2】【代理模式】

    今天学习了代理模式. 定义 官方: 代理模式:给某一个对象提供一个代理或占位符,并由代理对象来控制对原对象的访问. Proxy Pattern: Provide a surrogate or plac ...

  2. python读取excel并制表输出

    源码如下: #!/usr/bin/python #coding=UTF-8 import xlrd import sys from texttable import Texttable def she ...

  3. Unity Project Wizard (最近打开的项目记录)

    最近打开工程列表 当用Unity打开过的项目越来越多之后,在最近打开项目记录框中就会变的很长,那么如何才能删除最近打开的记录呢? Unity4.x最近打开的工程记录 Unity5.x最近打开的工程记录 ...

  4. USB Type-C 接口有什么优点?

    USB Type-C 接口有什么优点? 提到USB Type-C接口(以下简称为USB-C),大家第一个能想到的是USB-C接口能正反插,用起来很舒服.了解更多的可能还支持USB-C接口速度更快, 达 ...

  5. C# 文件下载四方法

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  6. java多线程系类:基础篇:10生产者消费者的问题

    概要 本章,会对"生产/消费者问题"进行讨论.涉及到的内容包括:1. 生产/消费者模型2. 生产/消费者实现 转载请注明出处:http://www.cnblogs.com/skyw ...

  7. 截取视图某一段另存为部分视图(Partial View)

    在做ASP.NET MVC后台管理程序时,根据程序需要,Isus.NET需要实现一个功能,就是动态截取视图某一段另存为部分视图Partial View. 思路为在视图中,使用jQury的程序截图以及P ...

  8. 安装Docker Toolbox后出现的问题

    Installing Docker Toolbox on Windows with Hyper-V Installed Installing Docker on Windows is a fairly ...

  9. 自定义圆形控件RoundImageView并认识一下attr.xml

    今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学 ...

  10. Button、ImageButton及ImageView详解

    Button.ImageButton及ImageView详解 在应用程序开发过程中,很多时候需要将View的background或者src属性设置为图片,即美观又支持点击等操作.常见的有Button. ...