题目链接:http://codeforces.com/problemset/problem/707/E

给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值。

有q个操作,操作ask问你一个子矩阵的和是多少,操作switch将第i条链上的值0变原来的数or原来的数变0。

比较明显的二维数组数组暴力,注意的是ask操作不会超过2000,所以在switch操作的时候不能进行update操作,否则会超时。所以你在ask操作的时候update就会省时。

复杂度大概是2000*2000*log2(2000)*log2*(2000),cf上3s妥妥过。

  1. //#pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <list>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. typedef long long LL;
  15. typedef pair <int, int> P;
  16. const int N = 2e3 + ;
  17. int n, m;
  18. LL bit[N][N];
  19. struct data {
  20. int x[N], y[N], w[N], len;
  21. }a[N];
  22. bool change[N];
  23. int flag[N];
  24.  
  25. void update(int x, int y, LL val) {
  26. for(int i = x; i <= n; i += (i&-i)) {
  27. for(int j = y; j <= m; j += (j&-j)) {
  28. bit[i][j] += val;
  29. }
  30. }
  31. }
  32.  
  33. LL sum(int x, int y) {
  34. LL s = ;
  35. for(int i = x; i >= ; i -= (i&-i)) {
  36. for(int j = y; j >= ; j -= (j&-j)) {
  37. s += bit[i][j];
  38. }
  39. }
  40. return s;
  41. }
  42.  
  43. int main()
  44. {
  45. int k, q;
  46. scanf("%d %d %d", &n, &m, &k);
  47. for(int i = ; i <= k; ++i) {
  48. scanf("%d", &a[i].len);
  49. for(int j = ; j <= a[i].len; ++j) {
  50. scanf("%d %d %d", &a[i].x[j], &a[i].y[j], &a[i].w[j]);
  51. update(a[i].x[j], a[i].y[j], (LL)a[i].w[j]);
  52. }
  53. }
  54. scanf("%d", &q);
  55. char query[];
  56. int x1, y1, x2, y2, c;
  57. for(int i = ; i <= q; ++i) {
  58. scanf("%s", query);
  59. if(query[] == 'A') {
  60. scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  61. for(int id = ; id <= k; ++id) {
  62. if(change[id]) {
  63. if(flag[id] % ) {
  64. for(int j = ; j <= a[id].len; ++j) {
  65. update(a[id].x[j], a[id].y[j], (LL)a[id].w[j]);
  66. }
  67. } else {
  68. for(int j = ; j <= a[id].len; ++j) {
  69. update(a[id].x[j], a[id].y[j], (LL)-a[id].w[j]);
  70. }
  71. }
  72. change[id] = change[id] ? false: true;
  73. ++flag[id];
  74. }
  75. }
  76. printf("%lld\n", sum(x2, y2) - sum(x2, y1 - ) - sum(x1 - , y2) + sum(x1 - , y1 - ));
  77. } else {
  78. scanf("%d", &c);
  79. change[c] = change[c] ? false: true;
  80. }
  81. }
  82. return ;
  83. }

Codeforces 707 E. Garlands (二维树状数组)的更多相关文章

  1. Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  2. Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分

    Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...

  3. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  4. Codeforces #590 D 二维树状数组

    题意 给一个10^5之内的字符串(小写字母)时限2s 输入n,有n个操作  (n<10^5) 当操作是1的时候,输入位置x和改变的字母 当操作是2的时候,输入区间l和r,有多少不同的字母 思路 ...

  5. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  6. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  7. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  8. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  9. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

随机推荐

  1. Dom对象的方法应用一getElementById技巧、getElementsByName() IE,firefox兼容

    在document对象中有以下三个方法,对于程序员来说,真可谓无人不知,无人不晓,他们分别是: 1.getElementById()                  返回对拥有指定 id 的第一个对 ...

  2. ecshop 优化_将商品详情页goods.php重命名为shangpin.php

    有人说,将商品详情页的文件名 goods.php 改一个名字,对百度收录会有帮助,也许吧,这里不讨论是否有帮助,这里只讲解如何重命名. 例如:我们将 goods.php 改为 shangpin.php ...

  3. jQuery事件绑定方式(转)

    bind() 简要描述 bind()向匹配元素添加一个或多个事件处理器. 使用方式 $(selector).bind(event,data,function) event:必需项:添加到元素的一个或多 ...

  4. Hdu 1521 排列组合

    a1 n1 a2 n2 ... ak nkn=n1+n2+...+nk从n个数中选r个排列(不是组合噢)// 指数型母函数// 模板#include <iostream> #include ...

  5. NPOI 2.0导出word(docx格式)

    大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是 ...

  6. ubuntu 上更新安装 openoffice.org3的过程

    方法一:手动安装1首先在“应用成程序--添加/删除”里卸载openoffice 2.4,可能openoffice.org 2.4 Draw因为关联而无法卸载,忽略,卸载其他几项.然后在中文官方网上下载 ...

  7. web开发利器 fiddler

    http://mccxj.github.io/blog/20130531_introduce-to-fiddler.html

  8. c/c++ 编译器内存对齐问题

    C语言结构体对齐问题详解 转载自:http://blog.csdn.net/tiany524/article/details/6295551 测试环境32位机 WinXP: 编译器VC6(MS cl. ...

  9. windows下mysql5.7安装及配置

    装完msi后,复制my-default.ini文件,黏贴为my.ini文件,内容修改如下: # For advice on how to change settings please see# htt ...

  10. 让pre标签自动换行示例代码

    pre 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码.对于技术博客经 ...