POJ 2029 Get Many Persimmon Trees 二维线段树 单点更新 区间求和

//POJ 2029 Get Many Persimmon Trees 二维线段树 单点更新 区间求和
/*
题意:一个0,1矩阵,在其n*m的子矩阵中找出含有1最多的,输出最多的数量

思路:二维线段树
*/

#include
#include
#include

#define N 105
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int n,m,k;
int sum[N<<2][N<<2];

void SubUpdate(int rt,int l,int r,int y,int t){
	if(l == r)
		++sum[t][rt];
	else{
		int mid = (l + r) >> 1;
		if(y <= mid) SubUpdate(lson,y,t);
		else		 SubUpdate(rson,y,t);
		sum[t][rt] = sum[t][rt<<1] + sum[t][rt<<1|1];
	}
}

void Update(int rt,int l,int r,int x,int y){
	SubUpdate(1,1,m,y,rt);
	if(l!=r){
		int mid = (l + r) >> 1;
		if(x <= mid) Update(lson,x,y);
		else		 Update(rson,x,y);
	}
}

int SubQuery(int rt,int l,int r,int LY,int RY,int t){
	if(LY <= l && RY >= r)
		return sum[t][rt];
	int mid = (l + r) >> 1;
	int ans = 0;
	if(LY <= mid) ans += SubQuery(lson,LY,RY,t);
	if(RY > mid ) ans += SubQuery(rson,LY,RY,t);
	return ans;
}

int Query(int rt,int l,int r,int LX,int RX,int LY,int RY){
	if(LX <= l && RX >= r)
		return SubQuery(1,1,m,LY,RY,rt);
	int mid = (l + r) >> 1;
	int ans = 0;
	if(LX <= mid) ans += Query(lson,LX,RX,LY,RY);
	if(RX > mid ) ans += Query(rson,LX,RX,LY,RY);
	return ans;
}

int Max(int x,int y){
	return x>y?x:y;
}

int main(){
	int i,j;
	int a,b;
	while(scanf("%d",&k),k){
		scanf("%d %d",&n,&m);
		memset(sum,0,sizeof(sum));
		while(k--){
			scanf("%d %d",&a,&b);
			Update(1,1,n,a,b);
		}
		scanf("%d %d",&a,&b);
		int mmax= 0;
		for(i = 1; i+a-1 <= n; ++i)
			for(j = 1; j+b-1 <= m; ++j)
				mmax = Max(mmax,Query(1,1,n,i,i+a-1,j,j+b-1));
		printf("%d\n",mmax);
	}
	return 0;
}

你可能感兴趣的:(POJ 2029 Get Many Persimmon Trees 二维线段树 单点更新 区间求和)