HDU6514 Monitor(二维前缀和)

这道题的原理是普通的二维差分和二维前缀和,但是因为不能开二维数组,因此映射到一维就行

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e7+10;
const int mod=1e9+7;
int a[N];
int sum[N];
int n,m;
void add(int x1,int y1,int x2,int y2,int val=1){
    a[(x1)*n+y1]+=val;
    a[(x2+1)*n+y2+1]+=val;
    a[(x1)*n+y2+1]-=val;
    a[(x2+1)*n+y1]-=val;
}
void modify(){
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            a[(i)*n+j]+=a[(i)*n+j-1]+a[(i-1)*n+j]-a[(i-1)*n+j-1];
        }
    }
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if(a[i*n+j]>0)
                sum[(i)*n+j]=sum[(i)*n+j-1]+sum[(i-1)*n+j]-sum[(i-1)*n+j-1]+1;
        }
    }
}
int query(int x1,int y1,int x2,int y2){
    return sum[x2*n+y2]-sum[x2*n+y1-1]-sum[(x1-1)*n+y2]+sum[(x1-1)*n+y1-1];
}
int main(){
    //ios::sync_with_stdio(false);
    while(~scanf("%d%d",&m,&n)){
        int i,j;
        for(i=0;i<=n*m+1;i++){
            a[i]=0;
            sum[i]=0;
        }
        int p;
        scanf("%d",&p);
        for(i=1;i<=p;i++){
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            add(x1,y1,x2,y2);
        }
        modify();
        int q;
        scanf("%d",&q);
        while(q--){
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int tmp=(x2-x1+1)*(y2-y1+1);
            if(tmp==query(x1,y1,x2,y2)){
                printf("YES\n");
            }
            else{
                printf("NO\n");
            }
        }
    }
    return 0;
}
View Code

 

posted @ 2020-10-24 16:01  朝暮不思  阅读(146)  评论(0编辑  收藏  举报