http://www.lydsy.com/JudgeOnline/problem.php?id=2716

http://www.lydsy.com/JudgeOnline/problem.php?id=2648

双倍经验题。。。

kdtree裸题吧。。。。。今天学了下kdtree。。。感觉挺简单的。。。。

其实就是对几何图形进行剖分建树,而特殊在于,x和y轴轮流剖。。。。这样可以提供很好的性质以便于查找。。。

(一开始脑补了个treap代替kdtree。。。。。显然我脑残了。。。。写到一半发现这是不能旋转的。。。。。

(然后又脑补了下。。。。这货如果和孙子(儿子的儿子)可以旋转。。。。。。。。因为这货的节点类似红黑树。。。但是蒟蒻我太弱从来没写过红黑树。。。

很好的资料(概念脑补):http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html

一些参考代码(虽然说完全没按照这样写):http://blog.csdn.net/jiangshibiao/article/details/34144829

说一下怎么查找。

首先这是曼哈顿距离。。和欧几里得距离不同。。。如果欧几里得那么简单多了。。

我们需要维护极值(即能包围所有子树包括自己的点的一个矩形的四个顶点):

如果要查找的点在矩形内,那么进入查找。

如果在矩形外,那么计算出查找点到矩形的曼哈顿距离,判断是否小于当前最优解,如果是,进入搜索。

(或许还能优化,因为一开始我的想法和这个不怎么一样。。我觉得。。递归进入两棵子树中其中一棵后(按x或y排序而不是找极值),然后判断最优解是否能从左子树的某个点越过当前根的分割线。。。。。如果是,就进入。。。

然后就没了。。。

留下的坑:欧几里得的没写过。。。删除操作没写过(觉对要斯巴达。。。。想写成平衡树(强迫症系列

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=2000005, oo=~0u>>1;
struct node *null;
struct node {
node *c[2];
int mx[2], mn[2], p[2];
void upd(node *x) {
if(x==null) return;
mx[0]=max(mx[0], x->mx[0]);
mx[1]=max(mx[1], x->mx[1]);
mn[0]=min(mn[0], x->mn[0]);
mn[1]=min(mn[1], x->mn[1]);
}
int getdis(node *x) {
int ret=0;
ret+=max(x->p[0]-mx[0], 0);
ret+=max(x->p[1]-mx[1], 0);
ret+=max(mn[0]-x->p[0], 0);
ret+=max(mn[1]-x->p[1], 0);
return ret;
}
void init(int x, int y) {
p[0]=mx[0]=mn[0]=x;
p[1]=mx[1]=mn[1]=y;
c[0]=c[1]=null;
}
int dis(node *x) { return abs(p[0]-x->p[0])+abs(p[1]-x->p[1]); }
}*root, T[N], *Tcnt=T;
node *newnode(int x=0, int y=0) { Tcnt->init(x, y); return Tcnt++; }
void init() { null=newnode(); null->c[0]=null->c[1]=null; root=null; }
void insert(node *&x, node *y, bool f) {
if(x==null) { x=y; return; }
bool d=x->p[f]<y->p[f];
x->upd(y);
insert(x->c[d], y, !f);
}
void ins(int x, int y) { insert(root, newnode(x, y), 0); }
void ask(node *x, node *y, int &ans) {
ans=min(ans, x->dis(y));
int d[2];
d[0]=x->c[0]==null?oo:x->c[0]->getdis(y);
d[1]=x->c[1]==null?oo:x->c[1]->getdis(y);
bool f=d[0]>d[1]; if(d[f]==oo) return;
ask(x->c[f], y, ans); if(d[!f]<ans) ask(x->c[!f], y, ans);
}
int ask(int x, int y) {
int ret=oo; static node r;
r.p[0]=x; r.p[1]=y;
ask(root, &r, ret);
return ret;
}
int main() {
init();
int n=getint(), m=getint();
rep(i, n) { int x=getint(), y=getint(); ins(x, y); }
while(m--) {
int t=getint(), x=getint(), y=getint();
if(t==2) printf("%d\n", ask(x, y));
else ins(x, y);
}
return 0;
}

  


Description

这天,SJY显得无聊。在家自己玩。在一个棋盘上,有N个黑色棋子。他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子。此处的距离是 曼哈顿距离 即(|x1-x2|+|y1-y2|) 。现在给出N<=500000个初始棋子。和M<=500000个操作。对于每个白色棋子,输出距离这个白色棋子最近的黑色棋子的距离。同一个格子可能有多个棋子。
 

Input

第一行两个数 N M
以后M行,每行3个数 t x y
如果t=1 那么放下一个黑色棋子
如果t=2 那么放下一个白色棋子

Output

对于每个T=2 输出一个最小距离
 

Sample Input

2 3
1 1
2 3
2 1 2
1 3 3
2 4 2

Sample Output

1
2

HINT

kdtree可以过

Source

【BZOJ】2648: SJY摆棋子 & 2716: [Violet 3]天使玩偶(kdtree)的更多相关文章

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

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

  2. BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶

    BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...

  3. bzoj2648SJY摆棋子&&bzoj2716[Violet 3]天使玩偶*

    bzoj2648SJY摆棋子 bzoj2716[Violet 3]天使玩偶 题意: 棋盘上有n个棋子,现在有m个操作,一种是加棋子,一种是查询离某个点最近的棋子.n,m≤500000. 题解: 先将已 ...

  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摆棋子(K-D Tree)

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

  7. bzoj 2648 SJY摆棋子 kd树

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

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

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

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

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

随机推荐

  1. DCMTK3.6.0(MD支持库)安装说明

    一.运行环境:WIN7 32bit + VisualStudio2008 + dcmtk3.6.0 + Cmake2.8.8 或者 WIN7 64bit 二.准备工作: 1)MD/MT的知识储备: / ...

  2. [ruby on rails] 跟我学之(3)基于rails console的查增删改操作

    本章节展开对model的介绍:包括查增删改操作.紧接着上面一节<[ruby on rails] 跟我学之HelloWorld> 创建模型 使用命令创建模型 创建表post,默认自带两栏位 ...

  3. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  4. maven项目 Java compiler level does not match the version of the installed Java project facet

    因工作的关系,Eclipse开发的Java项目拷来拷去,有时候会报一个很奇怪的错误.明明源码一模一样,为什么项目复制到另一台机器上,就会报“java compiler level does not m ...

  5. Linux shell脚本编程基础之练习篇

    shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...

  6. project.json

    概述 项目相关配置,由原来的cocos2d.js中转移到project.json中,该文件需要与index.html同级,一般建议放在根目录下. 字段说明 debugMode 相当于原来的COCOS2 ...

  7. 【python】捕获所有异常

    如下所示,在不知道异常名的情况下可以捕获所有异常 try: a=b b=c except Exception,ex: print Exception,":",ex

  8. Light OJ 1296 - Again Stone Game (博弈sg函数递推)

    F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. 有关PowerShell脚本你必须知道的十个基本概念

    1.PS1文件 一个PowerShell脚本其实就是一个简单的文本文件,这个文件包含了一系列PowerShell命令,每个命令显示为独立的一行,对于被视为PowerShell脚本的文本文件,它的文件名 ...

  10. 关于struts 2中的日期问题

    struts 2中引入了大量的jquery的内容 其中日期问题总结一下: 步骤: 1.当然不用说,先建一个web项目 2.导入struts2所需要的jar包,以及此插件的包(当然你也可以用:strut ...