1016(二维树状数组)

来源:互联网 发布:淘宝投诉不成立怎么班 编辑:程序博客网 时间:2024/06/05 04:22

Problem 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. <br> <br>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. <br>

Output
For each querying output one line, which has an integer representing A[x, y]. <br> <br>There is a blank line between every two continuous test cases. <br>

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

题目大概+思路:

区间更新,单点求和的二维树状数组
比一位复杂一点,不过也是属于模板题。


代码:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int n;long long c[1002][1002];int vis[1002][1002];int lowbit(int x){    return x&(-x);}void add(int x,int y,int v){    for(int i=x;i<=1001;i+=lowbit(i))    {        for(int j=y;j<=1001;j+=lowbit(j))        c[i][j]+=v;    }}long long sum(int x,int y){    long long su=0;    for(int i=x;i>0;i-=lowbit(i))    {        for(int j=y;j>0;j-=lowbit(j))        su+=c[i][j];    }    return su;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m;        memset(vis,0,sizeof(vis));        memset(c,0,sizeof(c));        scanf("%d%d",&n,&m);        while(m--)        {            char p[2];             scanf("%s",p);             if(p[0]=='C')             {                 int w1,w2,e1,e2;                  scanf("%d%d%d%d",&w1,&w2,&e1,&e2);                  add(w1,w2,1);                  add(e1+1,e2+1,1);                  add(w1,e2+1,-1);                  add(e1+1,w2,-1);             }             else if(p[0]=='Q')             {                 int r1,r2;                 scanf("%d%d",&r1,&r2);                 long long sun=0;                 sun=sum(r1,r2);                 int k=0;                 if(sun%2==0)k=0;                 else k=1;                 printf("%d\n",k);             }        }        if(t!=0)printf("\n");    }    return 0;}


原创粉丝点击