1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include <iostream> #include <string.h> #include <algorithm> #define lson p<<1,left,mid #define rson p<<1|1,mid+1,right using namespace std; const int maxn = 2000+5;
int add[maxn << 2]; double x[maxn<<2],sum[maxn<<2]; int n, cnt, res;
struct node { double l, r, h; int s; node() {}; node(double ll,double rr,double hh,int ss) { l = ll; r = rr; h = hh; s = ss; } bool operator<(const node& temp)const { return h < temp.h; } }s[maxn]; void pushup(int p,int left,int right) { if (add[p]) sum[p] = x[right + 1] - x[left]; else if (left == right) sum[p] = 0; else sum[p] = sum[p << 1] + sum[p<<1|1]; } void updata(int L,int R,int val,int p,int left,int right) { if (L <= left && right <= R) { add[p] += val; pushup(p,left,right); return; } int mid = (left + right) >>1; if (L <= mid) updata(L,R,val,lson); if (mid < R) updata(L,R,val,rson); pushup(p,left,right); }
int binary_find(double val) { int left = 0, right = res - 1; while (left <= right) { int mid = (left + right) >> 1; if (x[mid] < val) left = mid + 1; else right = mid - 1; } return left; } int main() { int count = 1; while (scanf("%d",&n) != EOF && n) { cnt = 0; for (int i = 0; i < n; i++) { double a, b, c, d; scanf("%lf%lf%lf%lf",&a,&b,&c,&d); s[cnt] = node(a,c,b,1); x[cnt++] = a; s[cnt] = node(a,c,d,-1); x[cnt++] = c; } sort(s,s+cnt); sort(x,x+cnt); res=1; for (int i = 1; i < cnt; i++) if (x[i] != x[i - 1]) x[res++] = x[i]; memset(add,0,sizeof add); memset(sum,0,sizeof sum); double ans = 0; for (int i = 0; i < cnt-1; i++) { int l = binary_find(s[i].l); int r = binary_find(s[i].r) - 1; updata(l,r,s[i].s,1,0,res-1); ans += sum[1] * (s[i+1].h - s[i].h); } printf("Test case #%d\nTotal explored area: %.2lf\n\n", count++, ans); } return 0; }
|