Bestcoders 57 1002

Conturbatio

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


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.
 

Source
BestCoder Round #57 (div.2)


两种解法:

I.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
#define N 100000 + 10

int n, m, k, q;

struct Tree {
    int c[N], maxn;
    void init(int n) { maxn = n; for (int i = 0; i <= n; i++)c[i] = 0; }
    int lowbit(int x) { return x&-x; }
    int sum(int x) {
        int ans = 0;
        while (x)ans += c[x], x -= lowbit(x);
        return ans;
    }
    void update(int pos, int val) {
        while (pos <= maxn)c[pos] += val, pos += lowbit(pos);
    }
}TX, TY;

int vx[N], vy[N];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(vx, 0, sizeof vx);
        memset(vy, 0, sizeof vy);
        scanf("%d%d%d%d", &n, &m, &k, &q);
        TX.init(n);
        TY.init(m);
        int x, y;
        for(int i = 0; i < k; i++)
        {
            scanf("%d%d", &x, &y);
            if(!vx[x])
            {
                TX.update(x, 1);
                vx[x] = 1;
            }
            if(!vy[y])
            {
                TY.update(y, 1);
                vy[y] = 1;
            }
        }
        int x1, y1, flag;
        for(int i = 0; i < q; i++)
        {
            scanf("%d%d%d%d", &x, &y, &x1, &y1);
            if((TX.sum(x1) - TX.sum(x - 1) == (x1 - x + 1)) || (TY.sum(y1) - TY.sum(y - 1) == (y1 - y + 1)))
                printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}

/*


10
5 5 3 100
1 1 1 2 5 5



*/


II.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
#define N 100000 + 10

int n, m, k, q;
int vx[N], vy[N];
int sx[N], sy[N];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d%d", &n, &m, &k, &q);

        for(int i = 0; i <= n; i++)
        {
            vx[i] = sx[i] = 0;
        }
        for(int i = 0; i <= m; i++)
        {
            vy[i] = sy[i] = 0;
        }
        int x, y;
        for(int i = 0; i < k; i++)
        {
            scanf("%d%d", &x, &y);
            if(!vx[x])
            {
                vx[x] = 1;
            }
            if(!vy[y])
            {
                vy[y] = 1;
            }
        }
        for(int i = 1; i <= n; i++)
            sx[i] += sx[i - 1] + vx[i];
        for(int i = 1; i <= m; i++)
            sy[i] += sy[i - 1] + vy[i];

        int x1, y1, flag;
        for(int i = 0; i < q; i++)
        {
            scanf("%d%d%d%d", &x, &y, &x1, &y1);
            if((sx[x1] - sx[x - 1] == (x1 - x + 1)) || (sy[y1] - sy[y - 1] == (y1 - y + 1)))
                printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}

/*


10
5 5 3 100
1 1 1 2 5 5



*/



你可能感兴趣的:(ACM)