【HDU】-5480-Conturbatio(思维,好)

Conturbatio

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


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
  
  
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
Hint
Huge input, scanf recommended.


题意:n*m的棋盘,有k个棋子,每个棋子可以攻击其那一行和那一列的格子。有q次查询,每次判断(x1,y1,x2,y2构成的)一个方格内的格子是否都可以被攻击。

题解:开始没有棋子攻击全部都是0,有棋子了,棋子的行和列被标记为1。

注意:下面 ah[ ] 和 aw[ ] 的含义

ah[ i ]表示 i 行及前面的行里会被攻击的所有行数,比如 h[ i ]=1,那么 ah[ i ] = ah[ i-1 ] + h[ i ];

同理: aw[ j ]表示 j 列及前面的列里会被攻击所有列数。

~最后看矩形的行或者列是否会被全部攻击即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
int ah[100010];
int aw[100010];
int h[100010];
int w[100010];
int main()
{
	int u;
	scanf("%d",&u);
	while(u--)
	{
		CLR(h,0);
		CLR(w,0);			//没有棋子全部初始化为 0 
		CLR(ah,0);
		CLR(aw,0);
		int n,m,k,q;
		scanf("%d %d %d %d",&n,&m,&k,&q);
		for(int i=1;i<=k;i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			h[x]=1,w[y]=1;			//那一行,那一列全部都可以被攻击标记为 1 
		}
		ah[0]=0;
		for(int i=1;i<=n;i++)
			ah[i]=ah[i-1]+h[i];		//ah[i]行以前(包括ah[i])有多少行可以被攻击,h[i]可以就是在ah[i-1]上+1 
		aw[0]=0;
		for(int i=1;i<=m;i++)
			aw[i]=aw[i-1]+w[i];		//aw[i]列以前(包括aw[i])有多少列可以被攻击 
		for(int i=1;i<=q;i++)
		{
			int x1,y1,x2,y2;
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
			if(x2>x1)
				swap(x1,x2);
			if(y2>y1)
				swap(y1,y2);				
			if(ah[x1]-ah[x2-1]==(x1-x2+1)||aw[y1]-aw[y2-1]==(y1-y2+1))	//判断 x1~x2,y1~y2是否被全部攻击 
				printf("Yes\n");			//如果矩形的行或者列可以被全部攻击就是可以的 
			else
				printf("No\n");
		}
	}
	return 0;
}

下面是我超时的代码,因为每次都需要把所有方格都跑一边!当然费时间啦!

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
int h[100010];
int w[100010];
int main()
{
	int u;
	scanf("%d",&u);
	while(u--)
	{
		CLR(h,0);
		CLR(w,0);
		int n,m,k,q;
		scanf("%d %d %d %d",&n,&m,&k,&q);
		for(int i=1;i<=k;i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			h[x]=1,w[y]=1;
		}
		for(int i=1;i<=q;i++)
		{
			int num=0;
			int x1,y1,x2,y2;
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
			if(x2>x1)
				swap(x1,x2);
			if(y2>y1)
				swap(y1,y2);		
			int num1=0,num2=0;
			for(int i=x2;i<=x1;i++)
				if(h[i])
					num1++;	
			if(num1==(x1-x2+1))
			{
				printf("Yes\n");
				continue;
			}
			for(int j=y2;j<=y1;j++)
				if(w[j])
					num2++;		
			if(num2==(y1-y2+1))	
			{
				printf("Yes\n");
				continue;
			}
			printf("No\n");
		}
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值