poj 1151 扫描线

来源:互联网 发布:淘宝商城女士高跟鞋 编辑:程序博客网 时间:2024/05/16 06:29
Atlantis
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 21347 Accepted: 8041

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

210 10 20 2015 15 25 25.50

Sample Output

Test case #1Total explored area: 180.00 
#include <iostream>#include <cstdio>#include <algorithm>#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1using namespace std;const int N = 1000;struct node1{    double l, r, f, h;}p[N<<2];struct node2{    int l, r,cnt;    double len;}tree[N<<2];double x[N<<2];int cmp(node1 a,node1 b){    return a.h<b.h;}int binary(double z,int a,int b);void build(int l,int r,int rt);void update(int L,int R,int k,int l, int r,int rt);void pushup(int rt);int main(){    int  n, ncase=1;    while(scanf("%d", &n),n!=0)    {        int k=0;        for(int i=0;i<n;i++)        {            double x1, y1, x2, y2;            scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);            p[k].l=x1, p[k].r=x2, p[k].h=y1, p[k].f=1;            p[k+1].l=x1, p[k+1].r=x2, p[k+1].h=y2, p[k+1].f=-1;            x[k]=x1, x[k+1]=x2;            k+=2;        }        sort(p,p+k,cmp);        sort(x,x+k);        int m=1;        for(int i=1;i<k;i++)        {            if(x[i]!=x[i-1])            {                x[m++]=x[i];            }        }        build(0,m-1,1);        double sum=0;        for(int i=0;i<k-1;i++)        {            int l=binary(p[i].l,0,m-1);            int r=binary(p[i].r,0,m-1)-1;            update(l,r,p[i].f,0,m-1,1);            sum+=((p[i+1].h-p[i].h)*tree[1].len);        }        printf("Test case #%d\n",ncase++);        printf("Total explored area: %.2f\n\n",sum);    }    return 0;}int binary(double z,int a,int b){    int l=a, r=b;    while(l<=r)    {        int mid=(l+r)/2;        if(x[mid]==z)        {            return mid;        }        else if(x[mid]<z)        {            l=mid+1;        }        else        {            r=mid-1;        }    }    return -1;}void build(int l,int r,int rt){    tree[rt].l=l, tree[rt].r=r,tree[rt].len=0, tree[rt].cnt=0;    if(l==r)    {        return ;    }    int mid=(l+r)/2;    build(lson);    build(rson);    return ;}void update(int L,int R,int k,int l, int r,int rt){    if(tree[rt].l==L&&tree[rt].r==R)    {        tree[rt].cnt+=k;        pushup(rt);        return ;    }    int mid=(l+r)/2;    if(R<=mid)    {        update(L,R,k,lson);    }    else if(L>mid)    {        update(L,R,k,rson);    }    else    {        update(L,mid,k,lson);        update(mid+1,R,k,rson);    }    pushup(rt);    return ;}void pushup(int rt){    if(tree[rt].cnt!=0)    {        tree[rt].len=x[tree[rt].r+1]-x[tree[rt].l];    }    else if(tree[rt].l==tree[rt].r)    {        tree[rt].len=0;    }    else    {        tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;    }    return ;}
0 0
原创粉丝点击