ural 1147. Shaping Regions
1147. Shaping Regions
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
N opaque rectangles (1 ≤ N ≤ 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the sheet's borders. All rectangles fall within the borders of the sheet so that different figures of different colors will be seen.
The coordinate system has its origin (0, 0) at the sheet's lower left corner with axes parallel to the sheet's borders.
Input
The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle “on the bottom”. First line contains A, B and N, space separated (1 ≤ A, B ≤ 10000). Lines 2, …, N + 1 contain five integers each: llx, lly, urx, ury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is color (1 ≤ color ≤ 2500) to be placed on the white sheet. The color 1 is the same color of white as the sheet upon which the rectangles are placed.
Output
The output should contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint), ordered by increasing color. Do not display colors with no area.
Sample
input | output |
---|---|
20 20 3 2 2 18 18 2 0 8 19 19 3 8 0 10 19 4 |
1 91 2 84 3 187 4 38 |
Tags: data structures
Difficulty: 833
题意:有一张n*m的白纸,一开始颜色为1。然后有k张各种颜色的纸放在这张纸上,问最后每种颜色的数量。
分析:
显然又是经典题。
一种做法是从后往前做,用并查集维护每行是否被覆盖,总的复杂度是O(n*m),当然这里的是离散化之后的n和m。
只是要注意行和列分开离散化,否则很容易MLE
另一种做法是暴力使用切割法,几乎相当于暴力计算对于每块纸会被其他纸遮住多少。
我使用了后者,因为离散化太麻烦。
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define ft first 30 #define sd second 31 #define mk make_pair 32 33 inline int Getint() 34 { 35 int Ret = 0; 36 char Ch = ' '; 37 bool Flag = 0; 38 while(!(Ch >= '0' && Ch <= '9')) 39 { 40 if(Ch == '-') Flag ^= 1; 41 Ch = getchar(); 42 } 43 while(Ch >= '0' && Ch <= '9') 44 { 45 Ret = Ret * 10 + Ch - '0'; 46 Ch = getchar(); 47 } 48 return Flag ? -Ret : Ret; 49 } 50 51 const int N = 1010, M = 2510; 52 struct Rectangle 53 { 54 int lx, rx, uy, dy, color; 55 inline void Read() 56 { 57 scanf("%d%d%d%d%d", &lx, &dy, &rx, &uy, &color); 58 } 59 } arr[N]; 60 int width, height, n; 61 int ans[M]; 62 63 inline void Input() 64 { 65 scanf("%d%d%d", &width, &height, &n); 66 for(int i = 1; i <= n; i++) 67 arr[i].Read(); 68 } 69 70 inline int Work(int lx, int dy, int rx, int uy, int index) 71 { 72 if(lx >= rx || dy >= uy) return 0; 73 while(index <= n && ( 74 lx >= arr[index].rx || 75 rx <= arr[index].lx || 76 dy >= arr[index].uy || 77 uy <= arr[index].dy)) index++; 78 if(index > n) return (rx - lx) * (uy - dy); 79 int ret = 0; 80 ret += Work(lx, dy, min(rx, arr[index].lx), uy, index + 1); 81 lx = max(lx, min(rx, arr[index].lx)); 82 83 ret += Work(max(lx, arr[index].rx), dy, rx, uy, index + 1); 84 rx = min(rx, max(lx, arr[index].rx)); 85 86 ret += Work(lx, dy, rx, min(uy, arr[index].dy), index + 1); 87 dy = min(dy, max(uy, arr[index].dy)); 88 89 ret += Work(lx, max(dy, arr[index].uy), rx, uy, index + 1); 90 uy = min(uy, max(dy, arr[index].uy)); 91 92 return ret; 93 } 94 95 inline void Solve() 96 { 97 ans[1] = width * height; 98 for(int i = n; i >= 1; i--) 99 { 100 int area = Work(arr[i].lx, 101 arr[i].dy, 102 arr[i].rx, 103 arr[i].uy, 104 i + 1); 105 ans[arr[i].color] += area; 106 ans[1] -= area; 107 } 108 109 for(int i = 1; i < M; i++) 110 if(ans[i]) printf("%d %d\n", i, ans[i]); 111 } 112 113 int main() 114 { 115 Input(); 116 Solve(); 117 return 0; 118 }