杭电acm5480

来源:互联网 发布:淘宝类目数据查询 编辑:程序博客网 时间:2024/05/22 15:23
Conturbatio
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 461 Accepted Submission(s): 212


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


解题思路:本题既可以用线段树做,也可以用前缀和做,首先来分析如何用线段树做。首先在建树的过程中先初始化横纵坐标a,b为1,然后对于有车的横纵坐标改为0,并找出最大值。接着就可以根据具体情况来输出yes和no.

线段树代码:

#include <iostream>#include <cstdio>#include <cstring>const int maxn=100000;int a[maxn],b[maxn];struct node{    int l,r,ma;}tree1[maxn<<2],tree2[maxn<<2];int max1(int a,int b){return a>b?a:b;}void build1(int l,int r,int p){    tree1[p].l = l;    tree1[p].r = r;    tree1[p].ma = -1;    if(l == r)    {        tree1[p].ma = a[l];        return ;    }    int mid = (l+r)>>1;    build1(l,mid,p<<1);    build1(mid+1,r,p<<1|1);    tree1[p].ma = max1(tree1[p<<1].ma,tree1[p<<1|1].ma);}int query1(int x,int y,int p){    if(x == tree1[p].l && y == tree1[p].r)        return tree1[p].ma;    int mid = (tree1[p].l+tree1[p].r)>>1;    if(x>mid)        return query1(x,y,p<<1|1);    else if(y<=mid)        return query1(x,y,p<<1);    else        return max1(query1(x,mid,p<<1),query1(mid+1,y,p<<1|1));}void build2(int l,int r,int p){    tree2[p].l = l;    tree2[p].r = r;    tree2[p].ma = -1;    if(l == r)    {        tree2[p].ma = b[l];        return;    }    int mid = (l+r)>>1;    build2(l,mid,p<<1);    build2(mid+1,r,p<<1|1);    tree2[p].ma = max1(tree2[p<<1].ma,tree2[p<<1|1].ma);}int query2(int x,int y,int p){    if(x == tree2[p].l && y == tree2[p].r)        return tree2[p].ma;    int mid = (tree2[p].l+tree2[p].r)>>1;    if(x>mid)        return query2(x,y,p<<1|1);    else if(y<=mid)        return query2(x,y,p<<1);    else        return max1(query2(x,mid,p<<1),query2(mid+1,y,p<<1|1));}int main(){    int t,n,m,k,q;    scanf("%d",&t);    while(t--)    {        scanf("%d %d %d %d",&n,&m,&k,&q);        for(int i=1;i<=n;i++)            a[i] = 1;        for(int i=1;i<=m;i++)            b[i] = 1;        while(k--)        {            int x,y;            scanf("%d %d",&x,&y);            a[x]=b[y]=0;        }        build1(1,n,1);        build2(1,m,1);        while(q--)        {            int x1,y1,x2,y2;            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);            if(!query1(x1,x2,1) || !query2(y1,y2,1))                puts("Yes");            else                puts("No");        }    }    return 0;}
方法二:对于找规律,应该是先初始化两个数组为0,再对其中车的坐标更改为1,最后进行叠加。

这种方法用c++交会判超时,用G++交才ac

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 100000;int xx[maxn],yy[maxn];int t,n,m,k,q;int main(){    scanf("%d",&t);    while(t--)    {        memset(xx,0,sizeof(xx));        memset(yy,0,sizeof(yy));        scanf("%d %d %d %d",&n,&m,&k,&q);        for(int i=0;i<k;i++)        {            int a,b;            scanf("%d %d",&a,&b);            xx[a]=yy[b]=1;        }        for(int i=1;i<=n;i++)            xx[i]+=xx[i-1];        for(int i=1;i<=m;i++)            yy[i]+=yy[i-1];        while(q--)        {            int x1,y1,x2,y2;            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);            if(x2-x1+1==xx[x2]-xx[x1-1]||y2-y1+1==yy[y2]-yy[y1-1])                printf("Yes\n");            else                printf("No\n");        }    }    return 0;}


0 0
原创粉丝点击