HDOJ 1542 Atlantis

扫描线+线段树 求矩形面积并


Atlantis

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


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
2 10 10 20 20 15 15 25 25.5 0
 

Sample Output
Test case #1 Total explored area: 180.00
 

Source
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn=11000;

int flag[maxn*10]; double sum[maxn*10];
double X[maxn];

struct Line
{
    double xl,xr,y;
    int s;

    bool operator<(const Line &M) const
    {
        return y<M.y;
    }

}line[maxn];

void push_up(int l,int r,int rt)
{
    if(flag[rt]) sum[rt]=X[r+1]-X[l];
    else if(l==r) sum[rt]=0;
    else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void update(int l,int r,int rt,int L,int R,int c)
{
    if(L<=l&&r<=R)
    {
        flag[rt]+=c;
        push_up(l,r,rt);
        return ;
    }

    int m=(l+r)>>1;

    if(R<=m) update(lson,L,R,c);
    else if(L>m) update(rson,L,R,c);
    else
    {
        update(lson,L,m,c);
        update(rson,m+1,R,c);
    }

    push_up(l,r,rt);
}

int find(double x,int k)
{
    int low=1,high=k,mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(X[mid]==x) return mid;
        else if(X[mid]>x) high=mid-1;
        else low=mid+1;
    }
}

int main()
{
    int n,cas=1;
while(scanf("%d",&n)!=EOF&&n)
{
    memset(flag,0,sizeof(flag));
    memset(sum,0,sizeof(sum));

    int num=0;
    for(int i=0;i<n;i++)
    {
        double x1,x2,y1,y2;
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        line[++num]=(Line){x1,x2,y1,1};
        X[num]=x1;
        line[++num]=(Line){x1,x2,y2,-1};
        X[num]=x2;
    }

    sort(X+1,X+1+num);
    sort(line+1,line+1+num);

    int k=1;
    for(int i=2;i<=num;i++)
    {
        if(X[i]!=X[i-1]) X[++k]=X[i];
    }
    double ans=0;
    for(int i=1;i<num;i++)
    {
        int l=find(line[i].xl,k);
        int r=find(line[i].xr,k)-1;
        update(1,k,1,l,r,line[i].s);
        ans+=sum[1]*(line[i+1].y-line[i].y);
    }

    printf("Test case #%d\n",cas++);
    printf("Total explored area: %.2lf\n\n",ans);
}
    return 0;
}



HDOJ 1542 Atlantis

上一篇:世纪大争论:Linux还是GNU/Linux?


下一篇:由计算机谈最强大脑周玮