首页 > 代码库 > POJ·1151 Atlantis·线段树求矩形面积并

POJ·1151 Atlantis·线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151

 

Atlantis

Time Limit: 1000MS Memory Limit: 10000K

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 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

210 10 20 2015 15 25 25.50

Sample Output

Test case #1Total explored area: 180.00 

Source

Mid-Central European Regional Contest 2000
 
 
还是比较裸的线段树求矩形面积并,与上一题基本相同,不同的是这题坐标是小数,需要离散。
离散的时候学习了一个函数:unique函数,功能是将数组相邻的重复元素放到最后面,返回最后面的地址。
  用的时候需要对数组进行排序,长度 = unique(name,name+len) - name - 1
codes:
 1 #include<set> 2 #include<queue> 3 #include<vector> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std;10 const int N = 110;11 #define Ch1 (i<<1)12 #define Ch2 (Ch1|1)13 #define mid(i) T[i].mid14 #define len(i) T[i].r - T[i].l15 #define rlen(i) T[i].rr - T[i].rl16 #define For(i,n) for(int i=1;i<=n;i++)17 #define Rep(i,l,r) for(int i=l;i<=r;i++)18 19 struct tnode{20     int l,r,mid,cover;21     double rl,rr,len;22 }T[N<<8];23 24 struct lines{25     double l,r,h;26     int kind;//1表示上底边 27 }L[N*2];28 int n,Lim,tot,kth;29 double x1,y1,x2,y2,cor[N*2],ans;30 31 bool cmp(lines A,lines B){32     return A.h<B.h;33 }34 35 void init(){36     tot = 0;ans = 0;37     For(i,n){38         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);39         L[++tot].l = x1; L[tot].r = x2; L[tot].h = y1; L[tot].kind = 0; cor[tot] = x1;40         L[++tot].l = x1; L[tot].r = x2; L[tot].h = y2; L[tot].kind = 1; cor[tot] = x2;41     }42     sort(L+1,L+tot+1,cmp);43     sort(cor+1,cor+tot+1);44     Lim = unique(cor+1,cor+tot+1) - cor - 1;45 }46 47 void Build(int l,int r,int i){48     T[i].l = l; T[i].r = r; T[i].mid = (l+r)>>1;49     T[i].rl = cor[l]; T[i].rr = cor[r];50     if(l==r-1) return;51     Build(l,mid(i),Ch1); Build(mid(i),r,Ch2);52 }53 54 void Modify(int i,double l,double r,int delta){55     if(l==T[i].rl&&T[i].rr==r){56         T[i].cover+=delta;57         if(T[i].cover>0)        T[i].len = rlen(i);else 58         if(T[i].l+1==T[i].r)    T[i].len = 0;      else                        59                                 T[i].len = T[Ch1].len + T[Ch2].len;60         return;61     }62     double Mid = cor[mid(i)];63     if(r<=Mid)  Modify(Ch1,l,r,delta);else64     if(l>=Mid)  Modify(Ch2,l,r,delta);else65     Modify(Ch1,l,Mid,delta),Modify(Ch2,Mid,r,delta);66     if(T[i].cover>0)            T[i].len = rlen(i);67     else                        T[i].len = T[Ch1].len + T[Ch2].len; 68 } 69 70 int main(){71     scanf("%d",&n);72     while(n){73         init();kth++; 74         Build(1,Lim,1);L[0].h = L[1].h;75         For(i,tot){76             if(!L[i].kind){77                 ans+=T[1].len * (L[i].h - L[i-1].h);78                 Modify(1,L[i].l,L[i].r,1); 79             }else{80                 ans+=T[1].len * (L[i].h - L[i-1].h);81                 Modify(1,L[i].l,L[i].r,-1);82             }83         }84         printf("Test case #%d\n",kth);85         printf("Total explored area: %.2f\n",ans);86         printf("\n");87         scanf("%d",&n);88         if(!n) return 0;89     }90     return 0;91 }