【二維差分】Monitor

Monitor

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

Time Limit: 6000/3000 MS (Java/Others)????Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 600????Accepted Submission(s): 190


?

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

 

6 6 3 2 2 4 4 3 3 5 6 5 1 6 2 2 3 2 5 4 1 5 6 5

?

?

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.

?

題目大意:

有多組輸入,對于每一組輸入,先輸入兩個個整數n,m,其下一行是一個整數p,下面p行每行輸入四個整數x1,y1,x2,y2,代表著從(x1,y1)到(x2,y2)這一矩形區域被全部標記,然后輸入一個整數q,其下q行,每行輸入四個整數x1,y1,x2,y2,如果矩形區域(x1,y1)到(x2,y2)這整個矩形均被標記,則輸出YES,否則輸出NO.

解題思路:

我們可以在圖中將所有被標記過的矩形區域里面的數全部置為1,然后求一下面積的前綴和,在查詢過程中,只需根據面積前綴和求出這一區域的面積,若求出的面積與該矩形的實際面積相同,則證明這一區域被全部覆蓋,因此,在每一次輸入覆蓋區域時,我們可以通過類似差分的思想,將mapp[x1][y1],mapp[x2+1][y2+1]都加上一,將mapp[x1][y2+1],mapp[x2+1][y1]均減去一,然后求整個區域的和,對于那些mapp[i][j]>1,將其置為1,方便后面求面積的前綴和,最后求一下面積的前綴和,查詢即可。注:此題數組范圍不確定,只能根據n,m的值來開空間,否則會內存超限。

代碼:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
/*vector<int> mapp[10000010];
*/int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int n,m;while(scanf("%d %d",&n,&m)!=EOF) {/*rep(i,0,n+1) {mapp[i].clear();rep(j,0,m+1) {mapp[i].push_back(0);}}*/int **mapp;mapp=(int**)malloc(sizeof(int*)*(n+10));rep(i,0,n+1) mapp[i]=(int*)malloc(sizeof(int)*(m+10));rep(i,0,n+1) rep(j,0,m+1) mapp[i][j]=0;int p;int x1,x2,y1,y2;scanf("%d",&p);rep(i,1,p) {scanf("%d %d %d %d",&x1,&y1,&x2,&y2);mapp[x1][y1]++;mapp[x2+1][y2+1]++;mapp[x1][y2+1]--;mapp[x2+1][y1]--;}rep(i,1,n) {rep(j,1,m) {mapp[i][j]=mapp[i][j]+mapp[i-1][j]+mapp[i][j-1]-mapp[i-1][j-1];}}rep(i,1,n) {rep(j,1,m) {if(mapp[i][j]) mapp[i][j]=1;}}rep(i,1,n) {rep(j,1,m) {mapp[i][j]=mapp[i][j]+mapp[i-1][j]+mapp[i][j-1]-mapp[i-1][j-1];}}int q;scanf("%d",&q);rep(i,1,q) {scanf("%d %d %d %d",&x1,&y1,&x2,&y2);int ans=mapp[x2][y2]-mapp[x1-1][y2]-mapp[x2][y1-1]+mapp[x1-1][y1-1];if(ans==(x2-x1+1)*(y2-y1+1)) printf("YES\n");else printf("NO\n");}}return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536295.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536295.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536295.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【數學】MORE XOR

Given a sequence of nnn numbers a1,a2,?&ThinSpace;,ana_1, a_2,\cdots, a_na1?,a2?,?,an? and three functions. Define a function f(l,r)f(l,r)f(l,r) which returns ⊕a[x](l≤x≤r)\oplus a[x] (l \le x \le r)⊕a[x](l≤x≤r). The \oplus⊕ represents excl…

【數學】Element Swapping

Element Swapping Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has an integer sequence a1,a2,a3,…,ana_1,a_2,a_3,\dots,a_na1?,a2?,a3?,…,an? and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements aia_iai? an…

【二分+二維前綴和】Largest Allowed Area

Largest Allowed Area 時間限制: 1 Sec 內存限制: 128 MB 提交: 146 解決: 54 [提交] [狀態] [命題人:admin] 題目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【數學】Floating-Point Hazard

Floating-Point Hazard 時間限制: 1 Sec 內存限制: 128 MB 提交: 106 解決: 42 [提交] [狀態] [命題人:admin] 題目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【manacher】Strings in the Pocket

Strings in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found two strings ss1s2…snss_1s_2\dots s_nss1?s2?…sn? and tt1t2…tntt_1t_2\dots t_ntt1?t2?…tn? in his left pocket, where sis_isi? indicates the iii-th character in…

【并查集+dp】Team

Team 時間限制: 1 Sec 內存限制: 128 MB 提交: 124 解決: 10 [提交] [狀態] [命題人:admin] 題目描述 ACM-ICPC is a interesting game. A team takes part in this game must consist of exactly (no more and no less) three players. Every year, many new members wil…

【線段樹】Segment Tree

Segment Tree 時間限制: 1 Sec 內存限制: 512 MB 提交: 107 解決: 23 [提交] [狀態] [命題人:admin] 題目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satis?ed that the maximum c can reach w…

UVA1025——A Spy in the Metro【dp】

題目鏈接&#xff1a;https://cn.vjudge.net/problem/UVA-1025 題目大意&#xff1a;Mario從第1站出發&#xff0c;目的是在時刻T會見車站 nnn 的一個間諜。由于在車站等待容易被抓&#xff0c;所以應盡量躲在開動的火車上&#xff0c;即在車站等待的時間最短&#xff0c;且Ma…

HDU1284——錢幣兌換問題【dp】

錢幣兌換問題 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14528 Accepted Submission(s): 8784 Problem Description 在一個國家僅有1分&#xff0c;2分&#xff0c;3分硬幣&#xff0c;將錢N兌換成硬…

HDU6092——Rikka with Subset 【dp】

題目網址&#xff1a; https://vjudge.net/problem/HDU-6092 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has nn positive A1?AnA1?An and their sum is mm…

洛谷P1725琪露諾【單調隊列+dp】

題目描述 在幻想鄉&#xff0c;琪露諾是以笨蛋聞名的冰之妖精。 某一天&#xff0c;琪露諾又在玩速凍青蛙&#xff0c;就是用冰把青蛙瞬間凍起來。但是這只青蛙比以往的要聰明許多&#xff0c;在琪露諾來之前就已經跑到了河的對岸。于是琪露諾決定到河岸去追青蛙。 小河可以看…

扶桑號戰列艦【RMQ+分治】

扶桑號戰列艦 時間限制: 1 Sec 內存限制: 128 MB Special Judge 提交: 197 解決: 63 [提交] [狀態] [命題人:admin] 題目描述 眾所周知&#xff0c;一戰過后&#xff0c;在世界列強建造超無畏級戰列艦的競爭之中&#xff0c;舊日本海軍根據“個艦優越主義”&#xff0c;建造了扶…

大鳳號裝甲空母【找規律+矩陣快速冪】

大鳳號裝甲空母 時間限制: 1 Sec 內存限制: 128 MB 提交: 108 解決: 15 [提交] [狀態] [命題人:admin] 題目描述 大鳳號航空母艦很喜歡算術。 它&#xff0c;是舊日本海軍中最為先進的航空母艦。 它&#xff0c;是舊日本海軍中最為短命的航空母艦。 同時&#xff0c;她還是最平…

codevs5429 多重背包【多重背包+單調隊列】

5429 多重背包 時間限制: 1 s 空間限制: 256000 KB 題目等級 : 鉆石 Diamond 題目描述 Description 你有一個容量為M的背包&#xff0c;和N種物品。 每種物品都有三個屬性&#xff0c;vi&#xff0c;wi&#xff0c;與ci&#xff0c;分別表示這種物品的體積、價值和件數。 你的…

generator 1【矩陣快速冪】

題目描述 You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0?,x1?,a,b. And you know xia?xi?1b?xi?2x_i a \cdot x_{i-1} b \cdot x_{i-2}xi?a?xi?1?b?xi?2? for all i≥2i \ge 2i≥2. Given two positive integers n, and MOD, please calcul…

Give Candies【快速冪+歐拉】

Give Candies 時間限制: 1 Sec 內存限制: 128 MB 提交: 243 解決: 92 [提交] [狀態] [命題人:admin] 題目描述 There are N children in kindergarten. Miss Li bought them N candies。To make the process more interesting, Miss Li comes up with the rule: All the childr…

Save the Room【找規律】

Save the Room 時間限制: 1 Sec 內存限制: 128 MB 提交: 149 解決: 90 [提交] [狀態] [命題人:admin] 題目描述 Bob is a sorcerer. He lives in a cuboid room which has a length of A, a width of B and a height of C, so we represent it as ABC. One day, he finds that …

Participate in E-sports【Java大數+二分】

Participate in E-sports 時間限制: 2 Sec 內存限制: 128 MB 提交: 194 解決: 53 [提交] [狀態] [命題人:admin] 題目描述 Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to…

Poor God Water【矩陣快速冪】

Poor God Water 時間限制: 1 Sec 內存限制: 128 MB 提交: 102 解決: 50 [提交] [狀態] [命題人:admin] 題目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonou…

Transport Ship【多重背包】

Transport Ship 時間限制: 1 Sec 內存限制: 128 MB 提交: 175 解決: 65 [提交] [狀態] [命題人:admin] 題目描述 There are N different kinds of transport ships on the port. The i^th kind of ship can carry the weight of V[i] and the number of the ith kind of ship i…