HDU 1542 / PKU 1151 (线段树 + 离散化 + 扫描线)

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 355

Problem Description
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.
 

 

Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.
 

 

Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.
 

 

Sample Input
   
   
   
   
2 10 10 20 20 15 15 25 25.5 0
 

 

Sample Output
   
   
   
   
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; }

你可能感兴趣的:(tree,delete,input,insert,Build,each)