#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;
int map[1015][1015];
void update(int x,int y, int n)
{
for(int i=x;i<=1005;i+=(i&(-i)))
{
for(int j=y;j<=1005;j+=(j&(-j)))
{
map[i][j]+=n;
}
}
}
int sum(int x,int y)
{
int s = 0;
for(int i=x;i>=1;i-=(i&(-i)))
{
for(int j=y;j>=1;j-=(j&(-j)))
{
s+=map[i][j];
}
}
return s;
}
int main(){
// freopen("exp.in","r",stdin);
int t, q;
char s[3];
int x1,x2,y1,y2,n1;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
printf("Case %d:\n",i);
memset(map,0,sizeof(map));
for(int j=1;j<=1005;j++)
for(int k = 1;k<=1005;k++)
update(j,k,1);
scanf("%d",&q);
for(int j=1;j<=q;j++)
{
scanf("%s",s);
if(s[0]=='S')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
printf("%d\n",sum(x2+1,y2+1)+sum(x1,y1)-sum(x1,y2+1)-sum(x2+1,y1));
}
else if(s[0]=='A')
{
scanf("%d%d%d",&x1,&y1,&n1);
update(x1+1,y1+1,n1);
}
else if(s[0]=='D')
{
scanf("%d%d%d",&x1,&y1,&n1);
int v = sum(x1+1,y1+1)+sum(x1,y1)-sum(x1,y1+1)-sum(x1+1,y1);
n1=(n1-v>0)?v:n1;
update(x1+1,y1+1,-n1);
}
else if(s[0]=='M')
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1);
int v = sum(x1+1,y1+1)+sum(x1,y1)-sum(x1,y1+1)-sum(x1+1,y1);
n1=(n1-v)>0?v:n1;
update(x1+1,y1+1,-n1);
update(x2+1,y2+1,n1);
}
}
}
// fclose(stdin);
return 0;
}
Code From Hdu 1982