POJ 2155 Matrix

Seeing code.

The portal:http://poj.org/problem?id=2155

//Two dimensional Tree_Array;
//Can sum Left_up's nodes in m * n Matrix;

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>

using namespace std;

const int N = 1005;
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;
}

void Deal_with(){
	int T,n,k;
	scanf("%d",&T);
	while(T--){
		memset(Bit,0,sizeof(Bit));
		scanf("%d %d",&n,&k);
		char temp_c;
		int x1,x2,y1,y2;
		for(int i=0;i<k;i++){
			getchar();
			//Deal '\n';
			scanf("%c %d %d",&temp_c,&x1,&y1);
			if(temp_c == 'C'){
				scanf("%d %d",&x2,&y2);
				Insert(x1,y1,1);
				Insert(x1,y2+1,1);
				Insert(x2+1,y1,1);
				Insert(x2+1,y2+1,1);
				//Use x2 + 1 and y2 + 1; Just for contain nodes in edge;
			}
			else {
				printf("%d\n",Query(x1,y1)&1);
				//Query (x1,y1),If an area contain (x1,y1); it will be a point in left_up of (x1,y1);
				//If an area don't contain(x1,y1);it may at left_up or right_down
				//and contails two points or four points
			}
		}
		if(T > 0)puts("");
	}
}

int main(void) {
	//freopen("a.in","r",stdin);
	Deal_with();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值