2648: SJY摆棋子

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

分析:   

  k-d tree 模板题。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4.  
  5. inline int read() {
  6. int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
  7. for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
  8. }
  9.  
  10. const int N = ;
  11. const int DIM = ;
  12. const int INF = 1e9;
  13.  
  14. #define lc T[rt].ch[0]
  15. #define rc T[rt].ch[1]
  16.  
  17. struct Point {
  18. int x[];
  19. Point() {x[] = ,x[] = ;}
  20. Point(int a,int b) {x[] = a,x[] = b;}
  21. }P[N];
  22. struct KD_Tree {
  23. int ch[],mn[],mx[];
  24. Point p;
  25. }T[N<<];
  26. int Cur,CntNode,Ans,Root;
  27.  
  28. inline bool cmp(Point a,Point b) {
  29. return a.x[Cur] < b.x[Cur];
  30. }
  31. inline void pushup(int rt) {
  32. for (int i=; i<DIM; ++i) {
  33. 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]);
  34. 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
  35. }
  36. }
  37. inline void NewNode(int rt,Point p) {
  38. T[rt].p = p;
  39. T[rt].mn[] = T[rt].mx[] = p.x[];
  40. T[rt].mn[] = T[rt].mx[] = p.x[];
  41. }
  42. int build(int l,int r,int cd) { // cd -- cur dimensions
  43. if (l > r) return ;
  44. int m = (l + r) >> ,rt = m;
  45. Cur = cd; nth_element(P+l, P+m, P+r+, cmp);
  46. NewNode(rt,P[m]);
  47. lc = build(l, m-, cd^);
  48. rc = build(m+, r, cd^);
  49. pushup(rt);
  50. return rt;
  51. }
  52. void Insert(Point p,int &rt,int cd) {
  53. if (!rt) {
  54. NewNode(rt = ++CntNode,p);return ;
  55. }
  56. if (p.x[cd] <= T[rt].p.x[cd]) Insert(p, lc, cd^);
  57. else Insert(p, rc, cd^);
  58. pushup(rt);
  59. }
  60. inline int dist(Point a,KD_Tree b) {
  61. int dis = ;
  62. if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
  63. if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
  64. if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
  65. if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
  66. return dis;
  67. }
  68. inline int dis(Point a,Point b) {
  69. return abs(a.x[] - b.x[]) + abs(a.x[] - b.x[]);
  70. }
  71. void query(Point p,int rt) {
  72. Ans = min(Ans,dis(p,T[rt].p)); // -- !!
  73. int dl = lc ? dist(p,T[lc]) : INF;
  74. int dr = rc ? dist(p,T[rc]) : INF;
  75. if (dl < dr) {
  76. if (dl < Ans) query(p,lc);
  77. if (dr < Ans) query(p,rc);
  78. }
  79. else {
  80. if (dr < Ans) query(p,rc);
  81. if (dl < Ans) query(p,lc);
  82. }
  83. }
  84. int main() {
  85. int n = read(),m = read();
  86. CntNode = n; // --- !!!
  87. for (int i=; i<=n; ++i)
  88. P[i].x[] = read(),P[i].x[] = read();
  89. Root = build(,n,);
  90. while (m--) {
  91. int opt = read(),x = read(),y = read();
  92. if (opt == ) Insert(Point(x,y),Root,);
  93. else {
  94. Ans = INF;
  95. query(Point(x,y),Root);
  96. printf("%d\n",Ans);
  97. }
  98. }
  99. return ;
  100. }

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4.  
  5. inline int read() {
  6. int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
  7. for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
  8. }
  9.  
  10. const int N = ;
  11. const int DIM = ;
  12. const int INF = 1e9;
  13.  
  14. #define alpha 0.75
  15. #define lc T[rt].ch[0]
  16. #define rc T[rt].ch[1]
  17.  
  18. struct Point {
  19. int x[];
  20. Point() {x[] = ,x[] = ;}
  21. Point(int a,int b) {x[] = a,x[] = b;}
  22. }P[N<<];
  23. struct KD_Tree {
  24. int ch[],mn[],mx[],sz;
  25. Point p;
  26. }T[N<<];
  27. int sk[N],Top,Cur,CntNode,Ans,Root;
  28.  
  29. inline bool cmp(Point a,Point b) {
  30. return a.x[Cur] < b.x[Cur];
  31. }
  32. inline void pushup(int rt) {
  33. T[rt].sz = T[lc].sz + T[rc].sz + ; //-- +1!!!
  34. for (int i=; i<DIM; ++i) {
  35. 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]);
  36. 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]);
  37. }
  38. }
  39. inline void NewNode(int &rt,Point p) {
  40. rt = Top ? sk[Top--] : ++CntNode;
  41. T[rt].p = p;
  42. T[rt].sz = ; // --!!!
  43. T[rt].mn[] = T[rt].mx[] = p.x[];
  44. T[rt].mn[] = T[rt].mx[] = p.x[];
  45. }
  46. int build(int l,int r,int cd) { // cd -- cur dimensions
  47. if (l > r) return ;
  48. int m = (l + r) >> ,rt;
  49. Cur = cd; nth_element(P+l, P+m, P+r+, cmp);
  50. NewNode(rt,P[m]);
  51. lc = build(l, m-, cd^);
  52. rc = build(m+, r, cd^);
  53. pushup(rt);
  54. return rt;
  55. }
  56. void dfs(int rt,int num) {
  57. if (lc) dfs(lc, num);
  58. P[num+T[lc].sz+] = T[rt].p, sk[++Top] = rt;
  59. if (rc) dfs(rc, num+T[lc].sz+);
  60. }
  61. inline void check(int &rt,int cd) {
  62. if (alpha * T[rt].sz < T[lc].sz || alpha * T[rt].sz < T[rc].sz) {
  63. dfs(rt, );
  64. rt = build(, T[rt].sz, cd);
  65. }
  66. }
  67. void Insert(Point p,int &rt,int cd) {
  68. if (!rt) {
  69. NewNode(rt,p);return ;
  70. }
  71. if (p.x[cd] <= T[rt].p.x[cd]) Insert(p, lc, cd^);
  72. else Insert(p, rc, cd^);
  73. pushup(rt);check(rt,cd);
  74. }
  75. inline int dist(Point a,KD_Tree b) {
  76. int dis = ;
  77. if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
  78. if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
  79. if (a.x[] < b.mn[]) dis += b.mn[] - a.x[];
  80. if (a.x[] > b.mx[]) dis += a.x[] - b.mx[];
  81. return dis;
  82. }
  83. inline int dis(Point a,Point b) {
  84. return abs(a.x[] - b.x[]) + abs(a.x[] - b.x[]);
  85. }
  86. void query(Point p,int rt) {
  87. Ans = min(Ans,dis(p,T[rt].p)); // -- !!
  88. int dl = lc ? dist(p,T[lc]) : INF;
  89. int dr = rc ? dist(p,T[rc]) : INF;
  90. if (dl < dr) {
  91. if (dl < Ans) query(p,lc);
  92. if (dr < Ans) query(p,rc);
  93. }
  94. else {
  95. if (dr < Ans) query(p,rc);
  96. if (dl < Ans) query(p,lc);
  97. }
  98. }
  99. int main() {
  100. int n = read(),m = read();
  101. for (int i=; i<=n; ++i)
  102. P[i].x[] = read(),P[i].x[] = read();
  103. Root = build(,n,);
  104. while (m--) {
  105. int opt = read(),x = read(),y = read();
  106. if (opt == ) Insert(Point(x,y),Root,);
  107. else {
  108. Ans = INF;
  109. query(Point(x,y),Root);
  110. printf("%d\n",Ans);
  111. }
  112. }
  113. return ;
  114. }

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. 在VMware上安装ubuntu——网络类型

    安装虚拟机时,向导提示选择网络类型: 当使用仅主机模式网络时,虚拟机和物理机不能互相访问共享.

  2. CentOS如何部署TinyProxy

    TinyProxy是个非常便利,及容易架设的HTTP代理 安装方法 rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release- ...

  3. 从命令行运行Jmeter及jmeter参数说明、Html报告生成

    为什么要命令行执行脚本,主要有以下三点: 1) 图形化界面消耗更多资源,CPU和内存 2) 图形化界面不支持大型的负载测试和性能测试 3) 命令行测试支持持续集成,例如放到Jenkins这样的CI工具 ...

  4. Hibernate的属性配置

    Hibernate配置属性 hibernate.dialect Hibernate方言(Dialect)的类名 - 可以让Hibernate使用某些特定的数据库平台的特性 取值. full.class ...

  5. Windows下同时安装Anaconda2与Anaconda3

    1. 安装一个作为主版本,比如先安装Anaconda2,安装时选择[添加path环境变量].我的安装地址为:E:\ProgramData\Anaconda3 2. 安装另一个版本python,安装时注 ...

  6. HDU 2859 Phalanx(对称矩阵 经典dp样例)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2859 Phalanx Time Limit: 10000/5000 MS (Java/Others)  ...

  7. For macOS.百度网盘 破解SVIP、下载速度限制~

    For macOS.百度网盘 破解SVIP.下载速度限制~ 是插件的 https://github.com/CodeTips/BaiduNetdiskPlugin-macOS 2019-01-03 让 ...

  8. cent os 上安装 matlab

    下载和安装可以参考,这个链接: https://lanseyujie.com/post/matlab-download-and-activate.html 上面这链接,在创建桌面快捷键时,未能创建,c ...

  9. 虚拟内存设置(解决linux内存不够情况)

    一.      虚拟内存介绍 背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的话,会有非常快的读写速度.但是,内存的造价是要高于 ...

  10. let's encrypt部署免费泛域名证书

    环境说明 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@localhos ...