POJ 2155 Matrix (二维树状数组)

来源:互联网 发布:js模块化规范 编辑:程序博客网 时间:2024/05/16 18:53

Matrix

Time Limit:3000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u
Submit        Status

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




这是楼教主出的经典题目啊~~~不A别后悔呦^_^


题意:给个N*N的矩阵,所有元素初始化为零,且该矩阵里的元素只能有0,1。有两个操作——改变和查询,改变是改变一个指定的矩阵(会给出左上角和右下角的坐标)里的

所有元素,即0变为1,1变为0。查询是查询矩阵中某个元素(会给出坐标)的值。


心得:细节没注意,TLE到吐血!!!最开始也想到了方法,但是到后来写出来的时候一直TLE,郁闷。。。后来一看好多小细节!比如说读取操作类型的时候,最好不要直接开一个char变量f然后用%c读入,这样很容易出问题,可以开一个字符数组f[4]用%s读入,判断的时候用f[0]去比较即可。


分析:

         思路一:直接按要求改变元素的值,最后输出即可。不过此时的sum()要有所改变,不是加了,而是抑或。change()里面,取反时可以用‘  !’取反,也可以&1。

详见代码1

  

         思路二:由于初始所有元素均为0,则可用sum()记录每个元素改变的次数,只需%2判断即可确定每个元素的值。详见代码2






AC代码1:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#define INF 0x7fffffffusing namespace std;const int maxn = 1000 + 10;int c[maxn][maxn];int n;int lowbit(int x){    return x & (-x);}void change(int x,int y){    int i,j;    for(i=x; i<=n; i+=lowbit(i))        for(j=y; j<=n; j+=lowbit(j))            c[i][j] = !c[i][j];   //            c[i][j] ^= 1;            //和上面的取反均可   }int sum(int x,int y){     int ret = 0,i,j;     for(i=x; i>0; i-=lowbit(i))        for(j=y; j>0; j-=lowbit(j))            ret ^= c[i][j];           //此时不再是+,而是^     return ret;}int main(){     int T,t,x1,y1,x2,y2;    char f[4];    scanf("%d",&T);    while(T--)    {        memset(c,0,sizeof(c));        scanf("%d%d",&n,&t);        while(t--)        {            scanf("%s",f);            if(f[0] == 'C')            {                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                change(x1,y1);          //此处很容易错,把图画出来就很清晰了                change(x1,y2+1);                change(x2+1,y1);                change(x2+1,y2+1);            }            else            {                scanf("%d%d",&x1,&y1);                printf("%d\n",sum(x1,y1));            }        }        printf("\n");    }    return 0;}

AC代码2:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#define INF 0x7fffffffusing namespace std;const int maxn = 1000 + 10;int c[maxn][maxn];int n;int lowbit(int x){    return x & (-x);}void change(int x,int y){    int i,j;    for(i=x; i<=n; i+=lowbit(i))        for(j=y; j<=n; j+=lowbit(j))            c[i][j]++;                 //只需++,用来计改变的次数}int sum(int x,int y){    int ret = 0,i,j;    for(i=x; i>0; i-=lowbit(i))        for(j=y; j>0; j-=lowbit(j))            ret += c[i][j];    return ret;}int main(){    int T,t,x1,y1,x2,y2;    char f[4];    scanf("%d",&T);    while(T--)    {        memset(c,0,sizeof(c));        scanf("%d%d",&n,&t);        while(t--)        {            scanf("%s",f);            if(f[0] == 'C')            {                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                change(x1,y1);                change(x1,y2+1);                change(x2+1,y1);                change(x2+1,y2+1);            }            else            {                scanf("%d%d",&x1,&y1);                printf("%d\n",sum(x1,y1)%2);            //%2判断,确定元素的值            }        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击