zoukankan      html  css  js  c++  java
  • HDU6514Monitor(二维差分)

    题目大意:p个子矩阵被覆盖 有q个询问 (x1,y1,x2,y2),这个矩阵是否被覆盖

    题解:二维差分

    代码:

    //
    //  moniter.cpp
    //  zyh
    //
    //  Created by cumt24 on 2020/10/27.
    //  Copyright © 2020年 cumt24. All rights reserved.
    //
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define N 10000003
    using namespace std;
    
    int n,m;
    
    int a[N],sum[N];
    /*
    int id(int x,int y)
    {
        return (x-1)*m+y;
    }*/
    int id(int i, int j)
    {
        if (i == 0 || j == 0 || i > n || j > m) return 0;
        return (i-1)*m + j;
    }
    
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(a,0,sizeof(a));
            int p,q;
            scanf("%d",&p);
            for(int i=1;i<=p;i++)
            {
                int x1,x2,y1,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                a[id(x1,y1)]++;
                a[id(x1,y2+1)]--;
                a[id(x2+1,y2+1)]++;
                a[id(x2+1,y1)]--;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    sum[id(i,j)]=sum[id(i-1,j)]+sum[id(i,j-1)]-sum[id(i-1,j-1)]+a[id(i,j)];
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    sum[id(i,j)]=sum[id(i,j)]>0?1:0;
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    sum[id(i,j)]=sum[id(i,j-1)]+sum[id(i-1,j)]-sum[id(i-1,j-1)]+sum[id(i,j)];
                }
            }
            scanf("%d",&q);
            for(int i=1;i<=q;i++)
            {
                int x1,y1,x2,y2,tmp;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                tmp=sum[id(x2,y2)]-sum[id(x2,y1-1)]-sum[id(x1-1,y2)]+sum[id(x1-1,y1-1)];
                if(tmp==(abs(x1-x2)+1)*(abs(y1-y2)+1)) puts("YES");
                else puts("NO");
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Android listview addHeaderView 和 addFooterView 详解
    android NDK 生成so 文件流程-ecplice
    PopupWindow+ListView+OnItemClick点击无效
    绘制圆动画--重写view
    绘制 ToggleButton --重写view
    ProgressBar 源码
    android rectF
    ActionBar
    Android源码
    子线程更新主线程的方法-转
  • 原文地址:https://www.cnblogs.com/zzyh/p/13887945.html
Copyright © 2011-2022 走看看