Atlantis - HDU 1542 扫描线

来源:互联网 发布:vray for mac 编辑:程序博客网 时间:2024/05/16 12:56

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7210    Accepted Submission(s): 3173


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.
 

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.
 

Sample Input
210 10 20 2015 15 25 25.50
 

Sample Output
Test case #1Total explored area: 180.00
 

题意:给你n个矩阵,问矩阵合并之后的总面积。

思路:裸扫描线。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;double q[21000];struct node{    int l,r,val;    double len;}tree[81000];struct node2{    double x1,x2,h;    int d;}op[21000];bool cmp(node2 a,node2 b){    return a.h<b.h;}int n,m;void build(int o,int l,int r){    tree[o].l=l;    tree[o].r=r;    tree[o].val=0;    tree[o].len=0;    if(l==r)      return;    int mi=(l+r)/2;    build(o*2,l,mi);    build(o*2+1,mi+1,r);}void up(int o){    if(tree[o].val>0)      tree[o].len=q[tree[o].r]-q[tree[o].l-1];    else if(tree[o].l==tree[o].r)      tree[o].len=0;    else      tree[o].len=tree[o*2].len+tree[o*2+1].len;}void update(int o,int l,int r,int val){    if(tree[o].l==l && tree[o].r==r)    {        tree[o].val+=val;        up(o);        return;    }    int mi=(tree[o].l+tree[o].r)/2;    if(r<=mi)      update(o*2,l,r,val);    else if(l>mi)      update(o*2+1,l,r,val);    else    {        update(o*2,l,mi,val);        update(o*2+1,mi+1,r,val);    }    up(o);}int find(double a){    int l=1,r=m,mi;    while(l<r)    {        mi=(l+r)/2;        if(q[mi]<a)          l=mi+1;        else          r=mi;    }    return l;}int main(){    int t=0,i,j,k;    double x1,x2,y1,y2,ans;    while(~scanf("%d",&n) && n)    {        t++;        for(i=1;i<=n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            op[i*2-1].x1=x1;op[i*2-1].x2=x2;op[i*2-1].d=-1;op[i*2-1].h=y2;            op[i*2].x1=x1;op[i*2].x2=x2;op[i*2].d=1;op[i*2].h=y1;            q[i*2-1]=x1;q[i*2]=x2;        }        sort(q+1,q+1+n*2);        sort(op+1,op+1+n*2,cmp);        op[0].h=0;        m=1;        for(i=2;i<=n*2;i++)           if(q[i]!=q[i-1])             q[++m]=q[i];        build(1,1,m);        ans=0;        for(i=1;i<n*2;i++)        {            update(1,find(op[i].x1)+1,find(op[i].x2),op[i].d);            ans+=tree[1].len*(op[i+1].h-op[i].h);        }        printf("Test case #%d\nTotal explored area: %.2f\n\n",t,ans);    }}



0 0
原创粉丝点击