Q - Conturbatio (前缀和)

该博客介绍了如何解决一个棋盘问题,其中涉及车(Rook)在给定位置攻击特定矩形区域内的所有单元格。通过输入的车的位置和查询,利用前缀和的概念判断每个查询矩形内的所有格子是否都被车攻击到。博主提供了一种有效的解决方案,通过计算行和列的前缀和,确保在O(1)时间内回答每个查询,避免了超时错误。
摘要由CSDN通过智能技术生成

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.

1≤n,m,K,Q≤100,000.

1≤x≤n,1≤y≤m.

1≤x1≤x2≤n,1≤y1≤y2≤m.
Output
For every query output “Yes” or “No” as mentioned above.

Sample Input
2
2 2 1 2
1 1
1 1 1 2
2 1 2 2
2 2 2 1
1 1
1 2
2 1 2 2

Sample Output
Yes
No
Yes

题目大意:
车能攻击直线上的棋子,在一个n*m的棋盘上,给出k个车的位置,然后给出q组测试数据,每组给出矩阵的左下角坐标x1、y1,和右上角坐标x2、y2。求是否矩阵内的格子都可以被车攻击到

解题思路:
矩阵内的格子都可以被车攻击到的必要条件是每一行都有车或者每一列都有车,可以根据前缀的思想解这道题。注意输入用scanf,cin会超时。

完整代码:

#include<iostream>
using namespace std;
int main()
{
	int t,n,m,k,q;
	cin>>t;
	while(t--){
		int row[100010]={0},col[100010]={0};
		cin>>n>>m>>k>>q;
		while(k--){
			int x,y;
			scanf("%d%d",&x,&y);
			row[y]=1;
			col[x]=1;
		}
		for(int i=1;i<=n;i++){	//求分段前缀和 
			if(col[i]){
				col[i]+=col[i-1];
			}
		}
		for(int i=1;i<=m;i++){
			if(row[i]){
				row[i]+=row[i-1];
			}
		}
		while(q--){
			int x1,y1,x2,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			if(x2-x1+1<=col[x2]||y2-y1+1<=row[y2]){		//判断必要条件是否成立 
				cout<<"Yes"<<endl;
			}else{
				cout<<"No"<<endl;
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧林墨烟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值