BZOJ 2648: SJY摆棋子
2648: SJY摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 2968 Solved: 1011
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 1
2 3
2 1 2
1 3 3
2 4 2
Sample Output
2
HINT
kdtree可以过
Source
既然题目都这么友好地告诉我们KD-Tree可解了,那就没理由不写KD-Tree了,对吧。( ̄_, ̄ )
对于原本就存在的黑色棋子,那么就先读入,然后用build()建树;对于之后插入的黑色棋子,在insert()中根据KD-Tree的二叉树性质找到合适的叶子结点,插入即可。查询的时候,记得用最优性剪枝,并且合理利用估价函数,以避免TLE。
#include <bits/stdc++.h>
inline int next(void) {
int ret = , neg = , bit = getchar();
while (bit < '') {
if (bit == '-')
neg ^= true;
bit = getchar();
}
while (bit >= '') {
ret = ret* + bit - '';
bit = getchar();
}
return neg ? -ret : ret;
}
const int siz = 2e6 + ;
const int inf = 2e9 + ;
struct node {
int pos[];
int son[];
int min[];
int max[];
}tree[siz];
int root;
int cmp_k;
int qry_x;
int qry_y;
int answer;
inline bool cmp(node a, node b) {
return
(a.pos[cmp_k] ^ b.pos[cmp_k])
? (a.pos[cmp_k] < b.pos[cmp_k])
: (a.pos[!cmp_k] < b.pos[!cmp_k]);
}
inline void update(int t) {
for (int i = ; i < ; ++i)if (tree[t].son[i])
for (int j = ; j < ; ++j) {
if (tree[t].min[j] > tree[tree[t].son[i]].min[j])
tree[t].min[j] = tree[tree[t].son[i]].min[j];
if (tree[t].max[j] < tree[tree[t].son[i]].max[j])
tree[t].max[j] = tree[tree[t].son[i]].max[j];
}
}
int build(int l, int r, int k) {
int d = (l + r) >> ; cmp_k = k;
std::nth_element(
tree + l + ,
tree + d + ,
tree + r + ,
cmp);
if (l != d)tree[d].son[] = build(l, d - , !k);
if (r != d)tree[d].son[] = build(d + , r, !k);
tree[d].min[] = tree[d].max[] = tree[d].pos[];
tree[d].min[] = tree[d].max[] = tree[d].pos[];
return update(d), d;
}
inline void insert(int t) {
for (int p = root, k = ; p != t; k = !k) {
for (int i = ; i < ; ++i) {
if (tree[p].min[i] > tree[t].min[i])
tree[p].min[i] = tree[t].min[i];
if (tree[p].max[i] < tree[t].max[i])
tree[p].max[i] = tree[t].max[i];
}
int &to = tree[p].son[tree[t].pos[k] >= tree[p].pos[k]];
to = to ? to : t; p = to;
}
}
inline int dist(int t) {
if (!t)return inf;
int ret = ;
if (qry_x < tree[t].min[])
ret += tree[t].min[] - qry_x;
if (qry_x > tree[t].max[])
ret += qry_x - tree[t].max[];
if (qry_y < tree[t].min[])
ret += tree[t].min[] - qry_y;
if (qry_y > tree[t].max[])
ret += qry_y - tree[t].max[];
return ret;
}
void query(int t) {
answer = std::min(answer,
std::abs(tree[t].pos[] - qry_x)
+ std::abs(tree[t].pos[] - qry_y));
if (dist(tree[t].son[]) < dist(tree[t].son[])) {
if (dist(tree[t].son[]) < answer)query(tree[t].son[]);
if (dist(tree[t].son[]) < answer)query(tree[t].son[]);
} else {
if (dist(tree[t].son[]) < answer)query(tree[t].son[]);
if (dist(tree[t].son[]) < answer)query(tree[t].son[]);
}
}
signed main(void) {
int n = next();
int m = next();
for (int i = ; i <= n; ++i) {
tree[i].pos[] = next();
tree[i].pos[] = next();
}
root = build(, n, );
for (int i = ; i <= m; ++i) {
int k = next();
int x = next();
int y = next();
if (k == ) {
++n;
tree[n].min[] = tree[n].max[] = tree[n].pos[] = x;
tree[n].min[] = tree[n].max[] = tree[n].pos[] = y;
insert(n);
}
else {
qry_x = x;
qry_y = y;
answer = inf;
query(root);
printf("%d\n", answer);
}
}
}
@Author: YouSiki
BZOJ 2648: SJY摆棋子的更多相关文章
- BZOJ 2648: SJY摆棋子 kdtree
2648: SJY摆棋子 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2648 Description 这天,SJY显得无聊.在家自己玩 ...
- bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec Memory Limit: 128 MB Description 这天,S ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- bzoj 2648 SJY摆棋子 kd树
题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...
- BZOJ 2648 SJY摆棋子(KD Tree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...
- bzoj 2648 SJY摆棋子——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 第一道KDtree! 学习资料:https://blog.csdn.net/zhl30 ...
- bzoj 2648 SJY摆棋子 —— K-D树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...
- BZOJ 2648 SJY摆棋子 ——KD-Tree
[题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...
- BZOJ 2648 SJY摆棋子(KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2716 [题目大意] 给出一些点,同时不断插入点和询问某点离插入点最近距离 [题解] 我 ...
随机推荐
- 使用 Fastlane 实现 IOS 持续集成
简介 持续集成是个“一次配置长期受益”的工作.但很多小公司都没有.以前在做Windows开发配置感觉简单一些,这次配置iOS的,感觉步骤还挺多.整理出来,分享给大家,不正确的地方请及时指正. 本文主要 ...
- 放松跑、间歇跑、节奏跑和LSD
放松跑(easy run),顾名思义,是没有负担的跑步,通常用于高强度训练之间,让机能得到恢复. 间歇跑(intervals),又叫变速跑,通常是用高于实际比赛速配速的速度进行反复短距离的快跑,当中配 ...
- 洛谷 1016 / codevs 1046 旅行家的预算
https://www.luogu.org/problem/show?pid=1016 http://codevs.cn/problem/1046/ 题目描述 Description 一个旅行家想驾驶 ...
- CentOS 7.0 Nvidia显卡安装步骤
from: http://blog.sina.com.cn/s/blog_49c0985a0102v3fa.html CentOS 7.0 Nvidia显卡安装步骤: 1 在英伟达官网下载相应驱动 搜 ...
- 浅谈设计模式--建造器模式(Builder Pattern)
建造器模式,是于创建带有大量参数的对象,并避免因参数数量多而产生的一些问题(如状态不一致-JavaBean的setter模式). 如果参数多且有些是必须初始化的,有些是不一定需要初始化的时候,创建对象 ...
- 品读吴军"之"系列
品读吴军"之"系列 这一两年,阅读吴军老师(微博,知乎专栏)的书占了我相当多的时间. 读吴军老师(微博,知乎专栏)的书,会让你心生敬佩,不禁想问"为什么有的作者有如此丰富 ...
- SQL Server中的索引结构与疑惑
说实话我从没有在实际项目中使用过索引,仅知道索引是一个相当重要的技术点,因此我也看了不少文章知道了索引的区别.分类.优缺点以及如何使用索引.但关于索引它最本质的是什么笔者一直没明白,本文是笔者带着这些 ...
- C# 调用一个按钮的Click事件(利用反射)
最基本的调用方法 (1)button1.PerformClick();(2)button1_Click(null,null);(3)button_Click(null,new EventArgs()) ...
- CSS 问题集锦
[1]让DIV中的内容居中 1.文字垂直居中,关键代码:height:100px;line-height:100px(两个值要相等) <div style="margin:0 auto ...
- poj-1314 Finding Rectangles
题目地址: http://poj.org/problem?id=1314 题意: 给出一串的点,有些点可以构成正方形,请按照字符排序输出. 因为这道题的用处很大, 最近接触的cv 中的Rectangl ...