HDU 5480 Conturbatio(前缀和)

本文探讨ConturbatioTime中棋盘上的Rook攻击问题,介绍了一种使用前缀和算法来高效判断任意矩形区域是否被所有格子攻击的方法。通过输入测试用例数量、棋盘尺寸、Rook数量及查询次数,算法计算并输出每个查询的判断结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Conturbatio

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1294    Accepted Submission(s): 611


Problem Description
There are many rook on a chessboard, a rook can attack the row and column it belongs, including its own place.

There are also many queries, each query gives a rectangle on the chess board, and asks whether every grid in the rectangle will be attacked by any rook?
 

Input
The first line of the input is a integer T, meaning that there are T test cases.

Every test cases begin with four integers n,m,K,Q.
K is the number of Rook, Q is the number of queries.

Then K lines follow, each contain two integers x,y describing the coordinate of Rook.

Then Q lines follow, each contain four integers x1,y1,x2,y2 describing the left-down and right-up coordinates of query.

1n,m,K,Q100,000.

1xn,1ym.

1x1x2n,1y1y2m.
 

Output
For every query output "Yes" or "No" as mentioned above.
 

Sample Input
 
 
22 2 1 21 11 1 1 22 1 2 22 2 2 11 11 22 1 2 2
 
Sample Output
 
 
YesNoYes
Hint
Huge input, scanf recommended.
 

Source
 

Recommend

hujie   |   We have carefully selected several similar problems for you:  6297 6296 6295 6294 6293 


借鉴:https://blog.youkuaiyun.com/u010885899/article/details/48765049


#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
#define MAXN 100005
int row[MAXN];
int column[MAXN];
int main()
{
	int t,i;
	int n,m,k,q,x,y,x1,x2,y1,y2;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d",&n,&m,&k,&q);
		memset(row,0,sizeof(row));
		memset(column,0,sizeof(column));
		for(i=1;i<=k;i++)
		{
			scanf("%d%d",&x,&y);
			row[x]=1;
			column[y]=1;
		}
		for(i=1;i<=n;i++)
		{
			row[i]=row[i]+row[i-1];
		}
		for(i=1;i<=m;i++)
		{
			column[i]=column[i]+column[i-1]; //算前缀和的关键算法,这样就算好了1 12 123 ... 123..n的所有和,column[i]表示1到i为止的和
		}                                    //2~n==column[n]-column[1],这样所有的和就都算出来了。
		for(i=1;i<=q;i++)
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			if(x2-x1+1==row[x2]-row[x1-1]||y2-y1+1==column[y2]-column[y1-1])  //利用和来进行枚举比较,精妙,这也解释了为什么赋值1的原因
			{
				puts("Yes");
			}
			else
			{
				puts("No");
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值