POJ2155 - Matrix (二维树状数组)

                                                               Matrix

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 20599   Accepted: 7673

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. 

思路:二维树状数组区间修改,单点查询,不过一直pe,最后看别人怎么输出才知道。。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lbt(x) (x&(-x))
#define IOS ios::sync_with_stdio(false)
#define DEBUG cout<<endl<<"DEBUG"<<endl;
using namespace std;
int w[1010][1010],n,a,b,c,d,m;char cs[100];
void add(int x,int y,int p){
    for(int i=x;i<=n;i+=lbt(i))
        for(int j=y;j<=n;j+=lbt(j))
            w[i][j]+=p;
}
int sum(int x,int y){
    int i,sum=0;
    i=y;
    while(x){
        y=i;
        while(y){
            sum+=w[x][y];
            y-=lbt(y);
        }
        x-=lbt(x);
    }
    return sum;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(w,0,sizeof(w));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%s",cs);
            if(cs[0]=='C'){
                scanf("%d%d%d%d",&a,&b,&c,&d);
                add(a,b,1);
                add(c+1,b,-1);
                add(c+1,d+1,1);
                add(a,d+1,-1);
            }
            else{
                scanf("%d%d",&a,&b);
                if(sum(a,b)%2 == 0)printf("0\n");
                else printf("1\n");
            }
        }
        if(t > 0)printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Endeavor_G/article/details/85268841
今日推荐