POJ 1151 Atlantis (线段树)

Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13873   Accepted: 5339

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.

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>

#include<cmath>



using namespace std;



#define L(rt) (rt<<1)

#define R(rt) (rt<<1|1)

#define eps 1e-8    // 这里用这个只是担心精度问题,发现这题不需要这样也行



const int N=110;



struct node{

    int l,r,count;  //count 记录覆盖矩形的个数

    double len; //len 当前区间的合法长度

}tree[N<<3];



struct data{

    double x,y1,y2;

    int flag;    // flag 1为入边 -1为出边

}seg[N<<1];



double y[N<<1];



bool cmp (data a,data b)       //x升序

{

    if (a.x < b.x)

        return true;

    if (a.x - b.x < eps && a.flag > b.flag)

       return true;

    return false;

}



void build(int l,int r,int rt){

    tree[rt].l=l;

    tree[rt].r=r;

    tree[rt].count=0;

    tree[rt].len=0;

    if(l+1==r)

        return ;

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

    build(l,mid,L(rt));

    build(mid,r,R(rt));

}



void update(int rt){    //更新合法长度

    if(tree[rt].count>0)

        tree[rt].len=y[tree[rt].r]-y[tree[rt].l];

    else if(tree[rt].l+1==tree[rt].r)

        tree[rt].len=0;

    else

        tree[rt].len=tree[L(rt)].len+tree[R(rt)].len;

}



void query(int flag,double l,double r,int rt){

    if(fabs(y[tree[rt].l]-l)<eps && fabs(y[tree[rt].r]-r)<eps){

        tree[rt].count+=flag;

        update(rt);

        return ;

    }

    double mid=y[(tree[rt].l+tree[rt].r)>>1];

    if(r<=mid)

        query(flag,l,r,L(rt));

    else if(l>=mid)

        query(flag,l,r,R(rt));

    else{

        query(flag,l,mid,L(rt));

        query(flag,mid,r,R(rt));

    }

    update(rt);

}



int main(){



    //freopen("input.txt","r",stdin);



    int n,cases=0;

    double x1,y1,x2,y2;

    while(~scanf("%d",&n) && n){

        int cnt=0;

        for(int i=0;i<n;i++){

            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);

            seg[cnt].x=x1, seg[cnt].y1=y1, seg[cnt].y2=y2;

            seg[cnt].flag=1, y[cnt++]=y1;

            seg[cnt].x=x2, seg[cnt].y1=y1, seg[cnt].y2=y2;

            seg[cnt].flag=-1, y[cnt++]=y2;

        }

        sort(seg,seg+cnt,cmp);

        sort(y,y+cnt);

        int len=unique(y,y+cnt)-y-1;    //离散化

        build(0,len,1);

        double area=0;

        query(seg[0].flag,seg[0].y1,seg[0].y2,1);

        for(int i=1;i<cnt;i++){

            area+=tree[1].len*(seg[i].x-seg[i-1].x);

            query(seg[i].flag,seg[i].y1,seg[i].y2,1);

        }

        printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cases,area);

    }

    return 0;

}

 

你可能感兴趣的:(ant)