树状数组之二

//poj 1195 Mobile phones

#include
<iostream> //简单二维树状数组
using namespace std;
int table[1025][1025]; //初始化为0
int S,X,Y,A,L,B,R,T,side;
int lowbit(int x)
{
return x&(-x);
}
void modify(int x,int y,int a)
{
x
++;y++; //使得表下标的最小值从1而不是从0开始
while(x<=side)
{
int j=y;
while(j<=side)
{
table[x][j]
+=a;
j
+=lowbit(j);
}
x
+=lowbit(x);
}
}
int sum(int x,int y)
{
x
++;y++;
int s=0;
while(x>0)
{
int j=y;
while(j>0)
{
s
+=table[x][j];
j
-=lowbit(j);
}
x
-=lowbit(x);
}
return s;
}
int main()
{
int d,s;
while(scanf("%d",&d))
{
switch (d)
{
case 0:
scanf(
"%d",&side);break;
case 1:
scanf(
"%d%d%d",&X,&Y,&A);
modify(X,Y,A);
break;
case 2:
scanf(
"%d%d%d%d",&L,&B,&R,&T);
s
=sum(R,T)-sum(L-1,T)-sum(R,B-1)+sum(L-1,B-1);
printf(
"%d\n",s);
break;
default:
break;
}
if(d==3)
break;
}
return 0;
}

  

posted on 2011-07-17 01:52  sysu_mjc  阅读(96)  评论(0编辑  收藏  举报

导航