poj1151(离线化+扫描线+线段树)

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24048 Accepted: 8944

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<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 222
#define eps 1e-8
struct Tree
{
    int left,right,num;//num记录这个区间被覆盖的次数 
    double len;//len记录这个区间被覆盖的长度 
}T[4*maxn];
struct Line
{
    double x,y1,y2;
    bool flag;
}line[maxn];
int n,cnt,res;
double y[maxn],ans,len;
bool equal(double x,double y)
{
    return fabs(x-y)<eps;
}
void add(double x,double y1,double y2,double yy,int flag)
{
    line[cnt].x=x;line[cnt].y1=y1;line[cnt].y2=y2;line[cnt].flag=flag;y[cnt++]=yy;
}
bool cmp(Line l1,Line l2)
{
    if(l1.x!=l2.x) return l1.x<l2.x;
    return l1.flag>l2.flag;
}
void build(int l,int r,int t)
{
    T[t].left=l;
    T[t].right=r;
    T[t].num=0;
    T[t].len=0;
    if(l+1==r)return ;
    int mid=(l+r)>>1;
    build(l,mid,2*t);
    build(mid,r,2*t+1);
}
void update_len(int t)
{
    if(T[t].num) T[t].len=y[T[t].right]-y[T[t].left];
    else if(T[t].left+1==T[t].right) T[t].len=0;
    else T[t].len=T[2*t].len+T[2*t+1].len;
}
void update(double l,double r,int t,int x)
{
    if(equal(y[T[t].left],l)&&equal(y[T[t].right],r)) T[t].num+=x;
    if(T[t].left+1<T[t].right)
    {
        int mid=(T[t].left+T[t].right)>>1;
        if(r<=y[mid]+eps) update(l,r,2*t,x);
        else if(l>=y[mid]-eps) update(l,r,2*t+1,x);
        else
        {
            update(l,y[mid],2*t,x);
            update(y[mid],r,2*t+1,x);
        }
    }
    update_len(t);
}
int main()
{
    int t=1;
    while(~scanf("%d",&n),n)
    {
        cnt=0;ans=0;len=0;
        double x1,x2,y1,y2;
        while(n--)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            //将每个矩形用左右两边表示 
            add(x1,y1,y2,y1,1);
            add(x2,y1,y2,y2,0); 
        }
        sort(line,line+cnt,cmp);//对得到的线段按x升序排序 
        //对y轴建树 
        sort(y,y+cnt);
        res=unique(y,y+cnt)-y;
        build(0,res-1,1);
        for(int i=0;i<cnt;i++)
        {
            if(line[i].flag) update(line[i].y1,line[i].y2,1,1);//扫到左边的边就在线段树中覆盖这部分 
            else update(line[i].y1,line[i].y2,1,-1);//扫到右边的边就在线段树中取消这部分的覆盖 
            if(i) ans+=len*(line[i].x-line[i-1].x);//累加每次扫描线扫过的面积 
            len=T[1].len;//维护扫描线上被覆盖的长度 
        }
        printf("Test case #%d\n",t++);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值