【HDU - 6514】Monitor(二维差分,前缀和)
題干:
Monitor
Time Limit: 6000/3000 MS (Java/Others)????Memory Limit: 163840/163840 K (Java/Others)
 Total Submission(s): 872????Accepted Submission(s): 145
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.
題目大意:
? ?給你p個紅色矩形局域,再給你q次詢問(每次一個藍色區域),每次問你能否藍色區域被紅色區域完全覆蓋。
解題報告:
? ?考慮暴力的做法,對于每一個紅色區域,每一行都做標記差分,這樣預處理的復雜度是O(pn),顯然會T,考慮優化掉這個n,那就是進行第二次差分,最后就可以得到真值(每一個點被覆蓋的次數)。然后xjb搞一下答案就可以了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; int n,m,p,q; int main() {while(~scanf("%d%d",&n,&m)) { vector<vector<ll> > vv(m+14,vector<ll>(n+14,0));vector<vector<ll> > sum(m+14,vector<ll>(n+14,0));scanf("%d",&p);for(int x1,x2,y1,y2,i = 1; i<=p; i++) {scanf("%d%d%d%d",&y1,&x1,&y2,&x2);vv[x1][y1]++;vv[x1][y2+1]--;vv[x2+1][y1]--;vv[x2+1][y2+1]++;}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {vv[j][i] += vv[j-1][i];}}for(int i = 1; i<=m; i++) {for(int j = 1; j<=n; j++) {vv[i][j] += vv[i][j-1];}}for(int i = 1; i<=m; i++) {for(int j = 1; j<=n; j++) {if(vv[i][j] >= 1) vv[i][j] = 1;else vv[i][j] = 0;}}for(int i = 1; i<=m; i++) {for(int j = 1; j<=n; j++) {sum[i][j] = vv[i][j] + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];} }scanf("%d",&q);while(q--) {int x1,x2,y1,y2;scanf("%d%d%d%d",&y1,&x1,&y2,&x2);ll tmp = sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1];if(tmp == 1LL*(x2-x1+1)*(y2-y1+1)) puts("YES");else puts("NO");}} return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 6514】Monitor(二维差分,前缀和)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 青岛暴雨 一奔驰4S店展厅变瀑布!路上有
 - 下一篇: 坦克300绝配!长城上线纯电越野自行车: