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. NYOJ-取石子

    (一) 描述一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作石子.游戏的规则是这样的.设有一堆石子,数量为N(1<=N<=1000000), ...

  2. Java中正则Matcher类的matches()、lookAt()和find()的区别

    在Matcher类中有matches.lookingAt和find都是匹配目标的方法,但容易混淆,整理它们的区别如下: matches:整个匹配,只有整个字符序列完全匹配成功,才返回True,否则返回 ...

  3. Java集合系列:-----------03ArrayList源码分析

    上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解ArrayLi ...

  4. 031医疗项目-模块三:药品供应商目录模块——供货商药品目录查询功能----------sql补充知识

    这个补充知识有一个点很有必要,视屏上的老师提出一点: 内链接关联查询: 如果表A和表B有一个外键关联 ,可以通过外键进行内链接查询 select dictinfo.*, dicttype.typena ...

  5. Razor 模板自己渲染出结果 string

    using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNet ...

  6. ios更新UI时请尝试使用performSelectorOnMainThread方法

    最近开发项目时发现联网获取到数据后,使用通知方式让列表刷新会存在死机的问题. 经过上网查找很多文章,都建议使用异步更新的方式,可是依然崩溃. 最后尝试使用performSelectorOnMainTh ...

  7. zabbix_proxy安装[yum mysql5.6]

      安装mysql rpm -ivh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm   修改mysql配置: [m ...

  8. adb logcat 基本用法

    入门android ,至少需要了解 adb 吧,那么打 log 也是必不可少的了. 下面简单介绍一下 adb logcat 基本用法: Usage: logcat [options] [filters ...

  9. 纯html的table打印注意事项

    1. 在firefox下,每页均会打印重复thead(表头),tfoot(表尾)的内容:IE8下无效(其它IE版本未测试) 2. 分页的处理 @media print {     .page-brea ...

  10. Chrome扩展开发之二——Chrome扩展中脚本的运行机制和通信方式

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...