Two dimensional Tree_Array.
The portal:http://acm.hdu.edu.cn/showproblem.php?pid=1892
//Two dimensional tree array.
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int M = 1005;
int Bit[M+5][M+5];
void Insert(int x,int y,int value) {
for(int i = x ; i <= M ; i += i & (-i)) {
for(int j = y ; j <= M ; j += j & (-j)) {
Bit[i][j] += value;
}
}
}
int Query(int x,int y) {
int ret = 0;
for(int i = x ; i ; i -= i & (-i)) {
for(int j = y ; j ; j -= j & (-j) ) {
ret += Bit[i][j];
}
}
return ret;
}
int Get_area_sum(int x1,int y1,int x2,int y2) {
return Query(x2+1,y2+1)+Query(x1,y1)-Query(x2+1,y1)-Query(x1,y2+1);
}
//To sum a area;
//Sum points from (x1+1,y1+1) to (x2+1,y2+1);
void Deal_with() {
int T,n,t = 1;
scanf("%d",&T);
while(T--) {
printf("Case %d:\n",t++);
scanf("%d",&n);
char temp_c;
int x1,x2,y1,y2,temp_value;
memset(Bit,0,sizeof(Bit));
for(int i = 1 ; i <= M ; i++) {
for(int j = 1 ; j <= M ; j ++) {
Insert(i,j,1);
}
}
for(int i = 0 ; i < n ; i++) {
getchar();
scanf("%c %d %d",&temp_c,&x1,&y1);
if(temp_c == 'S') {
scanf("%d %d",&x2,&y2);
if(x1 > x2) swap(x1,x2);
if(y1 > y2) swap(y1,y2);
printf("%d\n",Get_area_sum(x1,y1,x2,y2));
}
else if(temp_c == 'A') {
scanf("%d",&temp_value);
Insert(x1+1,y1+1,temp_value);
}
else if(temp_c == 'D') {
scanf("%d",&temp_value);
int temp = Get_area_sum(x1,y1,x1,y1);
temp_value = temp_value > temp ? temp : temp_value;
//May be etemp_value > point(x1,y1) , Because point(x1,y1) can't become minus;
Insert(x1+1,y1+1,-temp_value);
}
else {
scanf("%d %d %d",&x2,&y2,&temp_value);
int temp = Get_area_sum(x1,y1,x1,y1);
temp_value = temp_value > temp ? temp : temp_value;
//The same reason point(x1,y1) can't become minues;
Insert(x1+1,y1+1,-temp_value);
Insert(x2+1,y2+1,temp_value);
}
}
}
}
int main(void) {
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}