HDU-6514 Monitor(二维前缀和+差分)
http://acm.hdu.edu.cn/showproblem.php?pid=6514
Problem Description
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
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
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(二维前缀和+差分)的更多相关文章
- HDU - 6514 Monitor(二维差分)
题意 给定一个\(n×m\)的矩阵.(\(n×m <= 1e7\)). \(p\)次操作,每次可以在这个矩阵中覆盖一个矩形. \(q\)次询问,每次问一个矩形区域中,是否所有的点都被覆盖. 解析 ...
- [动态差分+二维前缀和][小a的轰炸游戏]
链接:https://ac.nowcoder.com/acm/contest/317/E来源:牛客网 题目描述 小a正在玩一款即时战略游戏,现在他要用航空母舰对敌方阵地进行轰炸 地方阵地可以看做是n× ...
- HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)
6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...
- Codeforces 1262E Arson In Berland Forest(二维前缀和+二维差分+二分)
题意是需要求最大的扩散时间,最后输出的是一开始的火源点,那么我们比较容易想到的是二分找最大值,但是我们在这满足这样的点的时候可以发现,在当前扩散时间k下,以这个点为中心的(2k+1)2的正方形块内必 ...
- C - Monitor CodeForces - 846D (二维前缀和 + 二分)
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started ...
- 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 ...
- COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)
题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...
- 二维前缀和好题hdu6514
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; ]; )* ...
- 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 ...
随机推荐
- 每天一点点之laravel框架开发 - API通过access_token获取用户id报 Unauthenticated. 错误(passport)
1.首先保证你的config/auth.php 中 guards 的 api 的 driver 选项改为 passport 2.注册中间件,在 app/Http/Kernel.php 文件中的 $ro ...
- Java算法练习——正则表达式匹配
题目链接 题目描述 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符 '*' 匹配零个或多个前面的那一个元素 所谓匹配,是要 ...
- 字符串匹配之BF算法
1)算法原理 BF(Brute Force)算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符:若不相等 ...
- vps 跑流量
- 了解OOM
1)什么是OOM? OOM,全称“Out Of Memory”,翻译成中文就是“内存用完了”,来源于java.lang.OutOfMemoryError.看下关于的官方说明: Thrown when ...
- 浅谈无字母数字构造webshell
0x00 问题 <?php include 'flag.php'; if(isset($_GET['code'])){ $code = $_GET['code']; if(strlen($cod ...
- POJ - 3662 Telephone Lines (dijstra+二分)
题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少. 分析: 1.二分临界线(符合的情况的点在右边),找可能的 ...
- python导出oracle中的表内容,并生成excel文件
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK; ### 如果oracle表中有中文输出,为防止乱码,执行脚本前,需要先制定字符集: #!/usr/bin/pyth ...
- 面试题:你使用过concurrent包下的那些类?
1.executor接口,使用executor接口的子接口ExecutorService用来创建线程池2.Lock接口下的ReentrantLock类,实现同步,比如三个线程循环打印ABCABCABC ...
- 《C Primer Plus》- 第一章 初试C语言
本笔记写于2020年1月25日. 从今天开始,我要全面的.彻底的将未来计划中所有的知识重新规划学习一遍,并整理成一套全面的笔记体系.为我将来的职业打下坚实的基础.而所有的一切从C语言开始. 本系列文章 ...