关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回

poj2155

发布时间:2022-07-27 13:55:35
                                                                                                          Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15090   Accepted: 5662
Descriptionpoj2155
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 Output1

0
0
1


转载:

思路:
这题和树状数组的操作正好相反,树状数组是对点更新,成段求和,这题要
求成段更新,对点求值。但是还是可以转化,我们不妨先来考虑一维的情况,给
定一排数字,每次在区间进行进行成端加上一个数,然后询问某个点的值,很容
易想到线段树,但是线段树的常系数太大,我们试图用树状数组来解决,方法是
给定区间[a, b],如果要求在[a,b]区间都加上T我们只要在a的位置插入一个T,
然后在b+1的位置插入一个-T,这样下次询问某个值k的时候,只要将[1,k]的和求
出来就是k这个位置的值,为什么呢?分三种情况讨论:
1. k < a 先前的插入操作不影响此次结果
2. a <= k <= b a的位置插入T后,统计时值被加了一次
3. k > b。 a的位置有T,b+1的位置有-T,正好抵消
所以结论成立。
然后就可以扩展到二维的情况,也是一样,如果对于(x1, y1) (x2, y2)这个
矩形,只要在(x1, y1) (x2+1, y2+1)这两个点插入T,而(x2+1, y1) (x1, y2+1)
这两个点插入-T即可。
本题的操作是异或,其实还是一样的,就是在二进制内的无进位加法。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int n;
int c[1001][1001];
int lowbit(int x)
{
    return (x)&(-x);
}
void add(int x,int y)
{
    
    while(x<=n)
    {
        int ty=y;
        while(ty<=n)
        {
            c[x][ty]^=1; // c[x][ty]++;
            ty+=lowbit(ty);
        }
        x+=lowbit(x);
    }
}
int sum(int x,int y)
{
    int s=0;
    if(x>n)x=n;
    if(y>n)y=n;

    while(x>0)
    {
            int ty=y;

        while(ty>0)
        {
            s^=c[x][ty]; //s += c[x][ty];
            ty-=lowbit(ty);
        }
        x-=lowbit(x);
    }
    return s; 
}
int main()
{
    int t,x1,y1,k,x2,y2;
    char s[3];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        memset(c,0,sizeof(c));
        while(k--)
        {
            scanf("%s",s);
            if(s[0]=='C')
            {
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                add(x1,y1); //这里很关键。。。
                add(x2+1,y2+1);
                add(x1,y2+1);
                add(x2+1,y1);
            }
            else 
            {
                scanf("%d%d",&x1,&y1);
                printf("%d
",sum(x1,y1)); //printf("%d
",sum(x1,y1)%2);  这样做也可以的。。
            }
        }
        if(t)
            printf("
");
    }
    return 0;
}

/template/Home/DawnNew/PC/Static

立即注册风纳云账号,免费体验多款产品

立即注册