poj 2155 分类: poj templates 2015-03-30 17:34 37人阅读 评论(0) 收藏


二维树状数组,函数部分是模板。。


#include<map>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
#define LowBit(x) (x&(-x))

const int MAXN =  1005;

int n;
int a[MAXN][MAXN] = {0};
char ch[5] = {'\0'};

void add(int x,int y,int val)
{
    for(int i = x; i <= n ; i += LowBit(i))
      for(int j = y ; j <= n ; j += LowBit(j))
         a[i][j] += val;
}
int count(int x,int y)
{
    int ret = 0;

    for(int i = x; i > 0; i -=LowBit(i))
      for(int j = y ; j > 0; j -= LowBit(j))
        ret += a[i][j];

    return  ret;    
}
int main()
{
    int X;

#ifndef ONLINE_JUDGE    
    freopen("poj2155.in","r",stdin);
    freopen("poj2155.out","w",stdout);
#endif

    scanf("%d",&X);

    while(X--)
    {
        memset(a,0,sizeof(a));

        int T; scanf("%d%d",&n,&T);
        while(T--)
        {
            int x1,x2,y1,y2;
            scanf("%s",ch);
            switch(ch[0])
            {
                case 'C':
                    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                    x2++,y2++;
                    add(x2,y2,1);
                    add(x1,y2,-1);
                    add(x2,y1,-1);
                    add(x1,y1,1);
                    break;
                case 'Q':
                    scanf("%d%d",&x1,&y1);
                    int sum = count(x1,y1);
                    printf("%d\n",sum&1);
                    break;
            }

        }   
        printf("\n");
    }

#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif  
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-03-30 17:34  <Dash>  阅读(104)  评论(0编辑  收藏  举报