寒假Day46:POJ2155Matrix二维树状数组

题面:

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.
View Code

样例:

input

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

output
1
0
0
1
 

题意:

一个N*N的矩阵,初始化为0,且只能为1或0,

C x、y、xx、yy 两个坐标,代表一个矩阵的左上角和右下角所包围的这个矩阵进行翻转(0变1,1变0)

Q x、y代表输出该坐标对应的点到该图最左下角累计共进行过几次翻转操作=ans

最后ans为奇数输出1,偶数输出0

思路:

对一个二维树状数组进行修改和查询操作

1和0的相互变换利用异或操作即可

update是修改操作:

void update(int x, int y)
{
    for(int i=x; i<=n; i+=lowbit(i))
    {
        for(int j=y; j<=n; j+=lowbit(j))
            c[i][j]^=1;//++也对
    }
}

sum是查询操作:

int sum(int x, int y)
{
    int ret=0;
    for(int i=x; i>0; i-=lowbit(i))
    {
        for(int j=y; j>0; j-=lowbit(j))
            ret+=c[i][j];
    }
    return ret;
}

对于区域的更新操作:

(我的理解是对于点(x,y)到(n,n)的区域分成四个部分,分别进行更新)(不知道这样理解是否正确??)

update(x,y),update(xx+1,y),update(x,yy+1),update(xx+1,yy+1);

画了一幅图,5*5的矩阵,假设给的数据是C 2 2 3 3,那么就是如下图所示(我的坐标是按照二维数组的定义来画的)

那么就将这块区域的下面、右面、右下方全部进行更新,就相当于该区域进行了单独变换

也就是需要更新(2,2)、(4,2)、(2,4)、(4,4)到(5,5)的区域,四个部分

AC代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;

const int N = 1010;

int n,c[N][N];

int lowbit(int x)
{
    return x&(-x);
}

void update(int x, int y)
{
    for(int i=x; i<=n; i+=lowbit(i))
    {
        for(int j=y; j<=n; j+=lowbit(j))
            c[i][j]^=1;
    }
}

int sum(int x, int y)
{
    int ret=0;
    for(int i=x; i>0; i-=lowbit(i))
    {
        for(int j=y; j>0; j-=lowbit(j))
            ret+=c[i][j];
    }
    return ret;
}

int main()
{
    int t,q;
    scanf("%d",&t);
    while(t--)
    {
        memset(c,0,sizeof(c));
        scanf("%d %d",&n,&q);
        for(int i=1; i<=q; i++)
        {
            getchar();
            char ch;
            scanf("%c",&ch);
            if(ch=='C')//4
            {
                int x,y,xx,yy;
                scanf("%d %d %d %d",&x,&y,&xx,&yy);
                update(x,y),update(xx+1,y),update(x,yy+1),update(xx+1,yy+1);
            }
            else//2
            {
                int x,y;
                scanf("%d %d",&x,&y);
                printf("%d\n",sum(x,y)%2);
            }
        }
        printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/OFSHK/p/12452104.html