标签:
Description
Input
Output
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
Source
1 /* 2 元稹 3 《离思五首·其四》 4 5 曾经沧海难为水,除却巫山不是云。 6 取次花丛懒回顾,半缘修道半缘君。 7 */ 8 #include <iostream> 9 #include <cstdio> 10 #include <algorithm> 11 #include <cstring> 12 #include <vector> 13 #include <utility> 14 #include <iomanip> 15 #include <string> 16 #include <cmath> 17 #include <queue> 18 #include <assert.h> 19 #include <map> 20 #include <ctime> 21 #include <cstdlib> 22 #include <stack> 23 #define LOCAL 24 const int MAXN = 10000 + 10; 25 const int MAXM = 75 + 10; 26 const int INF = 100000000; 27 const int SIZE = 450; 28 const int maxnode = 0x7fffffff + 10; 29 using namespace std; 30 struct Line{//扫描线 31 int flag;//表示是出线还是入线 32 double x, y1, y2; 33 34 Line (double a, double b, double c, double d){ 35 flag = (int)d; 36 x = a; 37 y1 = b; 38 y2 = c; 39 } 40 bool operator < (const Line &b)const{ 41 return x < b.x; 42 }; 43 }; 44 struct Seg_Tree{ 45 struct Node{ 46 int l, r, flag; 47 double ll, rr, len; 48 }tree[2000]; 49 vector<double>y, data; 50 vector<Line>line; 51 map<double, int>Map;//离散化 52 53 void update(int t, int s, int e, int val){ 54 int l = tree[t].l, r = tree[t].r; 55 if ((l + 1) == r) { 56 tree[t].flag += val; 57 if (tree[t].flag == 0) tree[t].len = 0; 58 else tree[t].len = tree[t].rr - tree[t].ll; 59 } 60 else{ 61 int mid = (l + r)>>1; 62 if (s < mid) update(t << 1, s, e, val); 63 if (e > mid) update((t << 1) | 1, s, e, val); 64 tree[t].len = tree[t << 1].len + tree[(t << 1) | 1].len; 65 66 } 67 } 68 void build(int t, int l, int r){ 69 tree[t].l = l; 70 tree[t].r = r; 71 tree[t].flag = 0; 72 tree[t].len = 0; 73 //这两个是代表真实的值 74 tree[t].ll = y[l]; 75 tree[t].rr = y[r]; 76 if ((l + 1) == r) return; 77 //左闭右开 78 int mid = (l + r)>>1; 79 build((t<<1), l, mid); 80 build((t<<1) | 1, mid, r); 81 } 82 void init(){ 83 Map.clear();; 84 line.clear(); 85 y.clear(); 86 data.clear(); 87 } 88 }A; 89 int n; 90 91 void init(){ 92 A.init(); 93 for (int i = 1; i <= n; i++){ 94 double x1, x2, y1, y2; 95 scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); 96 A.line.push_back(Line(x1, y1, y2, 1));//1代表入线 97 A.line.push_back(Line(x2, y1, y2, -1)); 98 A.data.push_back(y1); 99 A.data.push_back(y2); 100 } 101 int cnt = 0;//离散化 102 sort(A.data.begin(), A.data.end()); 103 for (int i = 0; i < A.data.size(); i++){ 104 if (i == 0 || A.data[i] != A.data[i - 1]){ 105 A.Map[A.data[i]] = cnt++; 106 A.y.push_back(A.data[i]); 107 } 108 } 109 } 110 void work(){ 111 sort(A.line.begin(), A.line.end()); 112 A.build(1, 0, A.y.size() - 1); 113 double Ans = 0; 114 for (int i = 0; i < A.line.size(); i++){ 115 if (i != 0) Ans += (A.line[i].x - A.line[i - 1].x) * A.tree[1].len; 116 A.update(1, A.Map[A.line[i].y1], A.Map[A.line[i].y2], A.line[i].flag); 117 } 118 printf("Total explored area: %.2lf\n\n" , Ans); 119 } 120 121 int main(){ 122 123 int t = 0; 124 while (scanf("%d", &n) && n){ 125 printf("Test case #%d\n", ++t); 126 init(); 127 work(); 128 } 129 return 0; 130 }
标签:
原文地址:http://www.cnblogs.com/hoskey/p/4334247.html