思路:首先我们将问题转换一下,变成问在某个点左下角的权值和,那么每一个询问可以拆成4的这样的询问,然后

进行CDQ 分治,回溯的时候按x轴排序,然后用树状数组维护y的值。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define pii pair<int, int>
  7. #define x2 skdjflsdg
  8. #define y2 sdkfjsktge
  9.  
  10. using namespace std;
  11.  
  12. const int N = 2e6 + ;
  13. const int M = 1e6 + ;
  14. const int inf = 0x3f3f3f3f;
  15. const LL INF = 0x3f3f3f3f3f3f3f3f;
  16. const int mod = 1e9 +;
  17.  
  18. int s, w, cnt, tot, op;
  19.  
  20. LL ans[M], a[N];
  21. struct Qus {
  22. int x, y, idx, id;
  23. } qus[M], tmp[M];
  24.  
  25. void modify(int x, int v) {
  26. for(int i = x; i < N; i += i & -i) {
  27. a[i] += v;
  28. }
  29. }
  30.  
  31. LL sum(int x) {
  32. LL ans = ;
  33. for(int i = x; i; i -= i & -i) {
  34. ans += a[i];
  35. }
  36. return ans;
  37. }
  38.  
  39. void cdq(int l, int r) {
  40.  
  41. if(l == r) return;
  42. int mid = l + r >> ;
  43. cdq(l, mid); cdq(mid + , r);
  44.  
  45. int p = l, q = mid + , cnt = l;
  46.  
  47. for(int i = mid + ; i <= r; i++) {
  48. while(p <= mid && qus[p].x <= qus[i].x) {
  49. if(qus[p].idx == )
  50. modify(qus[p].y, qus[p].id);
  51. p++;
  52. }
  53. ans[qus[i].id] += qus[i].idx * sum(qus[i].y);
  54. }
  55.  
  56. for(int i = l; i < p; i++) {
  57. if(qus[i].idx == ) {
  58. modify(qus[i].y, -qus[i].id);
  59. }
  60. }
  61.  
  62. p = l, q = mid + , cnt = l;
  63.  
  64. while(p <= mid && q <= r) {
  65. if(qus[p].x <= qus[q].x) {
  66. tmp[cnt++] = qus[p++];
  67. } else {
  68. tmp[cnt++] = qus[q++];
  69. }
  70. }
  71.  
  72. while(p <= mid) tmp[cnt++] = qus[p++];
  73. while(q <= r) tmp[cnt++] = qus[q++];
  74.  
  75. for(int i = l; i <= r; i++) qus[i] = tmp[i];
  76.  
  77. }
  78.  
  79. int main() {
  80. scanf("%d%d", &s, &w);
  81. while(scanf("%d", &op) && op < ) {
  82. if(op == ) {
  83. int x, y, a; scanf("%d%d%d", &x, &y, &a);
  84. x += ; y += ;
  85. qus[++tot].x = x;
  86. qus[tot].y = y;
  87. qus[tot].id = a;
  88. qus[tot].idx = ;
  89. } else {
  90. int x1, y1, x2, y2;
  91. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  92. x1 += , y1 += ;
  93. x2 += , y2 += ;
  94. qus[++tot].x = x2;
  95. qus[tot].y = y2;
  96. qus[tot].id = ++cnt;
  97. qus[tot].idx = ;
  98.  
  99. qus[++tot].x = x2;
  100. qus[tot].y = y1 - ;
  101. qus[tot].id = cnt;
  102. qus[tot].idx = -;
  103.  
  104. qus[++tot].x = x1 - ;
  105. qus[tot].y = y2;
  106. qus[tot].id = cnt;
  107. qus[tot].idx = -;
  108.  
  109. qus[++tot].x = x1 - ;
  110. qus[tot].y = y1 - ;
  111. qus[tot].id = cnt;
  112. qus[tot].idx = ;
  113.  
  114. ans[cnt] = 1ll * (x2 - x1) * (y2 - y1) * s;
  115. }
  116. }
  117. cdq(, tot);
  118. for(int i = ; i <= cnt; i++)
  119. printf("%lld\n", ans[i]);
  120. return ;
  121. }
  122. /*
  123. */

bzoj 1176 CDQ分治的更多相关文章

  1. bzoj 1176 cdq分治套树状数组

    题面: 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. Inp ...

  2. BZOJ 1537 cdq分治

    思路: 我只是想写一下cdq-- 二维偏序 一维排序 一维cdq分治 (我忘了归并排序怎么写了,,,) 写了个sort- 复杂度是O(nlog^2n) //By SiriusRen #include ...

  3. BZOJ 3262 cdq分治 OR 树套树

    注意判断 三个条件都一样的-- (CDQ分治 其实并不是很难理解 只是想不到--) CDQ分治: //By SiriusRen #include <cstdio> #include < ...

  4. bzoj 2683 CDQ分治

    题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...

  5. BZOJ - 1935 / 1176 cdq分治 三维偏序

    题意:给定n*m的网格,且给出n个(x,y)表示该网格已被占有,q次询问(x1,y1)到(x2,y2)的网格中有多少个被占有,n,m范围1e7,q范围5e5 cdq按x轴排序,树状数组维护y轴 #in ...

  6. 【BZOJ4237】稻草人(CDQ分治,单调栈)

    [BZOJ4237]稻草人(CDQ分治,单调栈) 题面 BZOJ 题解 \(CDQ\)分治好题呀 假设固定一个左下角的点 那么,我们可以找到的右下角长什么样子??? 发现什么? 在右侧是一个单调递减的 ...

  7. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  8. BZOJ 1176 Mokia CDQ分治+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  9. 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...

随机推荐

  1. 怎么用spring cloud service-id 进行调用接口

    这里最关键的就是加上@LoadBalanced @SpringBootApplication public class ConsumerMovieApplication { @Bean @LoadBa ...

  2. [DeeplearningAI笔记]序列模型1.1-1.2序列模型及其数学符号定义

    5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1什么是序列模型 在进行语音识别时,给定了一个输入音频片段X,并要求输出片段对应的文字记录Y,这个例子中的输入和输出都输 ...

  3. [LeetCode] 28. Implement strStr() ☆

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. 【Android】完善Android学习(七:API 4.0.3)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  5. Debian最完美安装flash的教程//适用于所有linux版本

    话说不管是新手还是老手,都离不开flash.没有flash的支持,菜鸟们也少了一些把玩linux的动力. flash有很多安装的方法,不过性能相差很大.这里的缘由就不重要了. 下面我介绍在chromi ...

  6. 庞老师集群.ziw

    2017年2月17日, 星期五 庞老师集群 链接:http://pan.baidu.com/s/1mhSw2TE 密码:hzz4   更改子网IP,及网关:     null

  7. C++类四个默认函数&深复制&浅复制

    学习C++语言的同学都知道,C++中类是有默认的几个函数的,主要是有四个函数: 四个函数 默认构造函数:A(void),无参构造函数 拷贝(复制)构造函数:A(const A&a).用一个对象 ...

  8. PHP与Ajax

    如何用PHP接收JSON格式数据 1.一般来说,我们直接用$_POST $_REQUEST $_GET这样的超全局变量接收就好了 <?php $obj_temp=$_POST['data']; ...

  9. Java实现链式存储的二叉树

    二叉树的定义: 二叉树(BinaryTree)是n(n≥0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的.分别称作这个根的左子树和右子树的二叉树组成. 二叉树的遍历方式主要 ...

  10. Linux内核同步原语之原子操作【转】

    转自:http://blog.csdn.net/npy_lp/article/details/7262388 避免对同一数据的并发访问(通常由中断.对称多处理器.内核抢占等引起)称为同步. ——题记 ...