Picture POJ - 1177
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.
The corresponding boundary is the whole set of line segments drawn in Figure 2.
The vertices of all rectangles have integer coordinates.
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.
The corresponding boundary is the whole set of line segments drawn in Figure 2.
The vertices of all rectangles have integer coordinates.
Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
Sample Input
7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
Sample Output
228
分析:扫描线,横线的周长是每次扫描上一次的覆盖长度与这一次差的绝对值,竖线的周长是本次扫描的高度*2*线段数
代码:
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int inf = 0x3f3f3f3f; 5 const int maxn = 2e4 + 10; 6 struct edge 7 { 8 int l, r; 9 int h; 10 int pos; 11 bool operator < (const edge& a)const 12 { 13 return h < a.h; 14 } 15 edge(int a = 0, int b = 0, int c = 0, int d = 0) : l(a), r(b), h(c), pos(d){} 16 }e[5010 << 1]; 17 18 struct node 19 { 20 int l, r; 21 int flag; 22 int len; 23 int num; 24 int ll, rr; 25 }t[maxn << 2]; 26 27 bool cmp(edge &a, edge &b) 28 { 29 return a < b; 30 } 31 32 void pushup(int tar) 33 { 34 if (t[tar].flag) 35 { 36 t[tar].len = t[tar].r - t[tar].l + 1; 37 t[tar].ll = t[tar].rr = 1; 38 t[tar].num = 1; 39 } 40 else if (t[tar].l == t[tar].r) 41 t[tar].len = t[tar].ll = t[tar].rr = t[tar].num = 0; 42 else 43 { 44 t[tar].len = t[tar << 1].len + t[tar << 1 | 1].len; 45 t[tar].num = t[tar << 1].num + t[tar << 1 | 1].num - (t[tar << 1].rr & t[tar << 1 | 1].ll); 46 t[tar].ll = t[tar << 1].ll, t[tar].rr = t[tar << 1 | 1].rr; 47 } 48 } 49 50 void build(int l, int r, int tar) 51 { 52 t[tar].l = l, t[tar].r = r, t[tar].flag = 0; 53 t[tar].len = t[tar].num = 0; 54 t[tar].rr = t[tar].ll = 0; 55 if (l == r) return; 56 int mid = (l + r) >> 1; 57 build(l, mid, tar << 1); 58 build(mid + 1, r, tar << 1 | 1); 59 } 60 61 void update(int l, int r, int tar, int v) 62 { 63 if (l == t[tar].l && r == t[tar].r) 64 { 65 t[tar].flag += v; 66 pushup(tar); 67 return; 68 } 69 int mid = (t[tar].l + t[tar].r) >> 1; 70 if (r <= mid) update(l, r, tar << 1, v); 71 else if (l > mid) update(l, r, tar << 1 | 1, v); 72 else update(l, mid, tar << 1, v), update(mid + 1, r, tar << 1 | 1, v); 73 pushup(tar); 74 } 75 76 int main() 77 { 78 int n; cin >> n; 79 int tot = 0; 80 int x1, y1, x2, y2; 81 int max1, max2; 82 83 max1 = inf, max2 = -inf; 84 for (int i = 1; i <= n; i++) 85 { 86 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 87 max1 = min(max1, min(x1, x2)); 88 max2 = max(max2, max(x1, x2)); 89 e[++tot] = edge(x1, x2, y1, -1); 90 e[++tot] = edge(x1, x2, y2, 1); 91 } 92 sort(e + 1, e + 1 + tot, cmp); 93 build(max1, max2 - 1, 1); 94 95 int res = 0; 96 int last = 0; 97 98 for (int i = 1; i <= tot; i++) 99 { 100 update(e[i].l, e[i].r - 1, 1, e[i].pos); 101 res += abs(t[1].len - last); 102 res += (e[i + 1].h - e[i].h) * 2 * t[1].num; 103 last = t[1].len; 104 } 105 cout << res << endl; 106 }