BestCoder Round #57 (div.2)1002

Conturbatio

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


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.
 

其实也是一个简单题;
这个题目最大的问题是在询问,每个询问要在O(logn)或O(1)时间完成
一开始以为是树状数组,当后来发现数组开不了那么大
询问那个矩形内是否会受到攻击,就是看它的每一行有一个车或它的每一列有一个车。
如果有就输出Yes,否者输出No
这样就很简单了,直接用两个数组标记那些行和列有车,然后得到其前缀和(用来判断它的每一行是否有一个车或每一列是否有一个车)
hang[i]表示前i行车的个数
lie[i]表示前i列车的个数
对于x1,y1,x2,y2这个矩形
只要判断hang[y2]-hang[y1-1] == y2-y1+1 和 lie[x2]-lie[x1-1] == x2-x1+1有一个成立就输出Yes否者输出No
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
using namespace std;

const int N = 100001;
int lie[N];
int hang[N];

int main(void)
{
    int T;
    int n, m, k, q;
    scanf("%d", &T);
    while(T--)
    {
        int i;
        scanf("%d%d%d%d", &n, &m, &k, &q);
        memset(lie, 0, sizeof(lie));
        memset(hang, 0, sizeof(hang));
        while(k--)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            hang[y] = 1;
            lie[x] = 1;
        }
        for(i = 1; i <= m; i++)
            lie[i] = lie[i-1]+lie[i];
        for(i = 1; i <= n; i++)
            hang[i] = hang[i-1]+hang[i];
        while(q--)
        {
            int x1, x2, y1, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            int flag = 0;
            if(lie[x2] - lie[x1-1] == x2-x1+1) flag = 1;
            if(hang[y2] - hang[y1-1] == y2-y1+1) flag = 1;

            if(flag) printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值