题目分析 :
一道二维树状数组 的裸题, 只是需要对坐标做些处理即可, 另外, 初始化的时候 原来 com[i][j] = lowbit (i) * lowbit (j); WA 好多次, 直接用的modify(i,j,1)
好了, 直接代码吧, 代码过长, 内存多了一点点 , HDU 第二
/*
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
http://www.cnblog.com/MiYu
Author By : MiYu
Test : 1
Program : 1892
*/
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
int T,N;
const int MAX = 1001;
int mat[1002][1002];
int com[1002][1002];
void modify ( int x,int y, int n )
{
while ( x <= MAX ){
int t = y;
while ( t <= MAX ){
com[x][t] += n;
t += lowbit(t);
}
x += lowbit(x);
}
}
int quy ( int x, int y )
{
int sum = 0;
while ( x > 0 ){
int t = y;
while ( t > 0 ){
sum += com[x][t];
t -= lowbit(t);
}
x -= lowbit(x);
}
return sum;
}
inline bool scan_d(int &num)
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
int main ()
{
scan_d(T);{
int ca = 1;
while ( T -- ){
printf ( "Case %d:\n",ca++ );
scan_d(N); char s[5]; int a,b,x,y,m,res,maxx,maxy,minx,miny;
for ( int i = 1; i <= MAX; ++ i )
for ( int j = 1; j <= MAX; ++ j )
com[i][j] = lowbit(i) * lowbit(j), mat[i][j] = 1;
for ( int i = 1; i <= N; ++ i ){
scanf ( "%s",s );
switch ( s[0] ){
case 'S' : scan_d(a);scan_d(b);scan_d(x);scan_d(y); minx = min ( a,x );miny=min(b,y);maxx=max(a,x)+1;maxy=max(b,y)+1;
res = 0; res += quy( maxx,maxy ); res -= quy (maxx,miny); res -= quy(minx,maxy); res += quy(minx,miny);
printf ( "%d\n",res ); break;
case 'A' : scan_d(x);scan_d(y);scan_d(a);x++;y++; modify ( x,y,a ); mat[x][y] += a; break;
case 'D' : scan_d(x);scan_d(y);scan_d(a);x++;y++; if ( mat[x][y] >= a ) { modify ( x,y,-a ); mat[x][y] -= a; }
else { modify ( x,y,-mat[x][y] ); mat[x][y] = 0; } break;
case 'M' : scan_d(a);scan_d(b);scan_d(x);scan_d(y);scan_d(m);a++;b++;x++;y++; if ( mat[a][b] >= m )
{ mat[a][b] -= m; mat[x][y] += m; modify ( a,b,-m ); modify ( x,y,m ); }
else { modify ( a,b,-mat[a][b] ); modify ( x,y,mat[a][b] ); mat[x][y] += mat[a][b]; mat[a][b] = 0; } break;
}
}
}
}
return 0;
}