Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18559 Accepted Submission(s): 7523
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.
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.
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
题目来源:
题目大意:
再一个平面上给你一些矩形,要你求矩形一共覆盖的面积。
线段树扫描线求面积。
这个问题,还是推荐一篇博客,我就只谈谈对博客的感想:
首先,给的数据是浮点数,一定要离散化,最好要去重。我这里手写一份自己常用的适应性很高离散化代码(去重),大家可以自己拿去调试一下。到本题中还是要有不小的改动,代码后面也会贴出来,读者可以对比一下。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<cstdio> #include<algorithm> using namespace std; const int N=100007; int t[N],a[N]; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",a+i); t[i]=a[i]; } sort(t,t+n); int m=unique(t,t+n)-t;//共有每个不重复的元素 for(int i=0;i<n;i++) a[i]=lower_bound(t,t+m,a[i])-t+1;//从1开始编号 for(int i=0;i<n;i++) printf("%d ",a[i]);//测试输出 return 0; }
之前建线段树都是以某个点为叶子结点的,但是这次却不能这么写,而要以单位长度为叶子结点。
线段树主要维护cnt(-1表示覆盖不明确,>=0表示完全覆盖cnt次,模仿海报问题),len(区间长度,这就要求离散化的时候建立离散后的数值与原数值的映射)。
其他的参考那篇博客就好啦。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<cstdio> #include<algorithm> using namespace std; double t[410];//排序数组 double a[410];//原始数据 int b[410];//离散后的数据 double c[410];//离散后的数据到原数据的映射 struct tsegment { int x1,x2; int y; int flag; }; tsegment segment[210]; int cmp(tsegment a,tsegment b) { return a.y<b.y; } struct ttree { int l,r; int cover;//-1不确定 >=0完全覆盖次数 就不打tag了,时间可能会长点 double len;//区间长度 }; ttree tree[400*4+20]; void pushup(int x) { if(tree[x].l+1==tree[x].r) return; if(tree[x*2].cover==tree[x*2+1].cover&&tree[x*2].cover>=0) tree[x].cover=tree[x*2].cover; else tree[x].cover=-1; } void pushdown(int x) { if(tree[x].l+1==tree[x].r) return; if(tree[x].cover>=0) tree[x*2].cover=tree[x*2+1].cover=tree[x].cover; } void build(int x,int l,int r) { tree[x].l=l; tree[x].r=r; tree[x].len=c[r]-c[l]; tree[x].cover=0; if(l+1<r) { int mid=(l+r+1)/2; build(x*2,l,mid); build(x*2+1,mid,r);//叶子节点为一小段区间 } } void modify(int x,int l,int r,int flag) { if(l<=tree[x].l&&r>=tree[x].r&&tree[x].cover>=0) { tree[x].cover+=flag; //printf("%d %d\n",tree[x].l,tree[x].r); } else if(tree[x].l+1<tree[x].r) { pushdown(x); int mid=(tree[x].l+tree[x].r+1)/2; if(l<mid) modify(x*2,l,r,flag); if(r>mid) modify(x*2+1,l,r,flag); pushup(x); } } double query(int x) { if(tree[x].cover>0) return tree[x].len; else if(tree[x].cover==0) return 0; else { pushdown(x); double ret=0; ret+=query(x*2); ret+=query(x*2+1); return ret; } } int main() { int n,k=1; while(scanf("%d",&n),n) { for(int i=0;i<n*4;i++) { scanf("%lf",a+i); t[i]=a[i]; } sort(t,t+n*4); int m=unique(t,t+n*4)-t;//共有m个不同的数据 for(int i=0;i<n*4;i++) { b[i]=lower_bound(t,t+m,a[i])-t+1;//编号从1开始 c[b[i]]=a[i]; } //for(int i=1;i<=m;i++) printf("%f\n",c[i]); for(int i=0;i<n;i++) { int x1=b[i*4],y1=b[i*4+1]; int x2=b[i*4+2],y2=b[i*4+3]; segment[i*2].x1=x1;segment[i*2].x2=x2; segment[i*2].y=y1;segment[i*2].flag=1; segment[i*2+1].x1=x1;segment[i*2+1].x2=x2; segment[i*2+1].y=y2;segment[i*2+1].flag=-1; } sort(segment,segment+n*2,cmp); build(1,1,m); modify(1,segment[0].x1,segment[0].x2,segment[0].flag); double ans=0; for(int i=1;i<n*2;i++) { //printf("%.2f\n",query(1)); //printf("%d %d\n",segment[i].x1,segment[i].x2); ans=ans+query(1)*(c[segment[i].y]-c[segment[i-1].y]); modify(1,segment[i].x1,segment[i].x2,segment[i].flag); } printf("Test case #%d\nTotal explored area: %.2f\n\n",k++,ans); } return 0; }