poj1511 Atlantis(线段树+扫描线)

来源:互联网 发布:淘宝卖家用什么软件 编辑:程序博客网 时间:2024/05/17 02:09
Atlantis
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 18197 Accepted: 6916

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 

Source

Mid-Central European Regional Contest 2000
/*再做一次,深刻理解扫描线以及区间更新。。加油!!!Time:2015-1-19 18:09*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson l,mid,rt<<1#define rson mid,r,rt<<1|1const int MAX=205;struct Tree{    double lf,rf,len;    int covers;//只要覆盖大于0就说明此段至少有一层}tree[MAX<<2];struct Line{    double x,y1,y2;    int isLeft;//如果是面积左边的,则标记为1,否则标记为-1}line[MAX];double yNode[MAX];bool cmp(Line a,Line b){    return a.x<b.x;}void Build(int l,int r,int rt){    tree[rt].lf=yNode[l];    tree[rt].rf=yNode[r];    tree[rt].len=tree[rt].covers=0;    if(l+1==r)return;    int mid=(l+r)>>1;    Build(lson);    Build(rson);}void CalLen(int l,int r,int rt){    if(tree[rt].covers>0){        tree[rt].len=tree[rt].rf-tree[rt].lf;        return;    }    if(r-l==1){        tree[rt].len=0;    }else{        tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;    }}void update(Line seg,int l,int r,int rt){    if(seg.y1==tree[rt].lf&&seg.y2==tree[rt].rf){        tree[rt].covers+=seg.isLeft;        CalLen(l,r,rt);        return ;    }    int mid=(l+r)>>1;    if(seg.y2<=tree[rt<<1].rf){        update(seg,lson);    }else if(seg.y1>=tree[rt<<1|1].lf){        update(seg,rson);    }else{        Line temp=seg;        temp.y2=tree[rt<<1].rf;        update(temp,lson);        temp=seg;        temp.y1=tree[rt<<1|1].lf;        update(temp,rson);    }    CalLen(l,r,rt);}int main(){    int n,k,i;    int nCase=1;    double x1,x2,y1,y2;    while(scanf("%d",&n)!=EOF,n){        printf("Test case #%d\n",nCase++);        k=1;        for(i=1;i<=n;i++){            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            line[k].x=x1;            line[k].y1=y1;            line[k].y2=y2;            line[k].isLeft=1;            yNode[k++]=y1;//这儿刚好利用k的变化把y坐标离散化一下            line[k].x=x2;            line[k].y1=y1;            line[k].y2=y2;            line[k].isLeft=-1;            yNode[k++]=y2;        }        sort(line+1,line+k,cmp);//x从小到大排序        sort(yNode+1,yNode+k);//排序之后建树,刚好离散化,每个都有个对应值        Build(1,k,1);        double ans=0;        update(line[1],1,k,1);        for(i=2;i<k;i++){            ans+=tree[1].len*(line[i].x-line[i-1].x);            update(line[i],1,k,1);        }        printf("Total explored area: %.2f\n\n",ans);    }return 0;}

0 0