Matrix(矩阵)

poj 2155

题目大意:第一行给出测试用例的次数,第二行第一个 给出矩阵的size,第二个给出命令的个数

C表示更新数据,将范围在x1<=x<=x2,和y1<=y<=y2区域内的数每个都加上1,Q表示询问某一个点的大小

解决:二维树状数组,与poj1195 mobile phones刚好相反,是插入区域,问点

#include <iostream> 
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1010;
int c[N][N];
void init()
{
    memset(c,0,sizeof(c));
}
int n;
int lowbit(int x)
{
    return x&(-x);
}
void updata(int x,int y,int inc)
{
    for(int i=x;i>0;i-=lowbit(i))
      for(int j=y;j>0;j-=lowbit(j))
       c[i][j]+=inc;
}
int get(int x,int y)
{
    int sum=0;
    for(int i=x;i<=n;i+=lowbit(i))
      for(int j=y;j<=n;j+=lowbit(j))
        sum+=c[i][j];
     return  sum%2;  
}
int main()
{
    int t,icase;
    char cmd;
    int a,b,c,d;
    scanf("%d",&icase);
    while(icase--)
    {
        init();
        scanf("%d%d",&n,&t);
        while(t--)
        {
            getchar();
            scanf("%c",&cmd);
            if(cmd=='C')
            {
                scanf("%d%d%d%d",&a,&b,&c,&d);
                updata(c,d,1);
                updata(a-1,b-1,1);
                updata(c,b-1,-1);
                updata(a-1,d,-1);
            }
            else 
            { 
                scanf("%d%d",&a,&b);
                printf("%d\n",get(a,b));
            }
         }
         printf("\n");
    }
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/hpustudent/p/2149319.html