Conturbatio
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 673 Accepted Submission(s): 309
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?
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 .
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 YesHintHuge input, scanf recommended.
问题描述
在一个n×m的国际象棋棋盘上有很多车(Rook),其中车可以攻击他所属的一行或一列,包括它自己所在的位置。
现在还有很多询问,每次询问给定一个棋盘内部的矩形,问矩形内部的所有格子是否都被车攻击到?
输入描述
输入文件包含多组数据,第一行为数据组数T。 每组数据有4个正整数n,m,K,Q。 K为车的数量,Q为询问的个数。 接下来有K行,每行两个整数x,y , 表示车所在的坐标。 再接下来有Q行,每行4个整数x1,y1,x2,y2,表示询问的矩形的左下角与右上角的坐标。 1≤n,m,K,Q≤100,000. 1≤x≤n,1≤y≤m. 1≤x1≤x2≤n,1≤y1≤y2≤m.
输出描述
对于每组询问,输出Yes或No。
对于题目要求,一个车的一行和一列都是攻击范围,所以我们这里应用两个数组就能够表示整个图,一个用来表示行是否被攻击,一个用来表示列是否被攻击~
输入的时候直接表示这行这列被攻击了即可:
for(int i=0;i<k;i++)//k个车
{
int x,y;
scanf("%d%d",&x,&y);
row[x]=1;
col[y]=1;
}
然后是询问,我们知道,一个区域如果行被攻击个遍或者是被列攻击个遍,那么就说明,这片区域已经沦落了、比如样例1中的图是这样的:1表示被攻击了的地方,0表示没有
1 | 1 |
1 | 0 |
row【1】=1;row【2】=0;
col【1】=1;col【2】=0;
样例1中询问1是这样的 1 1 1 2,我们显然看的出来,如果从x1到行x2中的row都是1,或者说从y1到y2中的col都是1,那么这片区域明显都被攻击过了;思路清晰了,这里就要上代码了:
#include<stdio.h>
#include<string.h>
using namespace std;
int row[101010];
int col[101010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
int n,m,k,q;
scanf("%d%d%d%d",&n,&m,&k,&q);
for(int i=0;i<k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
row[x]=1;
col[y]=1;
}
for(int i=1;i<=n;i++)//优化代码~
{
row[i]+=row[i-1];
col[i]+=col[i-1];
}
while(q--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x2-x1+1==row[x2]-row[x1-1]||y2-y1+1==col[y2]-col[y1-1])//然后这里用减法代替for循环
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
}
}