标签:uri anti contains sample sts end ram ons set
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #define M(a, b) memset(a, b, sizeof(a)) 8 #define lson o<<1 9 #define rson o<<1|1 10 const int N = 200 + 5; 11 int cnt[N<<2], qL, qR, v; 12 double X[N], sum[N<<2]; 13 struct Segment { 14 double l, r, h; 15 int f; 16 Segment() {} 17 Segment(double l, double r, double h, int f): l(l), r(r), h(h), f(f) {} 18 bool operator < (const Segment &rhs) const { 19 return h < rhs.h; 20 } 21 }node[N]; 22 23 void pushdown(int o, int L, int R) { 24 if (~cnt[o]) { 25 int M = (L + R) >> 1; 26 cnt[lson] = cnt[rson] = cnt[o]; 27 sum[lson] = cnt[lson] ? X[M+1]-X[L] : 0; 28 sum[rson] = cnt[rson] ? X[R+1]-X[M+1] : 0; 29 } 30 } 31 32 void pushup(int o, int L, int R) { 33 if (~cnt[lson] || ~cnt[rson]) cnt[o] = -1; 34 else if (cnt[lson] != cnt[rson]) cnt[o] = -1; 35 else { 36 cnt[o] = cnt[lson]; 37 } 38 sum[o] = sum[lson] + sum[rson]; 39 } 40 41 void build(int o, int L, int R) { 42 if (L == R) {cnt[o] = 0; sum[o] = 0;} 43 else { 44 int M = (L + R) >> 1; 45 build(lson, L, M); 46 build(rson, M+1, R); 47 pushup(o, L, R); 48 } 49 } 50 51 void update(int o, int L, int R) { 52 if (qL <= L && R <= qR) { 53 if (~cnt[o]) { 54 cnt[o] += v; 55 sum[o] = cnt[o] ? X[R+1]-X[L] : 0; 56 return; 57 } 58 } 59 int M = (L + R) >> 1; 60 pushdown(o, L, R); 61 if (qL <= M) update(lson, L, M); 62 if (M < qR) update(rson, M+1, R); 63 pushup(o, L, R); 64 } 65 66 int bin(int L, int R, double key) { 67 while (L < R) { 68 int M = (L + R) >> 1; 69 if (X[M] == key) return M; 70 else if (X[M] < key) L = M + 1; 71 else R = M - 1; 72 } 73 return L; 74 } 75 76 int main() { 77 int T = 0, n; 78 while (scanf("%d", &n), n) { 79 double x1, x2, y1, y2; 80 int num = 0, nn = 0; 81 for (int i = 1; i <= n; ++i) { 82 scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); 83 X[++num] = x1; X[++num] = x2; 84 node[++nn] = Segment(x1, x2, y1, 1); 85 node[++nn] = Segment(x1, x2, y2, -1); 86 } 87 sort(X+1, X+1+num); sort(node+1, node+1+nn); 88 int k = 1; 89 for (int i = 2; i <= nn; ++i) 90 if (X[i] != X[i-1]) X[++k] = X[i]; 91 build(1, 1, k-1); 92 double ans = 0; 93 for (int i = 1; i < nn; ++i) { 94 qL = bin(1, k, node[i].l); 95 qR = bin(1, k, node[i].r)-1; 96 v = node[i].f; 97 if(qL <= qR) update(1, 1, k-1); 98 ans += sum[1] * (node[i+1].h-node[i].h); 99 } 100 printf("Test case #%d\n", ++T); 101 printf("Total explored area: %.2f\n\n", ans); 102 } 103 104 return 0; 105 }
标签:uri anti contains sample sts end ram ons set
原文地址:http://www.cnblogs.com/robin1998/p/6660097.html