poj1151 Atlantis

来源:互联网 发布:淘宝店铺定位什么意思 编辑:程序博客网 时间:2024/06/06 09:10

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 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.

用线段树维护y轴上的长度,然后沿x轴扫描。
具体的维护方法是用cov记录当前区间被完整的【拼起来的不算】线段覆盖的次数,sum记录被覆盖的长度。每次扫到线段就增减cov,如果cov大于0长度就是区间长度,否则就是左右两段的和。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct seg{    int y1,y2,k;    double x,yy1,yy2;    bool operator < (const seg & ss) const    {        return x<ss.x||(x==ss.x&&k<ss.k);    }}a[210];int n,cov[1010],tot,m;double ord[210],sum[1010];void mark(int p,int L,int R,int l,int r,int k){    if (L==l&&R==r)    {        cov[p]+=k;        if (cov[p]) sum[p]=ord[R]-ord[L-1];        else sum[p]=sum[p*2]+sum[p*2+1];        return;    }    int M=(L+R)/2;    if (r<=M) mark(p*2,L,M,l,r,k);    else    {        if (l>M) mark(p*2+1,M+1,R,l,r,k);        else        {            mark(p*2,L,M,l,M,k);            mark(p*2+1,M+1,R,M+1,r,k);        }    }    if (cov[p]) sum[p]=ord[R]-ord[L-1];    else sum[p]=sum[p*2]+sum[p*2+1];}int main(){    int i,j,k,p,q,x,y,z,K=0;    double x1,y1,x2,y2,ans;    while (scanf("%d",&n)&&n)    {        tot=m=0;        memset(cov,0,sizeof(cov));        memset(sum,0,sizeof(sum));        for (i=1;i<=n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            ord[++tot]=y1;            ord[++tot]=y2;            a[++m]=(seg){0,0,1,x1,y1,y2};            a[++m]=(seg){0,0,-1,x2,y1,y2};        }        sort(ord+1,ord+tot+1);        tot=unique(ord+1,ord+tot+1)-ord-1;        for (i=1;i<=m;i++)        {            a[i].y1=lower_bound(ord+1,ord+tot+1,a[i].yy1)-ord+1;            a[i].y2=lower_bound(ord+1,ord+tot+1,a[i].yy2)-ord;        }        sort(a+1,a+m+1);        ans=0;        for (i=1;i<=m;i++)        {            mark(1,2,tot,a[i].y1,a[i].y2,a[i].k);            if (i<m) ans+=sum[1]*(a[i+1].x-a[i].x);        }        printf("Test case #%d\nTotal explored area: %.2f\n\n",++K,ans);    }}
0 0
原创粉丝点击