题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1452

题意:给出一个数字矩阵(矩阵中任何时候的数字均为[1,100]),两种操作:(1)修改某个位置的数字;(2)求某个子矩阵中某个数字的个数。

思路:二维树状数组的操作看起来跟一维的差不多,只是循环改为两重而已。主要操作有:(1)增加某个位置的值;(2)询问[1,1,x,y]子矩阵的和。利用(2)操作以及区间的减法操作我们能求出任意一个子矩阵的数字和。这道题用a[i][x][y]来记录关于数字i的信息。

int a[105][N][N];

void add(int a[N][N],int x,int y,int t)
{
    int i,j;
    for(i=x;i<N;i+=i&-i) for(j=y;j<N;j+=j&-j)
    {
        a[i][j]+=t;
    }
}

int query(int a[N][N],int x,int y)
{
    int ans=0,i,j;
    for(i=x;i;i-=i&-i) for(j=y;j;j-=j&-j)
    {
        ans+=a[i][j];
    }
    return ans;
}

int query(int a[N][N],int x1,int y1,int x2,int y2)
{
    return query(a,x2,y2)-query(a,x1-1,y2)-query(a,x2,y1-1)+query(a,x1-1,y1-1);
}

int n,m;
int b[N][N];

int main()
{
    Rush(n)
    {
        RD(m); clr(a,0);
        int i,j,x;
        FOR1(i,n) FOR1(j,m)
        {
            RD(x);
            add(a[x],i,j,1);
            b[i][j]=x;
        }
        int Q;
        RD(Q);
        int op,x1,x2,y1,y2,c;
        while(Q--)
        {
            RD(op);
            if(op==1)
            {
                RD(x1,y1,c);
                add(a[b[x1][y1]],x1,y1,-1);
                b[x1][y1]=c;
                add(a[c],x1,y1,1);
            }
            else
            {
                RD(x1,x2); RD(y1,y2); RD(c);
                PR(query(a[c],x1,y1,x2,y2));
            }
        }
    }
}

BZOJ 1452 Count(二维树状数组)的更多相关文章

  1. BZOJ 1452 Count(二维树状数组)

    大水题. 建立100个二维树状数组,总复杂度就是O(qlognlogm). # include <cstdio> # include <cstring> # include & ...

  2. bzoj 1452: [JSOI2009]Count ——二维树状数组

    escription Input Output Sample Input Sample Output 1 2 HINT ———————————————————————————————————————— ...

  3. bzoj1452 [JSOI2009]Count ——二维树状数组

    中文题面,给你一个矩阵,每一个格子有数字,有两种操作. 1. 把i行j列的值更改 2. 询问两个角坐标分别为(x1,y1) (x2,y2)的矩形内有几个值为z的点. 这一题的特点就是给出的z的数据范围 ...

  4. 【bzoj1452】[JSOI2009]Count 二维树状数组

    题目描述 输入 输出 样例输入 样例输出 1 2 题解 二维树状数组 一开始没看到 1≤c≤100 ,想到了主X树和X块,结果发现c的范围那么小... 二维树状数组水题,和一维的一样,向上修改,向下查 ...

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

    1452: [JSOI2009]Count Description Input Output Sample Input Sample Output 1 2 HINT Source 题解:设定C[101 ...

  6. Count(二维树状数组)

    [bzoj1452][JSOI2009]Count Description Input Output Sample Input Sample Output 12   HINT 题解:对于每一个颜色建一 ...

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

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

  8. bzoj 1452: [JSOI2009]Count (二维树状数组)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1452 思路: 对每个颜色开一个二维树状数组维护就好了 实现代码: #include<b ...

  9. BZOJ 1452 Count 【模板】二维树状数组

    对每种颜色开一个二维树状数组 #include<cstdio> #include<algorithm> using namespace std; ; ][maxn][maxn] ...

随机推荐

  1. dancing link

    http://www.cnblogs.com/grenet/p/3145800.html 链接给的博客写的很好,比较好懂. 可惜不是c语言... 于是决定自己要建一个模板. 一道裸题:hustoj 1 ...

  2. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...

  3. DirectShow学习笔记

    DirectShow, as you might have guessed, is a COM based multimedia framework that makes the task of ca ...

  4. POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)

    不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...

  5. 《Thinking in C++》学习笔记(一)【第二章】

    第二章 对象的创建与使用 2.1语言的翻译过程 翻译器分为两类:解释器(interpreter)和编译器(compiler). 2.1.1解释器 解释器将源代码转化成一些动作(它可由许多机器指令组成) ...

  6. Unix安装BerkeleyDB

    下载安装包Berkeley DB 5.3.21.tar.gz http://www.oracle.com/technetwork/products/berkeleydb/downloads/index ...

  7. .net web程序发布之后,出现编译错误

    .net web程序发布之后,在IIS上浏览的时候出现编译错误. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temp ...

  8. tomcat 跨域

    http://www.360doc.com/content/14/0920/14/7909375_411017244.shtml http://www.tuicool.com/articles/Znq ...

  9. UVA 11401 - Triangle CountingTriangle Counting 数学

    You are given n rods of length 1,2, . . . , n. You have to pick any 3 of them and build a triangle. ...

  10. poj 3228(二分+最大流)

    题目链接:http://poj.org/problem?id=3228 思路:增设一个超级源点和一个超级汇点,源点与每一个gold相连,容量为gold数量,汇点与仓库相连,容量为仓库的容量,然后就是二 ...