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. NOIP水题合集[3/未完待续]

    NOIP2008pj传球游戏 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球, ...

  2. MipMap

    MipMap 首先从MIPMAP的原理说起,它是把一张贴图按照2的倍数进行缩小.直到1X1.把缩小的图都存储起来.在渲染时,根据一个像素离眼睛为之的距离,来判断从一个合适的图层中取出texel颜色赋值 ...

  3. u3d_shader_surface_shader_3

    参考http://my.oschina.net/u/138823/blog/181131 加了个凹凸贴图: 抱歉把女神苏菲做成这样. 一:Normal Texture的制作: 1.首先是Normal ...

  4. date时间函数

    时间函数: date();和time();的相互转换 time();   在PHP中单位是秒,在js中是毫秒. microtime();  毫秒 date('Y-m-d H:i:s',time()); ...

  5. jQuery实例

    1.$("ul li").fliter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')").addClas ...

  6. IIS Enabling HTTP Keep-Alives

    IIS 6.0 from:https://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/ea116535-8e ...

  7. Visual Studio 2013编辑HTML文件无设计视图的解决方案

    在Visual Studio 2013中编辑HTML文件,会发现没有设计视图. 解决方法:点击Visual Studio 2013的”工具“菜单,再点击”选项“—>文本编辑器—>文件扩展名 ...

  8. Post model至Web Api创建或是保存数据

    前一篇<Post model至Web Api>http://www.cnblogs.com/insus/p/4343538.html中,使用Post来从Web Api获取数据.由于Post ...

  9. 使用LocalBroadcastManager

    Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中BroadcasetReceiver的注 ...

  10. JS事件详解

    hello,我是沐晴,最近呢,来总结一下 JS中的常用的事件,希望我们都能一起查漏补缺. 焦点事件 焦点事件在表单中经常用到,那什么是焦点呢?比如我们文本框里面的有光标的时候,就是获得了焦点,我们就可 ...