POJ 2155 Matrix (二维树状数组)

题意:

给你一个N*N的矩阵,一开始每个元素都是0,给你q 个操作,每个操作,可以把(x1,y1)到(x2,y2)的矩形里元素取反(0变1,1变0)。 可以查询每个元素的值。

思路:

二维树状数组的应用。

但是二维树状数组是用来求每个子矩阵元素之和的。


这里思路比较灵活一些。

我们先来考虑一个一维的问题:


我们把[L,R] 这个区间 进行取反的话

我们可以在L这个位置单点加一。 在R+1这个位置单点加一。

这就相当于取反了。

为什么呢, 我们任取一个点L+1把, 1~L+1的区间和 比原来多了一个1,那么我们模2的话,就可以体现出取反了。

二维也是一样,画个图分区间考虑就好了。

总共有四个位置需要加一。

#include 
#include 
#include 
using namespace std;



const int maxn = 1000 + 7;

int c[maxn][maxn];

int n,T,q,ks;

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

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

void add(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]++;
        }
    }
}

char cmd[2];
int main(){


    scanf("%d",&T);
    while(T--){
        if (ks++)puts("");
        memset(c,0,sizeof c);
        scanf("%d %d",&n, &q);
        while(q--){
            scanf("%s",cmd);
            if (cmd[0] == 'C'){
                int x,y,x2,y2;
                scanf("%d %d %d %d",&x, &y, &x2, &y2);

                add(x,y);
                add(x,y2+1);
                add(x2+1,y);
                add(x2+1,y2+1);

            }
            else {
                int x,y;
                scanf("%d %d",&x, &y);
                printf("%d\n",sum(x,y) % 2);
            }


        }



    }

    return 0;
}

Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 26564   Accepted: 9798

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

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

Source

POJ Monthly,Lou Tiancheng

[Submit]   [Go Back]   [Status]   [Discuss]


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