http://acm.hdu.edu.cn/showproblem.php?pid=6514

Problem Description

Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m. 
But recently Xiaoteng found that his crops were often stolen by a group of people, so he decided to install some monitors to find all the people and then negotiate with them.
However, Xiao Teng bought bad monitors, each monitor can only monitor the crops inside a rectangle. There are p monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known. 
Xiao Teng guess that the thieves would also steal q times of crops. he also guessed the range they were going to steal, which was also a rectangle. Xiao Teng wants to know if his monitors can see all the thieves at a time.

Input

There are mutiple test cases.
Each case starts with a line containing two integers n,m(1≤n,1≤m,n×m≤107) which represent the area of the land.
And the secend line contain a integer p(1≤p≤106) which represent the number of the monitor Xiaoteng has installed. This is followed by p lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m) ,meaning the lower left corner and upper right corner of the rectangle.
Next line contain a integer q(1≤q≤106) which represent the number of times that thieves will steal the crops.This is followed by q lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m),meaning the lower left corner and upper right corner of the rectangle.

Output

For each case you should print q lines.
Each line containing YES or NO mean the all thieves whether can be seen.

Sample Input


Sample Output

YES
NO

Hint

In the picture,the red solid rectangles mean the monitor Xiaoteng installed, and the blue dotted rectangles mean the area will be stolen.

(x1,y1)为矩形左下角,(x2,y2)为矩形右下角,竖直向上为x正方向,水平向右为y正方向

题意:

在一个面积不超过n*m的矩形上,有p个矩形A,问之后的q个矩形B能否被之前的A全部覆盖。

思路:

由于n*m,p,q的范围过大,于是考虑O(n*m+p+q)的做法,即二维前缀和+差分。

对于A类矩形(x1,y1,x2,y2),我们只需要在(x1,y1),(x2+1,y2+1)处+1,在(x1,y2+1),(x2+1,y1)处-1 ,

之后对整个面积求一个前缀和,则大于0的地方就是被A类矩形覆盖的点。

把值大于0的地方变成1,再一次求一次前缀和,处理好后即可在O(1)的时间算出一个矩形内被覆盖的点的数量。
(详细见注释)

亮点:

二维数组化为一维

两次求前缀和,重新赋值

代码如下:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=1e7+;
using namespace std; int a[maxn];//二维化为一维
int n,m; void Add(int x,int y,int val)//添加标记
{
if(x>n||y>m)
return;
a[(x-)*m+y]+=val;
} int Query(int x,int y)//得到该处的值,主要用来处理边界0
{
if(x==||y==)
return ;
return a[(x-)*m+y];
} void Sum()//求前缀和
{
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
a[(i-)*m+j]+=Query(i,j-)+Query(i-,j)-Query(i-,j-);
}
}
} int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(a,,sizeof(a));
int x1,x2,y1,y2;
int p,q;
scanf("%d",&p);
while(p--)
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
Add(x1,y1,);
Add(x2+,y2+,);
Add(x1,y2+,-);
Add(x2+,y1,-);
}
Sum();//第一次求二维前缀和,a[i]>0说明该处被覆盖过
for(int i=;i<=n;i++)//将被覆盖的点重新赋值为1,便于判断
{
for(int j=;j<=m;j++)
{
if(a[(i-)*m+j])
a[(i-)*m+j]=;
}
}
Sum();//第二次求前缀和,得到的结果即为矩形内被染色的面积
scanf("%d",&q);
while(q--)
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
int ans=Query(x2,y2)-Query(x1-,y2)-Query(x2,y1-)+Query(x1-,y1-);//利用前缀和得出矩形内被染色的面积
if(ans==(x2-x1+)*(y2-y1+))//看染色的面积是否等于矩形的总面积
printf("YES\n");
else
printf("NO\n");
}
}
return ;
}

HDU-6514 Monitor(二维前缀和+差分)的更多相关文章

  1. HDU - 6514 Monitor(二维差分)

    题意 给定一个\(n×m\)的矩阵.(\(n×m <= 1e7\)). \(p\)次操作,每次可以在这个矩阵中覆盖一个矩形. \(q\)次询问,每次问一个矩形区域中,是否所有的点都被覆盖. 解析 ...

  2. [动态差分+二维前缀和][小a的轰炸游戏]

    链接:https://ac.nowcoder.com/acm/contest/317/E来源:牛客网 题目描述 小a正在玩一款即时战略游戏,现在他要用航空母舰对敌方阵地进行轰炸 地方阵地可以看做是n× ...

  3. HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)

    6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...

  4. Codeforces 1262E Arson In Berland Forest(二维前缀和+二维差分+二分)

     题意是需要求最大的扩散时间,最后输出的是一开始的火源点,那么我们比较容易想到的是二分找最大值,但是我们在这满足这样的点的时候可以发现,在当前扩散时间k下,以这个点为中心的(2k+1)2的正方形块内必 ...

  5. C - Monitor CodeForces - 846D (二维前缀和 + 二分)

    Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started ...

  6. HDU - 6336 Problem E. Matrix from Arrays (规律+二维前缀和)

    题意: for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i - j] = A[cursor]; cursor = (cu ...

  7. COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)

    题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...

  8. 二维前缀和好题hdu6514

    #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; ]; )* ...

  9. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

随机推荐

  1. Fedora 32大变化:将删除Python 2及其软件包

    导读 虽然Fedora 30还没有上市,Fedora 32直到大约一年后才上市,但我们已经知道一个很大的变化:删除Python 2和包依赖它.随着Fedora 32将于2020年上半年推出,超过了Py ...

  2. 【STM32H7教程】第50章 STM32H7的LCD控制器LTDC基础知识和HAL库API

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第50章       STM32H7的LCD控制器LTDC基础 ...

  3. 如何在Swift的代码中使用OC的代码, 在OC的代码中使用Swift的代码?

    https://www.cnblogs.com/upliver/p/5138160.html 如何在Swift的代码中使用OC的代码, 在OC的代码中使用Swift的代码? 随着苹果公司对Swift的 ...

  4. mysql自增长字段设置

    mysql版本:5.7.27 说明:表中只能设置一个自增长字段[主键.索引(其他普通字段不行))] 设置自增长开始值: ALTER TABLE table_name AUTO_INCREMENT=10 ...

  5. [转]Linux命令行上传文件到 百度网盘 bypy

    安装软件工具: apt-get install python-pip pip install requests pip install bypy 授权登陆: 执行 bypy info,显示下边信息,根 ...

  6. python中的with用法

    with是从Python2.5引入的一个新的语法,它是一种上下文管理协议,目的在于从流程图中把 try,except 和finally 关键字和 资源分配释放相关代码统统去掉,简化try….excep ...

  7. h5-transform二维变换-盾牌还原案例

    就是8张盾牌的拼图 1 <div class="transforms"> <img src="../img/dp1.png" alt=&quo ...

  8. Java基础知识点简记

    此篇主要记录(搬运)的是Java中一些常见概念的理解,大致内容如下 final.finally.finalize的区别 Java的传值或者传引用的理解 重写Override和重载Overload的理解 ...

  9. 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL

    在实际的项目中,Aras内部的方法可能并不能完全满足我们,比如Office的组件,就必须引入,那么在Aras内部的Method中,我们如何引入外部Dll文件 首先,我们新建一个Dll文件,简单的Dem ...

  10. one_day_one_linuxCmd---光标快捷操作

    <坚持每天学习一个 linux 命令,今天我们来学习 切换光标的常用命令> 摘要:最近经常使用 xshell 软件来远程连接各种机器,在 bin/bash 下输入各种命令,因为都是一些非常 ...