前缀和 hdu 5480 (Conturbatio)

来源:互联网 发布:未来的大数据 编辑:程序博客网 时间:2024/05/17 01:13

Conturbatio

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 , meaning that there are  test cases. 

Every test cases begin with four integers 
 is the number of Rook,  is the number of queries. 

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

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






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          
题意:给出棋盘和车的位置,车可以攻击所在的行列,问能否攻击指定矩阵的所有位置。

题解 :前缀和。前缀算法能够从一个给定数据空间中找出其前缀特征,即前缀集合。前缀集合具有确定性,能够匹配该前缀集合的数据一定属于该数据空间,否则将一定不属于该数据空间。

#include<cstdio>#include<cstring>using namespace std;int ma[100100];int mark[100100];int main(){int t,n,m,k,q,i,j;int x,y,a,b,c,d;scanf ("%d",&t);while (t--){scanf ("%d %d %d %d",&n,&m,&k,&q);memset (mark,0,sizeof(mark));memset (ma,0,sizeof(ma));while (k--){scanf ("%d %d",&x,&y);mark[x]=1;//标记行ma[y]=1;//标记列}for (i=1;i<=n;i++)mark[i]+=mark[i-1];for (j=1;j<=m;j++)ma[j]+=ma[j-1];while (q--){scanf ("%d %d %d %d",&a,&b,&c,&d);if (c-a+1==(mark[c]-mark[a-1])||(d-b+1)==(ma[d]-ma[b-1]))printf ("Yes\n");elseprintf ("No\n");}}return 0;} 


0 0