HDU 1892 See you~

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值