标签:style blog http color os io for ar
题目链接
题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标。
分析:
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt 9 #define rson mid+1, r, 2*rt+1 10 const int maxn = 200+10; 11 using namespace std; 12 int n; 13 double y[maxn]; 14 struct node 15 { 16 int l, r, c; 17 double cnt, lf, rf; 18 }tr[4*maxn]; 19 struct Line 20 { 21 double x, y1, y2; 22 int f; 23 }line[maxn]; 24 bool cmp(Line a, Line b) 25 { 26 return a.x < b.x; 27 } 28 void build(int l, int r, int rt) 29 { 30 tr[rt].l = l; tr[rt].r = r; 31 tr[rt].cnt = tr[rt].c = 0; 32 tr[rt].lf = y[l]; tr[rt].rf = y[r]; 33 if(l+1==r) return; 34 int mid = (l+r)/2; 35 build(l, mid, 2*rt); 36 build(mid, r, 2*rt+1); //注意是mid,不是mid+1 37 } 38 void calen(int rt) 39 { 40 if(tr[rt].c>0) 41 { 42 tr[rt].cnt = tr[rt].rf-tr[rt].lf; 43 return; 44 } 45 if(tr[rt].l+1==tr[rt].r) tr[rt].cnt = 0; 46 else tr[rt].cnt = tr[2*rt].cnt+tr[2*rt+1].cnt; 47 } 48 void update(int rt, Line e) 49 { 50 if(e.y1==tr[rt].lf && e.y2==tr[rt].rf) 51 { 52 tr[rt].c += e.f; 53 calen(rt); 54 return; 55 } 56 if(e.y2<=tr[2*rt].rf) update(2*rt, e); 57 else if(e.y1>=tr[2*rt+1].lf) update(2*rt+1, e); 58 else 59 { 60 Line tmp = e; 61 tmp.y2 = tr[2*rt].rf; 62 update(2*rt, tmp); 63 tmp = e; 64 tmp.y1 = tr[2*rt+1].lf; 65 update(2*rt+1, tmp); 66 } 67 calen(rt); 68 } 69 int main() 70 { 71 int i, ca=1, cnt; 72 double x1, x2, y1, y2, ans; 73 while(~scanf("%d", &n)&&n) 74 { 75 cnt = 1; ans = 0; 76 for(i = 0; i < n; i++) 77 { 78 scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); 79 line[cnt].x = x1; line[cnt].y1 = y1; 80 line[cnt].y2 = y2; line[cnt].f = 1; 81 y[cnt++] = y1; 82 line[cnt].x = x2; line[cnt].y1 = y1; 83 line[cnt].y2 = y2; line[cnt].f = -1; 84 y[cnt++] = y2; 85 } 86 cnt--; 87 sort(line+1, line+cnt+1, cmp); 88 sort(y+1, y+cnt+1); 89 build(1, cnt, 1); 90 91 update(1, line[1]); 92 for(i = 2; i <= cnt; i++) 93 { 94 ans += tr[1].cnt*(line[i].x-line[i-1].x); 95 update(1, line[i]); 96 } 97 printf("Test case #%d\n", ca++); 98 printf("Total explored area: %.2lf\n\n", ans); 99 } 100 return 0; 101 }
poj 1151 Atlantis (离散化 + 扫描线 + 线段树),布布扣,bubuko.com
poj 1151 Atlantis (离散化 + 扫描线 + 线段树)
标签:style blog http color os io for ar
原文地址:http://www.cnblogs.com/bfshm/p/3900216.html