zoukankan      html  css  js  c++  java
  • POJ-2155-Matrix二位树状数组应用

    题目:

    一个只有0和1构成的二维平面,给你两种指令,一种是区间的更新,即0变为1,1变为0;一种是查询一个点是1还是0;

    由于是二进制,所以每次更新在相应的点上加一,最后对2取余即可。

    至于二维的树状数组的应用原理,我也是看了这篇论文才明白;

    国家队论文集/2009/武森《浅谈信息学竞赛中的“0”和“1”》

    我就在补充一下AC代码;

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <string>
    
    typedef long long LL;
    
    using namespace std;
    
    int T,M,n,c[1010][1010];
    char str[20];
    
    LL lowbit(LL x)
        {
            return x & -x;
        }
    
    void add(LL a,LL b,LL cont)
    {
        for(int i=a;i<=n;i+=lowbit(i))
        {
            for(int j=b;j<=n;j+=lowbit(j))
            {
                c[i][j]+=cont;
            }
        }
    }
    
    LL getsum(LL a,LL b)
    {
        LL sum=0;
        for(LL i = a;i>0;i-=lowbit(i))
        {
            for(LL j = b;j>0;j-=lowbit(j))
            {
                sum+=c[i][j];
            }
        }
        return sum;
    }
    
    int main(){
        scanf("%d",&T);
        while(T--)
        {
    
            scanf("%d%d",&n,&M);
            memset(c,0,sizeof(c));
            for(int i=1;i<=M;i++)
            {
                scanf("%s",str);
                if(str[0]=='C')
                {
                    LL x1,y1,x2,y2;
                    scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
                    add(x1,y1,1);            //分别对二维四个点进行更新
                    add(x1,y2+1,1);
                    add(x2+1,y1,1);
                    add(x2+1,y2+1,1);    
                }
                else if(str[0]=='Q')
                {
                    LL x,y;
                    scanf("%lld%lld",&x,&y);
                    printf("%lld
    ",getsum(x,y)%2);
                }
            }
            if(T!=0)printf("
    ");
        }
    
        return 0;
    }
    skr
  • 相关阅读:
    Nacos深入浅出(四)
    Nacos深入浅出(三)
    Nacos深入浅出(二)
    Nacos深入浅出(一)
    Mycat(1)
    redis事务
    git常用的方式
    redis主从复制
    redis持久化RDB和AOF
    Quartz
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/8408702.html
Copyright © 2011-2022 走看看