hdoj 5480 Conturbatio 【思维】

题目链接:hdoj 5480 Conturbatio

Conturbatio

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

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.

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
Yes

Hint
Huge input, scanf recommended.

题意:在n*m的格子上有k个士兵,每个士兵可以攻击到他所在行与列的每一个格子。现在有q次查询,每次给定一个矩形,问该矩形里面的所有格子能否被全部攻击到。

思路:先标记第i行、列是否被覆盖,再统计前i行、列被覆盖的个数。对于给定的矩形,若它的行或者列全部被覆盖则说明该矩形被全部覆盖。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <stack>
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
void add(LL &x, LL y) { x += y; x %= MOD; }
int row[MAXN], cul[MAXN];
int srow[MAXN], scul[MAXN];
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n, m, k, q;
        scanf("%d%d%d%d", &n, &m, &k, &q);
        CLR(row, 0); CLR(cul, 0);
        for(int i = 1; i <= k; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            row[x] = 1; cul[y] = 1;
        }
        srow[0] = 0;
        for(int i = 1; i <= n; i++) {
            srow[i] = srow[i-1] + row[i];
        }
        scul[0] = 0;
        for(int i = 1; i <= m; i++) {
            scul[i] = scul[i-1] + cul[i];
        }
        while(q--) {
            int x1, y1, x2, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            if(x1 < x2) swap(x1, x2);
            if(y1 > y2) swap(y1, y2);
            if(scul[y2] - scul[y1-1] == y2 - y1 + 1 || srow[x1] - srow[x2-1] == x1 - x2 + 1) {
                printf("Yes\n");
            }
            else {
                printf("No\n");
            }
        }
    }
    return 0;
}

你可能感兴趣的:(hdoj 5480 Conturbatio 【思维】)