题目链接

传送门

思路

这个题在\(BZOJ\)上有个二维平面的版本(\(BZOJ2716\)天使玩偶),不过是权限题因此就不附带链接了,我也只是在算法进阶指南上看到过,那个题的写法是\(CDQ\),然后比赛开始半个小时我就开始写\(CDQ\),\(T\)了之后冷静分析发现复杂度我少算了个\(log\),\(CDQ\)写这题的复杂度是\(nlog^3(n)\),然后就没思路了。

赛后看\(qls\)说用三维\(bit\)可以过后试了一下\(T\)了,然后发现\(qls\)的代码当时跑了\(937ms\)应该是卡过去的,感觉是\(vector\)常数大,但是想不出除\(vector\)的其他动态开全局数组大小的方法,最后疯狂交在\(T\)了一页后成功卡过去了,下面贴一下三维\(bit\)的代码和\(CDQ\)写法(对拍了很久貌似没问题,就当过了吧~)的代码。

这两份代码的主体思路就是拆绝对值符号,然后查询时(拿\(x_1\leq x2,y_1\leq y2,z_1\leq z_2\)举例,下标为\(2\)的是查询的点)\(|x_2-x_1|+|y_2-y_1|+|z_2-z_1|=(x_2+y_2+z_2)-(x_1+y_1+z_1)\),然后就是要查询\(x_1+y_1+z_1\)的最大值用树状数组维护即可,其他情况分别移动坐标即可。

update:发现题解里面贴的那份代码的三维\(bit\)写法贼牛逼,贴的代码就改成看了那个队的代码之后写的了。

代码

  1. //三维bit
  2. #include <set>
  3. #include <map>
  4. #include <deque>
  5. #include <queue>
  6. #include <stack>
  7. #include <cmath>
  8. #include <ctime>
  9. #include <bitset>
  10. #include <cstdio>
  11. #include <string>
  12. #include <vector>
  13. #include <cassert>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <iostream>
  17. #include <algorithm>
  18. using namespace std;
  19. typedef long long LL;
  20. typedef pair<LL, LL> pLL;
  21. typedef pair<LL, int> pLi;
  22. typedef pair<int, LL> pil;;
  23. typedef pair<int, int> pii;
  24. typedef unsigned long long uLL;
  25. #define lson rt<<1
  26. #define rson rt<<1|1
  27. #define lowbit(x) x&(-x)
  28. #define name2str(name) (#name)
  29. #define bug printf("*********\n")
  30. #define debug(x) cout<<#x"=["<<x<<"]" <<endl
  31. #define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
  32. #define IO ios::sync_with_stdio(false),cin.tie(0)
  33. const double eps = 1e-8;
  34. const int mod = 1000000007;
  35. const int maxn = 800000 + 7;
  36. const double pi = acos(-1);
  37. const int inf = 0x3f3f3f3f;
  38. const LL INF = 0x3f3f3f3f3f3f3f3fLL;
  39. int n, m, h, q, op, x, y, z;
  40. struct BIT {
  41. int a[maxn];
  42. int gethash(int x, int y, int z) {
  43. return x * h * m + y * h + z;
  44. }
  45. void init() {
  46. for(int i = 0; i <= n; ++i) {
  47. for(int j = 0; j <= m; ++j) {
  48. for(int k = 0; k <= h; ++k) {
  49. a[gethash(i, j, k)] = 0;
  50. }
  51. }
  52. }
  53. }
  54. void update(int x, int y, int z) {
  55. for(int i = x; i <= n; i += lowbit(i)) {
  56. for(int j = y; j <= m; j += lowbit(j)) {
  57. for(int k = z; k <= h; k += lowbit(k)) {
  58. int num = gethash(i, j, k);
  59. a[num] = max(a[num], x + y + z);
  60. }
  61. }
  62. }
  63. }
  64. int query(int x, int y, int z) {
  65. int ans = 0;
  66. for(int i = x; i > 0; i -= lowbit(i)) {
  67. for(int j = y; j > 0; j -= lowbit(j)) {
  68. for(int k = z; k > 0; k -= lowbit(k)) {
  69. ans = max(ans, a[gethash(i, j, k)]);
  70. }
  71. }
  72. }
  73. if(ans == 0) return inf;
  74. return x + y + z - ans;
  75. }
  76. }bit[8];
  77. int main() {
  78. scanf("%d%d%d%d", &n, &m, &h, &q);
  79. n += 1, m += 1, h += 1;
  80. for(int i = 0; i < 8; ++i) bit[i].init();
  81. for(int i = 1; i <= q; ++i) {
  82. scanf("%d%d%d%d", &op, &x, &y, &z);
  83. if(op == 1) {
  84. bit[1].update(x, y, z);
  85. bit[2].update(x, y, h - z);
  86. bit[3].update(x, m - y, z);
  87. bit[4].update(n - x, y, z);
  88. bit[5].update(n - x, m - y, z);
  89. bit[6].update(n - x, y, h - z);
  90. bit[7].update(x, m - y, h - z);
  91. bit[0].update(n - x, m - y, h - z);
  92. } else {
  93. int ans = inf;
  94. ans = min(ans, bit[1].query(x, y, z));
  95. ans = min(ans, bit[2].query(x, y, h - z));
  96. ans = min(ans, bit[3].query(x, m - y, z));
  97. ans = min(ans, bit[4].query(n - x, y, z));
  98. ans = min(ans, bit[5].query(n - x, m - y, z));
  99. ans = min(ans, bit[6].query(n - x, y, h - z));
  100. ans = min(ans, bit[7].query(x, m - y, h - z));
  101. ans = min(ans, bit[0].query(n - x, m - y, h - z));
  102. printf("%d\n", ans);
  103. }
  104. }
  105. return 0;
  106. }
  1. //CDQ
  2. #include <set>
  3. #include <map>
  4. #include <deque>
  5. #include <queue>
  6. #include <stack>
  7. #include <cmath>
  8. #include <ctime>
  9. #include <bitset>
  10. #include <cstdio>
  11. #include <string>
  12. #include <vector>
  13. #include <cassert>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <iostream>
  17. #include <algorithm>
  18. using namespace std;
  19. typedef long long LL;
  20. typedef pair<LL, LL> pLL;
  21. typedef pair<LL, int> pLi;
  22. typedef pair<int, LL> pil;;
  23. typedef pair<int, int> pii;
  24. typedef unsigned long long uLL;
  25. #define lson rt<<1
  26. #define rson rt<<1|1
  27. #define lowbit(x) x&(-x)
  28. #define name2str(name) (#name)
  29. #define bug printf("*********\n")
  30. #define debug(x) cout<<#x"=["<<x<<"]" <<endl
  31. #define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
  32. #define IO ios::sync_with_stdio(false),cin.tie(0)
  33. const double eps = 1e-8;
  34. const int mod = 1000000007;
  35. const int maxn = 100001;
  36. const double pi = acos(-1);
  37. const int inf = 0x3f3f3f3f;
  38. const LL INF = 0x3f3f3f3f3f3f3f3fLL;
  39. int n, m, h, q, op, x, y, z;
  40. int tree[maxn], ans[maxn];
  41. struct node {
  42. int id, x, y, z, op, val, flag;
  43. bool operator < (const node& a) const {
  44. return id < a.id;
  45. }
  46. }a[maxn], tmp[maxn], pp[maxn];
  47. void add(int x, int val) {
  48. while(x <= h) {
  49. tree[x] = max(tree[x], val);
  50. x += lowbit(x);
  51. }
  52. }
  53. int query(int x) {
  54. int ans = 0;
  55. while(x) {
  56. ans = max(ans, tree[x]);
  57. x -= lowbit(x);
  58. }
  59. if(ans == 0) ans = -inf;
  60. return ans;
  61. }
  62. void del(int x) {
  63. while(x <= h + 1) {
  64. tree[x] = 0;
  65. x += lowbit(x);
  66. }
  67. }
  68. void cdq2(int l, int r) {
  69. if(l == r) return;
  70. int mid = (l + r) >> 1;
  71. cdq2(l, mid), cdq2(mid + 1, r);
  72. int ls = l, rs = mid + 1, cnt = l;
  73. for(; rs <= r; ++rs) {
  74. for(; ls <= mid && tmp[ls].y <= tmp[rs].y; ++ls) {
  75. if(tmp[ls].op == 1 && tmp[ls].flag) {
  76. add(tmp[ls].z, tmp[ls].val);
  77. }
  78. }
  79. if(!tmp[rs].flag && tmp[rs].op == 2) ans[tmp[rs].id] = min(ans[tmp[rs].id], tmp[rs].val - query(tmp[rs].z));
  80. }
  81. --ls;
  82. while(ls >= l) {
  83. if(tmp[ls].op == 1 && tmp[ls].flag) {
  84. del(tmp[ls].z);
  85. }
  86. --ls;
  87. }
  88. ls = l, rs = mid + 1;
  89. while(ls <= mid && rs <= r) {
  90. if(tmp[ls].y < tmp[rs].y) {
  91. pp[cnt++] = tmp[ls++];
  92. } else if(tmp[ls].y > tmp[rs].y) {
  93. pp[cnt++] = tmp[rs++];
  94. } else if(tmp[ls].z <= tmp[rs].z) {
  95. pp[cnt++] = tmp[ls++];
  96. } else {
  97. pp[cnt++] = tmp[rs++];
  98. }
  99. }
  100. while(ls <= mid) pp[cnt++] = tmp[ls++];
  101. while(rs <= r) pp[cnt++] = tmp[rs++];
  102. for(int i = l; i <= r; ++i) tmp[i] = pp[i];
  103. }
  104. void cdq1(int l, int r) {
  105. if(l == r) return;
  106. int mid = (l + r) >> 1;
  107. cdq1(l, mid), cdq1(mid + 1, r);
  108. int ls = l, rs = mid + 1, cnt = l;
  109. while(ls <= mid && rs <= r) {
  110. if(a[ls].x < a[rs].x) {
  111. tmp[cnt] = a[ls++];
  112. tmp[cnt++].flag = 1;
  113. } else if(a[ls].x > a[rs].x) {
  114. tmp[cnt] = a[rs++];
  115. tmp[cnt++].flag = 0;
  116. } else if(a[ls].y < a[rs].y) {
  117. tmp[cnt] = a[ls++];
  118. tmp[cnt++].flag = 1;
  119. } else if(a[ls].y > a[rs].y) {
  120. tmp[cnt] = a[rs++];
  121. tmp[cnt++].flag = 0;
  122. } else if(a[ls].z <= a[rs].z) {
  123. tmp[cnt] = a[ls++];
  124. tmp[cnt++].flag = 1;
  125. } else {
  126. tmp[cnt] = a[rs++];
  127. tmp[cnt++].flag = 0;
  128. }
  129. }
  130. while(ls <= mid) {
  131. tmp[cnt] = a[ls++];
  132. tmp[cnt++].flag = 1;
  133. }
  134. while(rs <= r) {
  135. tmp[cnt] = a[rs++];
  136. tmp[cnt++].flag = 0;
  137. }
  138. for(int i = l; i <= r; ++i) a[i] = tmp[i];
  139. cdq2(l, r);
  140. }
  141. int main() {
  142. #ifndef ONLINE_JUDGE
  143. FIN;
  144. #endif
  145. scanf("%d%d%d%d", &n, &m, &h, &q);
  146. n += 1, m += 1, h += 1;
  147. for(int i = 1; i <= q; ++i) {
  148. scanf("%d%d%d%d", &op, &x, &y, &z);
  149. a[i] = {i, x, y, z, op, 0, 0};
  150. }
  151. //x,y,z
  152. for(int i = 1; i <= q; ++i) {
  153. ans[i] = inf;
  154. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  155. }
  156. cdq1(1, q);
  157. //x,y,-z
  158. for(int i = 1; i <= q; ++i) {
  159. a[i].z = h - a[i].z;
  160. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  161. }
  162. sort(a + 1, a + q + 1);
  163. cdq1(1, q);
  164. //x,-y,z
  165. for(int i = 1; i <= q; ++i) {
  166. a[i].z = h - a[i].z;
  167. a[i].y = m - a[i].y;
  168. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  169. }
  170. sort(a + 1, a + q + 1);
  171. cdq1(1, q);
  172. //-x,y,z
  173. for(int i = 1; i <= q; ++i) {
  174. a[i].y = m - a[i].y;
  175. a[i].x = n - a[i].x;
  176. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  177. }
  178. sort(a + 1, a + q + 1);
  179. cdq1(1, q);
  180. //-x,-y,z
  181. for(int i = 1; i <= q; ++i) {
  182. a[i].y = m - a[i].y;
  183. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  184. }
  185. sort(a + 1, a + q + 1);
  186. cdq1(1, q);
  187. //-x,y,-z
  188. for(int i = 1; i <= q; ++i) {
  189. a[i].y = m - a[i].y;
  190. a[i].z = h - a[i].z;
  191. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  192. }
  193. sort(a + 1, a + q + 1);
  194. cdq1(1, q);
  195. //x,-y,-z
  196. for(int i = 1; i <= q; ++i) {
  197. a[i].x = n - a[i].x;
  198. a[i].y = m - a[i].y;
  199. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  200. }
  201. sort(a + 1, a + q + 1);
  202. cdq1(1, q);
  203. //-x,-y,-z
  204. for(int i = 1; i <= q; ++i) {
  205. a[i].x = n - a[i].x;
  206. a[i].val = a[i].x + a[i].y + a[i].z, a[i].flag = 0;
  207. }
  208. sort(a + 1, a + q + 1);
  209. cdq1(1, q);
  210. sort(a + 1, a + q + 1);
  211. for(int i = 1; i <= q; ++i) {
  212. if(a[i].op == 2) {
  213. printf("%d\n", ans[i]);
  214. }
  215. }
  216. return 0;
  217. }

Distance(2019年牛客多校第八场D题+CDQ+树状数组)的更多相关文章

  1. Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)

    题目链接 传送门 题意 给你一张无向图,每条边\(u_i,v_i\)的权值范围为\([L_i,R_i]\),要经过这条边的条件是你的容量要在\([L_i,R_i]\),现在问你你有多少种容量使得你可以 ...

  2. 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟

    Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...

  3. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  4. 2020牛客多校第八场K题

    __int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...

  5. 2019年牛客多校第四场 B题xor(线段树+线性基交)

    题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...

  6. Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...

  7. generator 1(2019年牛客多校第五场B题+十进制矩阵快速幂)

    目录 题目链接 思路 代码 题目链接 传送门 思路 十进制矩阵快速幂. 代码 #include <set> #include <map> #include <deque& ...

  8. Find the median(2019年牛客多校第七场E题+左闭右开线段树)

    题目链接 传送门 题意 每次往集合里面添加一段连续区间的数,然后询问当前集合内的中位数. 思路 思路很好想,但是卡内存. 当时写的动态开点线段树没卡过去,赛后机房大佬用动态开点过了,\(tql\). ...

  9. 2019年牛客多校第三场 F题Planting Trees(单调队列)

    题目链接 传送门 题意 给你一个\(n\times n\)的矩形,要你求出一个面积最大的矩形使得这个矩形内的最大值减最小值小于等于\(M\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...

随机推荐

  1. GameZ游戏排名系统

    GameZ游戏排名系统 GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...

  2. JVM常用命令和性能调优建议 [Could not create the Java virtual machine]

    一.查看jvm常用命令jinfo:可以输出并修改运行时的java 进程的opts. jps:与unix上的ps类似,用来显示本地的java进程,可以查看本地运行着几个java程序,并显示他们的进程号. ...

  3. Win10 系统直接在目录下打开cmd

    每次用cmd命令,就要定位到当前文件夹,很麻烦 这里介绍一种直接定位到要操作的文件夹的方法: 操作步骤: (1)选择要cmd的文件夹,按住Shift键,鼠标右键快捷方式,先打开Powershell窗口 ...

  4. 62 网络编程(三)——UDP编程

    UDP编程标准步骤 服务器端 使用DatagramSocket创建服务端:DatagramSocket server = new DatagramSocket(port);//参数为自定义端口号 准备 ...

  5. TCMalloc - 基本流程

    SizeMap tcmalloc通过classid将不同的小对象映射到不同的对象桶中,sizemap记录了一些对象大小和对象class的映射以及反向映射,除此之外,还记录了一些ThreadCache与 ...

  6. 一个简单便捷的树形显示Ztree

    这是本人在闲时研究的一个用于显示树形列表的小玩意. zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. 下面就说说怎么用吧 ...

  7. 关于使用mySqlSugar插入数据异常解决方案

    项目的解决方案中引用的有mysqlsugar的数据库操作库,在使用插入数据过程中一些特殊的生僻字或表情符号总会提示: Incorrect string value: '\xF0\x9F...' for ...

  8. Git 管理版本/回退

    参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/896954074659008 Git status命令可以让我们时刻掌握仓库当前的状态,比 ...

  9. English--虚拟语气和条件状语从句

    English|虚拟语气和条件状语从句 虚拟语气在英语中,还是有一定地位的,毕竟大家都做着我有一百万的梦~~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力求不含任 ...

  10. asp.net 自定义特性

    今天看张子阳的.net中的反射(反射特性)一文,觉得反射配合自定义的特性确实还挺有用,之前看书.看博客之后好多心血来潮敲的代码随便往桌面上一放,时间一久,连自己也分不清它们是干嘛的了,然后就是删除,虽 ...