啥都不会啊!怎么办啊!

Fitz

慢慢来生活总会好起来的!!!

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. 

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.

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

这题写了很久很久
有点难受

现在还是有点迷糊

  1 #include <iostream>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <cmath>
  5 #include <cstdio>
  6 using namespace std;
  7 typedef long long LL;
  8 const int maxn = 5e4 + 10;
  9 #define rtl  rt<<1
 10 #define rtr  rt<<1|1
 11 #define bug  printf("*******");
 12 struct LINE {
 13     int x, y1, y2, flag;
 14 } line[maxn];
 15 int cmp(LINE a, LINE b) {
 16     if (a.x == b.x) return a.flag > b.flag;
 17     return a.x < b.x;
 18 }
 19 struct node {
 20     int l, r, line, cover;
 21     int m, lbd, rbd;
 22 } tree[maxn << 2];
 23 int y[maxn];
 24 void build(int l, int r, int rt) {
 25     tree[rt].l = l, tree[rt].r = r;
 26     tree[rt].cover = tree[rt].m = tree[rt].line = 0;
 27     tree[rt].lbd = tree[rt].rbd = 0;
 28     if (r - l > 1) {
 29         int m = (l + r) >> 1;
 30         build(l, m, rtl);
 31         build(m, r, rtr);
 32     }
 33 }
 34 void update_line(int rt) {
 35     if (tree[rt].cover > 0) {
 36         tree[rt].lbd = tree[rt].rbd = tree[rt].line = 1;
 37     } else if (tree[rt].r - tree[rt].l == 1) {
 38         tree[rt].lbd = tree[rt].rbd = tree[rt].line = 0;
 39     } else {
 40         tree[rt].lbd = tree[rtl].lbd;
 41         tree[rt].rbd = tree[rtr].rbd;
 42         tree[rt].line = tree[rtl].line + tree[rtr].line - tree[rtl].rbd * tree[rtr].lbd;
 43     }
 44 }
 45 void update_m(int rt) {
 46     if (tree[rt].cover > 0)   tree[rt].m = y[tree[rt].r] - y[tree[rt].l];
 47     else if (tree[rt].r - tree[rt].l == 1) tree[rt].m = 0;
 48     else tree[rt].m = tree[rtl].m + tree[rtr].m;
 49 
 50 }
 51 void add(int l, int r, int rt) {
 52     if (y[tree[rt].l] >= l && y[tree[rt].r] <= r) tree[rt].cover++;
 53     else if (tree[rt].r - tree[rt].l == 1) return ;
 54     else {
 55         int m = (tree[rt].l + tree[rt].r) >> 1;
 56         if (r <= y[m]) add(l, r, rtl);
 57         else if (l > y[m]) add(l, r, rtr);
 58         else {
 59             add(l, y[m], rtl);
 60             add(y[m], r, rtr);
 61         }
 62     }
 63     update_line(rt);
 64     update_m(rt);
 65 }
 66 void del(int l, int r, int rt) {
 67     if (y[tree[rt].l] >= l && y[tree[rt].r] <= r) tree[rt].cover--;
 68     else if (tree[rt].r - tree[rt].l == 1) return;
 69     else {
 70         int m = (tree[rt].l + tree[rt].r) >> 1;
 71         if (r <= y[m]) del(l, r, rtl);
 72         else if (l > y[m]) del(l, r, rtr);
 73         else {
 74             del(l, y[m], rtl);
 75             del(y[m], r, rtr);
 76         }
 77     }
 78     update_line(rt);
 79     update_m(rt);
 80 }
 81 int main() {
 82     int n, cnt = 0;
 83     scanf("%d", &n);
 84     for (int i = 0 ; i < n ; i++) {
 85         int x1, y1, x2, y2;
 86         scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
 87         line[cnt].x = x1;
 88         line[cnt].y1 = y1;
 89         line[cnt].y2 = y2;
 90         line[cnt].flag = 1;
 91         y[cnt++] = y1;
 92         line[cnt].x = x2;
 93         line[cnt].y1 = y1;
 94         line[cnt].y2 = y2;
 95         line[cnt].flag = 0;
 96         y[cnt++] = y2;
 97     }
 98     sort(y, y + cnt);
 99     sort(line, line + cnt, cmp);
100     int len = unique(y, y + cnt) - y;
101     build(0, len - 1, 1);
102     int ans = 0, now_m = 0, now_line = 0;
103     for (int i = 0 ; i < cnt ; i++) {
104         if (line[i].flag) add(line[i].y1, line[i].y2, 1);
105         else del(line[i].y1, line[i].y2, 1);
106         if (i >= 1) ans += 2 * now_line * (line[i].x - line[i - 1].x);
107         ans += abs(tree[1].m - now_m);
108         now_m = tree[1].m;
109         now_line = tree[1].line;
110     }
111     printf("%d\n", ans);
112     return 0;
113 }

 

posted @ 2018-07-29 23:09  Fitz~  阅读(388)  评论(0编辑  收藏  举报