POJ 1195 Mobile phones(裸的二维树状数组)

http://poj.org/problem?id=1195

题意:给出一个矩阵,给某个格子加/减一个数,就某个子矩阵的和,1024*1024的范围,二维的树状数组

子矩阵(x1,y1,x2,y2)(左下角的点和右上角的点)的和ans=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define nMAX 1050
using namespace std;
int c[nMAX][nMAX],n;
int lowbit(int x)
{
    return x&(-x);
}
void add(int i,int j,int w)
{
    int temp;
    while(i<=n)
    {
        temp=j;
        while(temp<=n)
        {
            c[i][temp]+=w;
            temp+=lowbit(temp);
        }
        i+=lowbit(i);
    }
}
int sum(int i,int j)//与一维的稍有不同
{
    int ans=0,temp;
    while(i>=1)
    {
        temp=j;
        while(temp>=1)
        {
            ans+=c[i][temp];
            temp-=lowbit(temp);
        }
        i-=lowbit(i);
    }
    return ans;
}
int main()
{
    int i,j,k,f,commend;
    while(~scanf("%d",&commend))
    {
       if(commend==3)break;
       if(commend==0)
       {
           scanf("%d",&n);
           memset(c,0,sizeof(c));
       }
       if(commend==1)
       {
           scanf("%d%d%d",&i,&j,&k);//下表必须从1,1开始,因为lowbit(0)=0死循环
           add(i+1,j+1,k);
       }
       if(commend==2)
       {
           scanf("%d%d%d%d",&i,&j,&k,&f);
           i++,j++,k++,f++;
           int ans=sum(k,f)-sum(i-1,f)-sum(k,j-1)+sum(i-1,j-1);
           printf("%d\n",ans);
       }
    }
    return 0;
}

  

posted @ 2012-08-17 21:46  快乐.  阅读(117)  评论(0编辑  收藏  举报