首页 > 代码库 > HDU1542_Atlantis(扫描线/线段树+离散)
HDU1542_Atlantis(扫描线/线段树+离散)
解题报告
题目传送门
题意:
求矩形并面积。
思路:
离散+线段树+扫描线。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; struct Seg { int v; double h,lx,rx; friend bool operator < (Seg a,Seg b) { return a.h<b.h; } } seg[4000]; struct R { double lx,ly,rx,ry; } re[1100]; int lz[401000]; double _hash[101000],sum[401000]; void push_up(int rt,int l,int r) { if(lz[rt])sum[rt]=_hash[r+1]-_hash[l]; else if(l==r)sum[rt]=0; else sum[rt]=sum[rt*2]+sum[rt*2+1]; } void update(int rt,int l,int r,int ql,int qr,int v) { if(ql>r||qr<l)return; if(ql<=l&&r<=qr) { lz[rt]+=v; push_up(rt,l,r); //sum[rt]+=_hash[r+1]-_hash[l]; return ; } int mid=(l+r)/2; update(rt*2,l,mid,ql,qr,v); update(rt*2+1,mid+1,r,ql,qr,v); push_up(rt,l,r); } int main() { int n,i,j,k=1; while(~scanf("%d",&n)) { if(!n)break; int cnt=0; memset(lz,0,sizeof(lz)); memset(sum,0,sizeof(sum)); double ans=0; for(i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&re[i].lx,&re[i].ly,&re[i].rx,&re[i].ry); _hash[i]=re[i].lx; _hash[i+n]=re[i].rx; seg[cnt].h=re[i].ly; seg[cnt].lx=re[i].lx; seg[cnt].rx=re[i].rx; seg[cnt++].v=1; seg[cnt].h=re[i].ry; seg[cnt].lx=re[i].lx; seg[cnt].rx=re[i].rx; seg[cnt++].v=-1; } sort(seg,seg+cnt); sort(_hash,_hash+n*2); int m=unique(_hash,_hash+n*2)-_hash; for(i=0; i<cnt-1; i++) { int l=lower_bound(_hash,_hash+m,seg[i].lx)-_hash; int r=lower_bound(_hash,_hash+m,seg[i].rx)-_hash-1; update(1,0,m-1,l,r,seg[i].v); ans+=sum[1]*(seg[i+1].h-seg[i].h); } printf("Test case #%d\nTotal explored area: ",k++); printf("%.2lf\n",ans); printf("\n"); } return 0; }
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6942 Accepted Submission(s): 3046
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
Source
Mid-Central European Regional Contest 2000
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。