POJ - 2155 Matrix(二维树状数组)

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
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
Sample Output
1
0
0
1

题意:给出一个N*N的矩阵只包含0和1,初始所有值都是0,输入C为区间或操作,输入Q为查询单点状态。输入C,X1,Y1,X2,Y2后即对该范围内所优点进行 或 操作,1取0,0取1
输入Q X Y查询X Y处坐标状态。
注意对此区间更新可以改为单点更新,只需更改要更改区域的四个边界点即可。因为是从x,y更新到n*n,因此要进行容斥操作,对x1,y1操作后又对x1,y2+1进行取反操作恢复状态,然后对x2+1,y1到n*n进行恢复状态,最后因为x2+1,y2+1恢复了2次状态,再取反一次恢复征程状态。
最后查询时查询到达x,y坐标的前缀和再%2即可得到该节点被其他区域覆盖后的取反次数,最终次数的前缀和%2就是被取反了计数次还是偶数次。

#include
#include
int tre[1005][1005];
int n,t;
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))
            tre[i][j]++;///对于区间更新,利用容斥原理,将区间的四个节点进行更新操作
}
int sum(int x,int y)///二维查询
{
    int sum=0;
    for(int i=x; i>0; i-=lowbit(i))
        for(int j=y; j>0; j-=lowbit(j))
            sum+=tre[i][j];
    return sum;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&t);
        memset(tre,0,sizeof(tre));
        while(t--)
        {
            char flag[3];
            scanf("%s",flag);
            if(flag[0]=='C')
            {
                int x1,y1,x2,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                update(x1,y1);///注意容斥原理更新,每次更新四个节点,每个节点从传参处开始
                update(x1,y2+1);///对于右边界和下边界,做+1的处理
                update(x2+1,y1);
                update(x2+1,y2+1);
            }
            else
            {
                int x,y;
                scanf("%d%d",&x,&y);
                printf("%d\n",sum(x,y)%2);///查询前缀和结果%2即查询节点当前状态
            }
        }
        printf("\n");
    }
}

你可能感兴趣的:(树状数组)