二维树状数组-poj2155

来源:互联网 发布:玻璃胶 白色透明知乎 编辑:程序博客网 时间:2024/04/30 17:39
Language:
Matrix
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 15922 Accepted: 6004

Description

Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N). 

We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions. 

1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 
2. Q x y (1 <= x, y <= n) querys A[x, y]. 

Input

The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case. 

The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above. 

Output

For each querying output one line, which has an integer representing A[x, y]. 

There is a blank line between every two continuous test cases. 

Sample Input

12 10C 2 1 2 2Q 2 2C 2 1 2 1Q 1 1C 1 1 2 1C 1 2 1 2C 1 1 2 2Q 1 1C 1 1 2 1Q 2 1

Sample Output

1001思路:二维树状数组。问题:一个由数字构成的大矩阵,能进行两种操作 1) 对矩阵里的某个数加上一个整数(可正可负) 2) 查询某个子矩阵里所有数字的和,要求对每次查询,输出结果。   一维树状数组很容易扩展到二维,在二维情况下:数组A[][]的树状数组定义为:   C[x][y] = ∑a[i][j], 其中, x-lowbit(x) + 1 <= i <= x, y-lowbit(y) + 1 <= j <= y. 例:举个例子来看看C[][]的组成。 设原始二维数组为: A[][]={{a11,a12,a13,a14,a15,a16,a17,a18,a19},          {a21,a22,a23,a24,a25,a26,a27,a28,a29},        {a31,a32,a33,a34,a35,a36,a37,a38,a39},        {a41,a42,a43,a44,a45,a46,a47,a48,a49}}; 那么它对应的二维树状数组C[][]呢?   记:     B[1]={a11,a11+a12,a13,a11+a12+a13+a14,a15,a15+a16,...} 这是第一行的一维树状数组     B[2]={a21,a21+a22,a23,a21+a22+a23+a24,a25,a25+a26,...} 这是第二行的一维树状数组     B[3]={a31,a31+a32,a33,a31+a32+a33+a34,a35,a35+a36,...} 这是第三行的一维树状数组     B[4]={a41,a41+a42,a43,a41+a42+a43+a44,a45,a45+a46,...} 这是第四行的一维树状数组 那么:   C[1][1]=a11,C[1][2]=a11+a12,C[1][3]=a13,C[1][4]=a11+a12+a13+a14,c[1][5]=a15,C[1][6]=a15+a16,... 这是A[][]第一行的一维树状数组   C[2][1]=a11+a21,C[2][2]=a11+a12+a21+a22,C[2][3]=a13+a23,C[2][4]=a11+a12+a13+a14+a21+a22+a23+a24, C[2][5]=a15+a25,C[2][6]=a15+a16+a25+a26,... 这是A[][]数组第一行与第二行相加后的树状数组   C[3][1]=a31,C[3][2]=a31+a32,C[3][3]=a33,C[3][4]=a31+a32+a33+a34,C[3][5]=a35,C[3][6]=a35+a36,... 这是A[][]第三行的一维树状数组   C[4][1]=a11+a21+a31+a41,C[4][2]=a11+a12+a21+a22+a31+a32+a41+a42,C[4][3]=a13+a23+a33+a43,... 这是A[][]数组第一行+第二行+第三行+第四行后的树状数组   搞清楚了二维树状数组C[][]的规律了吗? 仔细研究一下,会发现: (1)在二维情况下,如果修改了A[i][j]=delta,则对应的二维树状数组更新函数为: 
 private void Modify(int i, int j, int delta){                  A[i][j]+=delta;            for(int x = i; x< A.length; x += lowbit(x))        for(int y = j; y <A[i].length; y += lowbit(y)){          C[x][y] += delta;                }     }


(2)在二维情况下,求子矩阵元素之和∑ a[i][j](前i行和前j列)的函数为
    int Sum(int i, int j){      int result = 0;      for(int x = i; x > 0; x -= lowbit(x)) {        for(int y = j; y > 0; y -= lowbit(y)) {            result += C[x][y];        }      }    return result;   }比如:    Sun(1,1)=C[1][1];  Sun(1,2)=C[1][2]; Sun(1,3)=C[1][3]+C[1][2];...    Sun(2,1)=C[2][1];  Sun(2,2)=C[2][2]; Sun(2,3)=C[2][3]+C[2][2];...    Sun(3,1)=C[3][1]+C[2][1]; Sun(3,2)=C[3][2]+C[2][2];
转化为树状数组的方法便是:01矩阵初始化为0,对于置反操作,等价于将小矩形块的四角置反(以此表示这四角代表的矩形块被执行置反),于是我们对于置反操作,只操作四角的即可(可以累加操作次数,也可以单纯的模拟置反操作,此时可利用bool数组),然后对于询问,只统计其左上方的矩阵元素的和(累加)奇偶情况,奇数说明最终置1,否则置0。
注意修改函数add只是对于[i][j]以及其后的点起作用,而我们统计的时候显然是不把自身统计在内的,所以对于子矩形块终点
[c][d],要分别++,这样就可避免统计的时候把自己算在内

代码如下:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int MAX=1005;int N,T;int A[MAX][MAX];void update(int i,int j,int num){    while(i<=N)    {        int tmp=j;        while(tmp<=N)        {            A[i][tmp]+=num;            tmp+=tmp&(-tmp);        }        i+=i&(-i);    }}int sum(int x,int y){    int cnt=0;    while(x>0)    {        int tmp=y;        while(tmp>0)        {            cnt+=A[x][tmp];            tmp-=tmp&(-tmp);        }        x-=x&(-x);    }    return cnt;}int main(){    //freopen("in.txt","r",stdin);    int X;    cin>>X;    char x;    int x1,y1,x2,y2;    while(X--)    {        cin>>N>>T;        memset(A,0,sizeof(A));        for(int i=0;i<T;i++)        {            cin>>x;            if(x=='C')            {                cin>>x1>>y1>>x2>>y2;                x2++;                y2++;                update(x1,y1,1);                update(x1,y2,-1);                update(x2,y1,-1);                update(x2,y2,1);            }            else            {                cin>>x1>>x2;                cout<<sum(x1,x2)%2<<endl;            }        }        cout<<endl;    }    return 0;}


原创粉丝点击