2648: SJY摆棋子

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

分析:   

  k-d tree 模板题。

代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
const int DIM = ;
const int INF = 1e9; #define lc T[rt].ch[0]
#define rc T[rt].ch[1] struct Point {
int x[];
Point() {x[] = ,x[] = ;}
Point(int a,int b) {x[] = a,x[] = b;}
}P[N];
struct KD_Tree {
int ch[],mn[],mx[];
Point p;
}T[N<<];
int Cur,CntNode,Ans,Root; inline bool cmp(Point a,Point b) {
return a.x[Cur] < b.x[Cur];
}
inline void pushup(int rt) {
for (int i=; i<DIM; ++i) {
if (lc) T[rt].mn[i] = min(T[rt].mn[i],T[lc].mn[i]), T[rt].mx[i] = max(T[rt].mx[i],T[lc].mx[i]);
if (rc) T[rt].mn[i] = min(T[rt].mn[i],T[rc].mn[i]), T[rt].mx[i] = max(T[rt].mx[i],T[rc].mx[i]); // -- rc - lc
}
}
inline void NewNode(int rt,Point p) {
T[rt].p = p;
T[rt].mn[] = T[rt].mx[] = p.x[];
T[rt].mn[] = T[rt].mx[] = p.x[];
}
int build(int l,int r,int cd) { // cd -- cur dimensions
if (l > r) return ;
int m = (l + r) >> ,rt = m;
Cur = cd; nth_element(P+l, P+m, P+r+, cmp);
NewNode(rt,P[m]);
lc = build(l, m-, cd^);
rc = build(m+, r, cd^);
pushup(rt);
return rt;
}
void Insert(Point p,int &rt,int cd) {
if (!rt) {
NewNode(rt = ++CntNode,p);return ;
}
if (p.x[cd] <= T[rt].p.x[cd]) Insert(p, lc, cd^);
else Insert(p, rc, cd^);
pushup(rt);
}
inline int dist(Point a,KD_Tree b) {
int dis = ;
if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
return dis;
}
inline int dis(Point a,Point b) {
return abs(a.x[] - b.x[]) + abs(a.x[] - b.x[]);
}
void query(Point p,int rt) {
Ans = min(Ans,dis(p,T[rt].p)); // -- !!
int dl = lc ? dist(p,T[lc]) : INF;
int dr = rc ? dist(p,T[rc]) : INF;
if (dl < dr) {
if (dl < Ans) query(p,lc);
if (dr < Ans) query(p,rc);
}
else {
if (dr < Ans) query(p,rc);
if (dl < Ans) query(p,lc);
}
}
int main() {
int n = read(),m = read();
CntNode = n; // --- !!!
for (int i=; i<=n; ++i)
P[i].x[] = read(),P[i].x[] = read();
Root = build(,n,);
while (m--) {
int opt = read(),x = read(),y = read();
if (opt == ) Insert(Point(x,y),Root,);
else {
Ans = INF;
query(Point(x,y),Root);
printf("%d\n",Ans);
}
}
return ;
}

luogu上需要拍扁重构,否则会T,而bzoj上拍扁重构却不如不拍扁重构跑得快

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
const int DIM = ;
const int INF = 1e9; #define alpha 0.75
#define lc T[rt].ch[0]
#define rc T[rt].ch[1] struct Point {
int x[];
Point() {x[] = ,x[] = ;}
Point(int a,int b) {x[] = a,x[] = b;}
}P[N<<];
struct KD_Tree {
int ch[],mn[],mx[],sz;
Point p;
}T[N<<];
int sk[N],Top,Cur,CntNode,Ans,Root; inline bool cmp(Point a,Point b) {
return a.x[Cur] < b.x[Cur];
}
inline void pushup(int rt) {
T[rt].sz = T[lc].sz + T[rc].sz + ; //-- +1!!!
for (int i=; i<DIM; ++i) {
if (lc) T[rt].mn[i] = min(T[rt].mn[i],T[lc].mn[i]), T[rt].mx[i] = max(T[rt].mx[i],T[lc].mx[i]);
if (rc) T[rt].mn[i] = min(T[rt].mn[i],T[rc].mn[i]), T[rt].mx[i] = max(T[rt].mx[i],T[rc].mx[i]);
}
}
inline void NewNode(int &rt,Point p) {
rt = Top ? sk[Top--] : ++CntNode;
T[rt].p = p;
T[rt].sz = ; // --!!!
T[rt].mn[] = T[rt].mx[] = p.x[];
T[rt].mn[] = T[rt].mx[] = p.x[];
}
int build(int l,int r,int cd) { // cd -- cur dimensions
if (l > r) return ;
int m = (l + r) >> ,rt;
Cur = cd; nth_element(P+l, P+m, P+r+, cmp);
NewNode(rt,P[m]);
lc = build(l, m-, cd^);
rc = build(m+, r, cd^);
pushup(rt);
return rt;
}
void dfs(int rt,int num) {
if (lc) dfs(lc, num);
P[num+T[lc].sz+] = T[rt].p, sk[++Top] = rt;
if (rc) dfs(rc, num+T[lc].sz+);
}
inline void check(int &rt,int cd) {
if (alpha * T[rt].sz < T[lc].sz || alpha * T[rt].sz < T[rc].sz) {
dfs(rt, );
rt = build(, T[rt].sz, cd);
}
}
void Insert(Point p,int &rt,int cd) {
if (!rt) {
NewNode(rt,p);return ;
}
if (p.x[cd] <= T[rt].p.x[cd]) Insert(p, lc, cd^);
else Insert(p, rc, cd^);
pushup(rt);check(rt,cd);
}
inline int dist(Point a,KD_Tree b) {
int dis = ;
if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
return dis;
}
inline int dis(Point a,Point b) {
return abs(a.x[] - b.x[]) + abs(a.x[] - b.x[]);
}
void query(Point p,int rt) {
Ans = min(Ans,dis(p,T[rt].p)); // -- !!
int dl = lc ? dist(p,T[lc]) : INF;
int dr = rc ? dist(p,T[rc]) : INF;
if (dl < dr) {
if (dl < Ans) query(p,lc);
if (dr < Ans) query(p,rc);
}
else {
if (dr < Ans) query(p,rc);
if (dl < Ans) query(p,lc);
}
}
int main() {
int n = read(),m = read();
for (int i=; i<=n; ++i)
P[i].x[] = read(),P[i].x[] = read();
Root = build(,n,);
while (m--) {
int opt = read(),x = read(),y = read();
if (opt == ) Insert(Point(x,y),Root,);
else {
Ans = INF;
query(Point(x,y),Root);
printf("%d\n",Ans);
}
}
return ;
}

2648: SJY摆棋子的更多相关文章

  1. BZOJ 2648: SJY摆棋子

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

  2. BZOJ 2648: SJY摆棋子 kdtree

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

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

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

  4. 【BZOJ】2648: SJY摆棋子 & 2716: [Violet 3]天使玩偶(kdtree)

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

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

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

  6. bzoj 2648 SJY摆棋子 kd树

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

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

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

  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 ...

随机推荐

  1. Full scan vs index 执行计划的实验

    根据Oracle-L邮件列表里主题「 Full scan vs index 」的讨论而来. 1.测试环境创建 SYS@HEMESRHTDB2(1.206)> select * from v$ve ...

  2. Python:函数的命名空间、作用域与闭合函数

    1,参数陷阱 如果默认参数的只是一个可变数据类型,那么每一次调用的时候,如果不传值就共用这个数据类型的资源. 2,三元运算 c=a if a>b else b#如果a>b返回a,否则,返回 ...

  3. java实现按拼音排序

    List<WaPayFileVO> list =(List<WaPayFileVO>) dao.execQueryBeanList(pagesql, params.toArra ...

  4. 【洛谷P2447】[SDOI2010]外星千足虫

    外星千足虫 题目链接 首先,它一看题解就是个高斯消元 只是改成了奇偶性的判断 性质: 奇数+奇数=偶数 奇数+偶数=奇数 偶数+奇数=奇数 偶数+偶数=偶数 于是我们用1表示奇数,0表示偶数 1+1= ...

  5. CSS 中定位方式有几种,说明他们的意义

    1.static  默认定位方式 显示为没有设置定位时的位置 2.fixed(固定定位)  他所相对固定的对象是可视窗口,与其他无关.以浏览器窗口作为参考进行定位 3.relative(相对定位) 元 ...

  6. JSON Web Tokens介绍

    转载请标明出处: http://blog.csdn.net/forezp/article/details/72804324 本文出自方志朋的博客 ##什么是JWT 这篇文章选择性翻译于https:// ...

  7. Django-rest-framework(二)serializers 使用

    简介 初次见到serializers文件,想必大家都会感到陌生,所以,我们不妨换个词来形容他的作用,那就是django 中的Form,这样是不是感觉熟悉了一点. 实际上,serializers 的作用 ...

  8. Spring Boot应用的测试——Mockito

    Spring Boot应用的测试——Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring ...

  9. 我的第一个Python程序

    #我的第一个Python程序 def is_valid_date(strdate): #判断是否是一个有效的日期字符串 try: #判断是否是时间格式 if ":" in strd ...

  10. NPM 常见错误

    找不到兼容版本 你有一个过时的npm.请更新到最新稳定的npm. 权限错误 npm ERR! code EPERM npm ERR! code EACCES 修复缓存的权限sudo chown -R ...