Hdu 1542 Atlantis
来源:互联网 发布:validform.js tiptype 编辑:程序博客网 时间:2024/05/16 07:32
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
题意:矩形面积并
思路:浮点数先要离散化;然后把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,进行线段树操作
解题:线段树+扫描线
#include<cstdio>#include<algorithm>#include<cmath>using namespace std;#define maxn 111#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define Min(a,b) (a<b?a:b)#define Max(a,b) (a>b?a:b)struct Node{ double l,r,h; int flag;}num[maxn<<1];double Hash[maxn<<1];int Mi[maxn<<3],Ma[maxn<<3];//Mi存区间最小值,Ma存区间最大值int lazy[maxn<<3];bool operator <(Node a,Node b){ return a.h<b.h;}int Bin(int star,int las,double x){ int m; while(star<=las) { m=(star+las)>>1; if(fabs(Hash[m]-x)<1e-10) return m; if(Hash[m]>x) las=m-1; else star=m+1; } return -1;}void PushDown(int rt){ lazy[rt<<1]+=lazy[rt]; lazy[rt<<1|1]+=lazy[rt]; Mi[rt<<1]+=lazy[rt]; Ma[rt<<1]+=lazy[rt]; Mi[rt<<1|1]+=lazy[rt]; Ma[rt<<1|1]+=lazy[rt]; lazy[rt]=0;}void PushUp(int rt){ Mi[rt]=Min(Mi[rt<<1],Mi[rt<<1|1]); Ma[rt]=Max(Ma[rt<<1],Ma[rt<<1|1]);}void build(int l,int r,int rt){ Mi[rt]=0; Ma[rt]=0; lazy[rt]=0; if(l==r) return ; int m=(l+r)>>1; build(lson); build(rson);}void updata(int l,int r,int rt,int L,int R,int flag){ if(l>=L && r<=R) { lazy[rt]+=flag; Mi[rt]+=flag; Ma[rt]+=flag; return ; } PushDown(rt); int m=(l+r)>>1; if(L<=m) updata(lson,L,R,flag); if(R>m) updata(rson,L,R,flag); PushUp(rt);}double query(int l,int r,int rt){ if(Mi[rt]>0) return Hash[r+1]-Hash[l]; if(Ma[rt]==0) return 0; PushDown(rt); int m=(l+r)>>1; double s=query(lson)+query(rson); PushUp(rt); return s; }int main(){ int n; int i,cnt,t=1; double x1,x2,y1,y2; double sum; while(scanf("%d",&n),n) { for(i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); Hash[i*2]=x1; Hash[i*2+1]=x2; num[i*2].l=x1; num[i*2].r=x2; num[i*2].h=y1; num[i*2].flag=1; num[i*2+1].l=x1; num[i*2+1].r=x2; num[i*2+1].h=y2; num[i*2+1].flag=-1; } sort(Hash,Hash+2*n); sort(num,num+2*n); for(i=1,cnt=0;i<2*n;i++) if(fabs(Hash[i]-Hash[cnt])>1e-10) Hash[++cnt]=Hash[i]; build(0,cnt,1); sum=0.0; for(i=0;i<2*n;i++) { updata(0,cnt,1,Bin(0,cnt,num[i].l),Bin(0,cnt,num[i].r)-1,num[i].flag); sum+=query(0,cnt,1)*(num[i+1].h-num[i].h); } printf("Test case #%d\nTotal explored area: %.2lf\n\n",t++,sum); } return 0;}
- 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 1542 Atlantis
- HDU 1542 Atlantis
- hdu 1542 Atlantis
- HDU 1542 Atlantis
- HDU 1542 Atlantis
- 【HDU】 1542 Atlantis
- JSONObject简介
- JBoss是什么? ---又又哥
- Asp.Net Web项目打包Setup
- GSL连续内存块分配
- Hibernate QBC查询
- Hdu 1542 Atlantis
- ffmpeg解码流程
- android开发图片分辨率
- Android APK 签名比对
- URI是什么,在Android中有什么作用? .
- HibernateTemplate用法&session的区别
- Android中的签名机制
- 管理诊断数据(1)---故障诊断性基础结构
- Ignatius and the Princess III(hdu1028,母函数之整数划分)