1176: [Balkan2007]Mokia

Time Limit: 30 Sec  Memory Limit: 162 MB
Submit: 1854  Solved: 821
[Submit][Status][Discuss]

Description

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

Input

第一行两个整数,S,W;其中S为矩阵初始值;W为矩阵大小

接下来每行为一下三种输入之一(不包含引号):

"1 x y a"

"2 x1 y1 x2 y2"

"3"

输入1:你需要把(x,y)(第x行第y列)的格子权值增加a

输入2:你需要求出以左上角为(x1,y1),右下角为(x2,y2)的矩阵内所有格子的权值和,并输出

输入3:表示输入结束

Output

对于每个输入2,输出一行,即输入2的答案

Sample Input

0 4
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3

Sample Output

3
5

HINT

保证答案不会超过int范围

Solution  

CDQ分治+树状数组裸题,矩阵分成四个点统计,单点更新。

Code

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. const int maxn = ;
  11. const int maxm = +;
  12. int s, w, ans[maxm];
  13. struct Node
  14. {
  15. int op, id, x, y, k, to;
  16. Node (int op = , int id = , int x = , int y = , int k = , int to = ):
  17. op(op), id(id), x(x), y(y), k(k), to(to) {}
  18. bool operator < (const Node &AI) const
  19. {
  20. if (x == AI.x && y == AI.y)
  21. return op < AI.op;
  22. if (x == AI.x)
  23. return y < AI.y;
  24. return x < AI.x;
  25. }
  26. }q[maxm], temp[maxm];
  27. struct BIT
  28. {
  29. int c[maxn+];
  30. int lowbit(int x)
  31. {
  32. return x & -x;
  33. }
  34. void update(int x, int d)
  35. {
  36. while (x <= w)
  37. {
  38. c[x] += d;
  39. x += lowbit(x);
  40. }
  41. }
  42. int query(int x)
  43. {
  44. int ret = ;
  45. while (x > )
  46. {
  47. ret += c[x];
  48. x -= lowbit(x);
  49. }
  50. return ret;
  51. }
  52. }T;
  53.  
  54. void cdq(int l, int r)
  55. {
  56. if (l == r)
  57. return ;
  58. int mid = (l+r)>>;
  59. for (int i = l; i <= r; ++i)
  60. {
  61. if (q[i].id <= mid && q[i].op == )
  62. T.update(q[i].y, q[i].k);
  63. if (q[i].id > mid && q[i].op == )
  64. ans[q[i].to] += q[i].k*T.query(q[i].y);
  65. }
  66. for (int i = l; i <= r; ++i)
  67. if (q[i].id <= mid && q[i].op == )
  68. T.update(q[i].y, -q[i].k);
  69. int t1 = l-, t2 = mid;
  70. for (int i = l; i <= r; ++i)
  71. if (q[i].id <= mid)
  72. temp[++t1] = q[i];
  73. else
  74. temp[++t2] = q[i];
  75. for (int i = l; i <= r; ++i)
  76. q[i] = temp[i];
  77. cdq(l, mid), cdq(mid+, r);
  78. }
  79.  
  80. int main()
  81. {
  82. scanf("%d %d", &s, &w);
  83. w += ;
  84. int oper, cnt = , cnt_2 = ;
  85. while (~scanf("%d", &oper) && oper != )
  86. {
  87. if (oper == )
  88. {
  89. int x, y, d;
  90. scanf("%d %d %d", &x, &y, &d);
  91. x += , y += ;
  92. q[++cnt] = Node(, cnt, x, y, d, );
  93. }
  94. else
  95. {
  96. int x1, y1, x2, y2;
  97. scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  98. x1 += , y1 += , x2 += , y2 += ;
  99. cnt_2 ++;
  100. ans[cnt_2] = s*(x2-x1+)*(y2-y1+);
  101. q[++cnt] = Node(, cnt, x2, y2, , cnt_2);
  102. q[++cnt] = Node(, cnt, x1-, y2, -, cnt_2);
  103. q[++cnt] = Node(, cnt, x2, y1-, -, cnt_2);
  104. q[++cnt] = Node(, cnt, x1-, y1-, , cnt_2);
  105. }
  106. }
  107. sort(q+, q+cnt+);
  108. cdq(, cnt);
  109. for (int i = ; i <= cnt_2; ++i)
  110. printf("%d\n", ans[i]);
  111. return ;
  112. }

  

BZOJ 1176 Mokia CDQ分治+树状数组的更多相关文章

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

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

  2. bzoj 3262 陌上花开 - CDQ分治 - 树状数组

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...

  3. BZOJ 2683 简单题 cdq分治+树状数组

    题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...

  4. 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组

    [BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...

  5. 【bzoj3262】陌上花开 CDQ分治+树状数组

    题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...

  6. 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组

    题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...

  7. LOJ3146 APIO2019路灯(cdq分治+树状数组)

    每个时刻都形成若干段满足段内任意两点可达.将其视为若干正方形.则查询相当于求历史上某点被正方形包含的时刻数量.并且注意到每个时刻只有O(1)个正方形出现或消失,那么求出每个矩形的出现时间和消失时间,就 ...

  8. BZOJ 4553 [Tjoi2016&Heoi2016]序列 ——CDQ分治 树状数组

    考虑答案的构成,发现是一个有限制条件的偏序问题. 然后三个维度的DP,可以排序.CDQ.树状数组各解决一维. #include <map> #include <cmath> # ...

  9. BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ第一题,warush了好久.. CDQ分治推荐论文: 1 <从<C ...

随机推荐

  1. redis中插入用户集合的语句,有四个属性

    一.redis 数据结构使用场景 原来看过 redisbook 这本书,对 redis 的基本功能都已经熟悉了,从上周开始看 redis 的源码.目前目标是吃透 redis 的数据结构.我们都知道,在 ...

  2. Redis使用详细教程【转】

    转自 Redis使用详细教程 - wangyuyu - 博客园http://www.cnblogs.com/wangyuyu/p/3786236.html 一.Redis基础部分: 1.redis介绍 ...

  3. sicily 1259. Sum of Consecutive Primes

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  4. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...

  5. Char 与 Byte

    var c: Char; b: Byte; begin c := 'A'; ShowMessage(c); //A b := ; ShowMessage(IntToStr(b)); c := Chr( ...

  6. SGU 209. Areas

    209. Areas time limit per test: 0.25 sec.memory limit per test: 65536 KB input: standardoutput: stan ...

  7. cent 7.0 安装mysql

    安装命令 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community ...

  8. Python教程(一)Python简介

    Python就为我们提供了非常完善的基础代码库,覆盖了网络.文件.GUI.数据库.文本等大量内容,被形象地称作“内置电池(batteries included)”.用Python开发,许多功能不必从零 ...

  9. neo4j中索引的使用

    neo4j可以对node和relationship中的属性建立索引,索引中的node(relationship)和属性对key-value为多对多的关系.一个node(relationship)可以在 ...

  10. CentOS下Redis安装与配置

    本文详细介绍redis单机单实例安装与配置,服务及开机自启动.如有不对的地方,欢迎大家拍砖o(∩_∩)o (以下配置基于CentOS release 6.5 Final, redis版本3.0.2 [ ...