HDU 1542 Atlantis 线段树扫面线求面积并
来源:互联网 发布:网络小说 神作 知乎 编辑:程序博客网 时间:2024/05/26 05:52
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
210 10 20 2015 15 25 25.50
Sample Output
Test case #1Total explored area: 180.00
Source
Mid-Central European Regional Contest 2000
Recommend
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8using namespace std;#define N 605struct node{int le,ri,cover;double val;}f[N];struct stud{double x1,x2,h;int type;}e[N];map<double,int>mp;double X[N];int cmp(stud a,stud b){return a.h<b.h;}void build(int pos,int le,int ri){f[pos].le=le;f[pos].ri=ri;f[pos].val=0;f[pos].cover=0;if(le==ri) return ;int mid=MID(le,ri);build(L(pos),le,mid);build(R(pos),mid+1,ri);}void pushup(int pos){ if(f[pos].cover){f[pos].val=X[f[pos].ri]-X[f[pos].le-1];return ;}if(f[pos].le==f[pos].ri)f[pos].val=0;elsef[pos].val=f[L(pos)].val+f[R(pos)].val;}void update(int pos,int le,int ri,int type){ if(f[pos].le>=le&&f[pos].ri<=ri) { f[pos].cover+=type; pushup(pos); return ; } int mid=MID(f[pos].le,f[pos].ri);// if(le<=mid)//update(L(pos),le,ri,type);//// if(ri>mid)//update(R(pos),le,ri,type); if(mid>=ri)update(L(pos),le,ri,type); elseif(mid<le) update(R(pos),le,ri,type); else { update(L(pos),le,mid,type); update(R(pos),mid+1,ri,type); } pushup(pos);}int main(){ int i,j; int n,k,ca=0; double x1,x2,y1,y2; while(scanf("%d",&n),n){ k=0; while(n--) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); X[k]=x1; e[k].x1=x1; e[k].x2=x2; e[k].h=y1; e[k++].type=1; X[k]=x2; e[k].x1=x1; e[k].x2=x2; e[k].h=y2; e[k++].type=-1; }sort(X,X+k);sort(e,e+k,cmp);int m=1;for(i=1;i<k;i++) if(X[i]!=X[i-1])X[m++]=X[i];for(i=0;i<m;i++)mp[X[i]]=i;m--;build(1,1,m);double ans=0;for(i=0;i<k-1;i++){int le=mp[e[i].x1]+1;int ri=mp[e[i].x2];update(1,le,ri,e[i].type);ans+=f[1].val*(e[i+1].h-e[i].h);}printf("Test case #%d\n",++ca);printf("Total explored area: %.2lf\n\n",ans);}return 0;}//另外一种建树方法#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8using namespace std;#define N 605struct stud{ double x1,x2,h; int type;}e[N];struct node{ int le,ri,cover; double val;}f[N];int n;double X[N];map<double,int>mp;int cmp(stud a,stud b){return a.h<b.h;}void build(int pos,int le,int ri){f[pos].le=le;f[pos].ri=ri;f[pos].val=0;f[pos].cover=0;if(le+1==ri) return ;int mid=MID(le,ri);build(L(pos),le,mid);build(R(pos),mid,ri);}void pushup(int pos){if(f[pos].cover){f[pos].val=X[f[pos].ri]-X[f[pos].le];return ;}if(f[pos].le==f[pos].ri||f[pos].le+1==f[pos].ri)f[pos].val=0;else f[pos].val=f[L(pos)].val+f[R(pos)].val;}void update(int pos,int le,int ri,int type){if(le==ri) return ;if(f[pos].le>=le&&f[pos].ri<=ri){f[pos].cover+=type;pushup(pos);return ;}int mid=MID(f[pos].le,f[pos].ri);if(mid>=ri)update(L(pos),le,ri,type);else if(mid<le) update(R(pos),le,ri,type); else { if(mid>le) update(L(pos),le,mid,type); if(mid<ri) update(R(pos),mid,ri,type); } pushup(pos);}int main(){int i,j,ca=0;while(scanf("%d",&n),n){double x1,x2,y1,y2;int k=0;while(n--){scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);if(x1>x2) swap(x1,x2);X[k]=x1;e[k].x1=x1;e[k].x2=x2;e[k].h=y1;e[k++].type=1;X[k]=x2;e[k].x1=x1;e[k].x2=x2;e[k].h=y2;e[k++].type=-1;}sort(X,X+k);sort(e,e+k,cmp);int m=1;for(i=1;i<k;i++)if(X[i]!=X[i-1]) X[m++]=X[i];for(i=0;i<m;i++)mp[X[i]]=i;m--;build(1,0,m);double ans=0;for(i=0;i<k-1;i++){int le=mp[e[i].x1];int ri=mp[e[i].x2];update(1,le,ri,e[i].type);ans+=f[1].val*(e[i+1].h-e[i].h);} printf("Test case #%d\n",++ca); printf("Total explored area: %.2lf\n\n",ans);}return 0;}
1 0
- HDU 1542 Atlantis 线段树扫面线求面积并
- hdu 1542 Atlantis 面积并 线段树 扫描线
- hdu 1542(线段树 求矩形面积并)Atlantis
- Hdu 1542 Atlantis 线段树 求矩形面积并
- Hdu 1542/ POj 1151 Atlantis 线段树+矩形面积并
- hdu 1542 Atlantis(线段树 面积并+扫描线)
- hdu 1542 Atlantis(线段树&扫描线&面积并)
- HDU-1542-Atlantis-线段树+面积并(模板)
- HDU 1542 Atlantis(线段树扫描线,面积并)
- HDU 1542 Atlantis(线段树求矩形面积并)
- hdu 1542 Atlantis 线段树矩形面积并+离散化
- HDU 1542 Atlantis (线段树扫描线求面积并)
- HDU 1542 Atlantis(线段树求矩形面积并)
- hdu 1542 Atlantis (线段树求矩形面积并)
- HDU 1542 Atlantis(线段树+扫描线+面积并)
- 【HDU】1542 Atlantis 矩形面积并->线段树
- Hdu 1542 Atlantis + Hdu 1255 覆盖的面积 (线段树矩形面积并)
- 面积并问题 HDU 1542 Atlantis
- 正则表达式30分钟入门教程
- C算法->指针操作
- VirtualBox上Ubuntu Server虚拟机配置双网卡
- uboot 源码官方下载地址
- jsp中pageEncoding、charset=UTF -8 jsp中pageEncoding、charset=UTF -8"
- HDU 1542 Atlantis 线段树扫面线求面积并
- TSP问题遗传算法通用Matlab程序
- Linux学习之十六、文件的格式化与相关处理
- IOS学习之路-①语言
- 安装storm入口机
- 差分线走法——PADS
- Ac6605 AP上线关键点
- 2015考研总结
- iOS 消息推送 (Message) 证书设置指南