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. 解决CGrIdCtrl合并单元格后继续拆分后的问题

    DWORD dwMergeCellCount = vMergeCells.size(); ; i < dwMergeCellCount; i++){ m_HFlexGrid.SplitCells ...

  2. 线程与进程 queue模块

    queue模块的基本用法 https://www.cnblogs.com/chengd/articles/7778506.html 模块实现了3种类型的队列,区别在于队列中条目检索的顺序不同.在FIF ...

  3. Loading PDSC Debug Description Failed for STMicroelectronics STM32Lxxxxxxx”

    今天在调程序的时候遇到这个问题 解决办法:将安装在MDK下面的文件属性由只读去掉: 成功!可以下载.

  4. prometheus配置简介

    参考网页:https://my.oschina.net/wangyunlong/blog/3060776 global: scrape_interval:             15s evalua ...

  5. MyBatis 关联查询的实现:一对多

    有2个实体:用户.订单,一个用户可以拥有多个订单,同时这多个订单属于一个用户,即一对多. user_tb: order_tb: 在“多”的一方(order)添加“一”的一方(user)的主键(user ...

  6. Linux - 安装 dotnet core 环境

    Linux -  安装 dotnet core 环境 系统环境:CentOS7 官方安装指导 https://www.microsoft.com/net/learn/get-started/linux ...

  7. c# 属性 (get、set)

    //属性是一种用于访问对象或类的特性的成员.属性可以包括字符串的长度.字体的大小.窗体的标题和客户的名称.属性是成员的自然扩展,二者都是关联类型的命名成员.namespace ConsoleAppli ...

  8. selumium 中 xpath获取文本、属性正确写法

    报错“The result of the xpath expression is: [object Attr]. It should be an element” yutube爬虫动态加载,需要用到s ...

  9. 吴裕雄--天生自然TensorFlow2教程:数据加载

    import tensorflow as tf from tensorflow import keras # train: 60k | test: 10k (x, y), (x_test, y_tes ...

  10. python期末复习—列表

    列表:列表元素需用逗号分隔,放在方括号里,如:list=['Q',15];元素类型可以是数字,单个字符,字符串,列表.字符串和字符需要用单引号引起来. 访问列表:print(列表名[0])访问第一个元 ...