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?
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
Then K
Then Q
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
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;
}