题意:给定一个坐标系,每个点有一个值,值可以改变,求任意矩形所围的值得大小。
#include <iostream>
using namespace std;
#define S 1025
int c[S][S], n;
int lowbit ( int x )
{
return x & ( -x );
}
void modify ( int x, int y, int v )
{
int i, j;
for ( i = x; i <= n; i += lowbit(i) )
{
for ( j = y; j <= n; j += lowbit(j) )
c[i][j] += v;
}
}
int sum ( int x, int y )
{
int i, j, sum = 0;
for ( i = x; i > 0; i -= lowbit(i) )
{
for ( j = y; j > 0; j -= lowbit(j) )
sum += c[i][j];
}
return sum;
}
int getSum ( int x1, int y1, int x2, int y2 )
{
return sum(x2,y2) - sum(x2,y1-1) - sum(x1-1,y2) + sum(x1-1,y1-1);
}
int main()
{
int oper, x1, y1, x2, y2, v;
scanf("%d%d",&oper,&n);
memset(c,0,sizeof(c));
while ( scanf("%d",&oper) && oper != 3 )
{
if ( oper == 1 )
{
scanf("%d%d%d",&x1,&y1,&v);
modify ( x1+1, y1+1, v );
}
else if ( oper == 2 )
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%d\n", getSum ( x1+1, y1+1, x2+1, y2+1 ) );
}
}
return 0;
}