hdu 1542 Atlantis (线段树求矩形面积并)

来源:互联网 发布:使命召唤4mac迅雷下载 编辑:程序博客网 时间:2024/05/19 03:24

Atlantis

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


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
210 10 20 2015 15 25 25.50
 

Sample Output
Test case #1Total explored area: 180.00
 

题意:
给你n个矩形,让你求这些矩形面积的并值

解析:
具体解析:http://blog.csdn.net/lwt36/article/details/48908031
http://www.cnblogs.com/anhuizhiye/p/3574724.html?utm_source=tuicool&utm_medium=referral

线段树+离散化+扫描——模拟题

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define Me(x) memset(x,0,sizeof(x))using namespace std;const int MAXN = 1100*4;struct seg{double l,r,h;int flag;seg(){}seg(double l,double r,double h,int flag):l(l),r(r),h(h),flag(flag){}bool operator < (const seg& obj)const{return h<obj.h;}}a[MAXN];double xx[MAXN];int res;double sum[MAXN+10];int tree[MAXN+10];void pushUp(int root,int l,int r){if(tree[root]) sum[root]=xx[r+1]-xx[l];  //sum[root]表示root对应区间内现在的线段长度else if(l==r) sum[root]=0;   //叶子节点无孩子节点,这里的叶子结点就表示一个点else  sum[root]=sum[root<<1]+sum[(root<<1)+1];   //向上传递}void update(int s,int d,int l,int r,int root,int val){if(l>d||r<s) return ;if(s<=l&&r<=d){tree[root]+=val;pushUp(root,l,r);return ;}int mid=(l+r)>>1;if(mid>=s) update(s,d,l,mid,2*root,val);if(mid<d) update(s,d,mid+1,r,2*root+1,val);pushUp(root,l,r);}int binary_find(double x){int l,r;l=1;r=res;while(l<r){int mid=(l+r)>>1;if(xx[mid]>=x) r=mid;else l=mid+1;}return r;}int main(){int n,t=0;double x1,y1,x2,y2;while(scanf("%d",&n),n){t++;res=1;for(int i=1;i<=n;i++){scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);a[i]=seg(x1,x2,y1,1);xx[i]=x1;a[i+n]=seg(x1,x2,y2,-1);xx[i+n]=x2;}sort(a+1,a+2*n+1);  //对所有线从低到高排序sort(xx+1,xx+2*n+1);   //对所有点按从左到右排序for(int i=2;i<=2*n;i++)  //删除横坐标相同的点,因为在图中它们都是在同一条直线上{if(xx[i]!=xx[i-1]) xx[++res]=xx[i];}Me(sum);Me(tree);double ans=0;for(int i=1;i<=2*n-1;i++){int l=binary_find(a[i].l);   //二分查找,在xx中找到a[i]这条线左右的端点横坐标在xx中的下标int r=binary_find(a[i].r)-1;  //左闭右开[,)update(l,r,1,res,1,a[i].flag);  //在线段树中插入这个条线,即就是将tree[l]~tree[r]中所有点+flag,这就是离散化建树,根据的是一个个点在xx[]中的下标建树,而不是tree[1]就表示1ans+=sum[1]*(a[i+1].h-a[i].h);}printf("Test case #%d\nTotal explored area: %.2lf\n",t,ans);printf("\n");}return 0;}

阅读全文
0 0
原创粉丝点击