Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 721 Accepted Submission(s): 355
2 10 10 20 20 15 15 25 25.5 0
Test case #1 Total explored area: 180.00
#include <iostream> #include <algorithm> using namespace std; #define N 105 #define min(a,b) a>b?b:a struct Line { double x,y1,y2;//竖直线段的上下端点y1,y2和位置x int flag;//flag=1则为矩形的左线段,=0为矩形的右线段 }; struct LineTree { int r,l,mid; int cover; double nlen; }tree[N*8]; Line y[N*2]; double mark[N*2]; int cnt; bool cmp(Line a,Line b) { return a.x < b.x; } int Find(double x) { int low,high,mid; low = 0; high = cnt - 1; while(low != high) { mid = (low + high)>>1; if( x > mark[mid]) low = mid + 1; else high = mid; } return low + 1; } void Build(int root,int a,int b) { tree[root].l = a; tree[root].r = b; tree[root].mid = (a+b)>>1; tree[root].cover = 0; tree[root].nlen = 0; if(a + 1 == b) return; Build(root<<1,a,tree[root].mid); Build((root<<1)+1,tree[root].mid,b); } void Update(int root) { if(tree[root].cover > 0) tree[root].nlen = mark[tree[root].r - 1] - mark[tree[root].l - 1]; else if(tree[root].l + 1 == tree[root].r) tree[root].nlen = 0; else tree[root].nlen = tree[root<<1].nlen + tree[(root<<1)+1].nlen; } void Insert(int root,int a,int b) { if(tree[root].l >= a && tree[root].r <= b) { tree[root].cover++; Update(root); return; } if(a < tree[root].mid) Insert(root<<1,a,b); if(b > tree[root].mid) Insert((root<<1)+1,a,b); Update(root); } void Delete(int root,int a,int b) { if (tree[root].l >= a && tree[root].r <= b) { tree[root].cover--; Update(root); return; } if(a < tree[root].mid) Delete(root<<1,a,b); if(b > tree[root].mid) Delete((root<<1)+1,a,b); Update(root); } int main() { int t,i,a,b,ttt=0; double x1,y1,x2,y2; double area; while (scanf("%d",&t),t) { for(i=0;i<t;i++) { scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); y[i<<1].x = x1; y[(i<<1)+1].x = x2; y[i<<1].y1 = y1; y[(i<<1)+1].y1 = y1; y[i<<1].y2 = y2; y[(i<<1)+1].y2 = y2; y[i<<1].flag = 1; y[(i<<1)+1].flag = 0; mark[i<<1] = y1; mark[(i<<1)+1] = y2; } sort(y,y+2*t,cmp); sort(mark,mark+2*t); double temp = mark[0]; cnt = 1; for(i=1;i<2*t;i++) { if(temp != mark[i]) { mark[cnt++] = mark[i]; temp = mark[i]; } } Build(1,1,cnt); area = 0; for(i=0;i<2*t;i++) { a = Find(y[i].y1); b = Find(y[i].y2); if(y[i].flag) Insert(1,a,b); else Delete(1,a,b); area += tree[1].nlen * (y[i+1].x - y[i].x); } printf("Test case #%d/nTotal explored area: %.2lf/n/n",++ttt,area); } return 0; }