题目连接:
题意:要求设计这样一个数据结构,支持下列操作
1.add(x,y,a).对二维数组的第x行,第y列加上a.
2.sum(l,b,r,t).求所有满足l<=x<=r,b<=y<=t,的数组元素的和
二位树状数组~
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define loop(s,i,n) for(i = s;i <= n;i++)
#define lowbit(x) x&-x
using namespace std;
int c[][];
int n;
void add(int a,int b,int val)
{
int i,j;
for(i = a;i <= n;i += lowbit(i))
{
for(j = b;j <= n;j += lowbit(j))
c[i][j] += val;
}
} int sum(int a,int b)
{
int res = ;
int i,j;
for(i = a;i > ;i -= lowbit(i))
{
for(j = b;j > ;j -= lowbit(j))
res+=c[i][j];
}
return res;
}
int main()
{
int t,i,j,a,b,l,r;
while(~scanf("%d",&t) != EOF)
{
if(!t)
{
scanf("%d",&n);
loop(,i,n)
{
loop(,j,n)
c[i][j] = ;
}
}
if(t == )
{
scanf("%d %d %d",&a,&b,&l);
add(a+,b+,l);
}
else if(t == )
{
scanf("%d %d %d %d",&a,&b,&l,&r);
printf("%d\n",sum(a,b)+sum(l+,r+)-sum(a,r+)-sum(l+,b));
}
if(t == )
break;
}
return ;
}