POJ 1151 Atlantis(线段树扫描线)

来源:互联网 发布:淘宝直播抢红包神器 编辑:程序博客网 时间:2024/06/06 08:49

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
210 10 20 2015 15 25 25.50
 

Sample Output
Test case #1Total explored area: 180.00
 

线段树扫描线:

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int maxn=201;struct node{    int l,r;//记录线段树左右端点的值    int c;//记录覆盖情况    double cnt,lf,rf;//cnt真实长度。lf,rf记录的是左右的真实值}t[maxn*3];struct Line{    double x,y1,y2;//记录线段的上下端点和x坐标    int f;//记录是左边还是右边;}line[maxn];double y[maxn];//储存y坐标int cmp(Line l1,Line l2){    return l1.x<l2.x;}void build(int rs,int l,int r)//y坐标离散化后构造线段树{    t[rs].l=l;    t[rs].r=r;    t[rs].cnt=t[rs].c=0;    t[rs].lf=y[l];    t[rs].rf=y[r];    if(l+1==r)        return ;    int mid=(l+r)>>1;    build(rs<<1,l,mid);    build(rs<<1|1,mid,r);;}void pushup(int rs)//计算长度{    if(t[rs].c>0)  t[rs].cnt=t[rs].rf-t[rs].lf;    else if(t[rs].l+1==t[rs].r)  t[rs].cnt=0;    else  t[rs].cnt=t[rs<<1].cnt+t[rs<<1|1].cnt;}void update(int rs,Line e)//加入线段e后更新线段树{    if(e.y1==t[rs].lf&&e.y2==t[rs].rf)    {        t[rs].c+=e.f;        pushup(rs);        return ;    }    if(e.y2<=t[rs<<1].rf)  update(rs<<1,e);    else if(e.y1>=t[rs<<1|1].lf)  update(rs<<1|1,e);    else    {        Line temp=e;        temp.y2=t[rs<<1].rf;        update(rs<<1,temp);        temp=e;        temp.y1=t[rs<<1|1].lf;        update(rs<<1|1,temp);    }    pushup(rs);}int main(){    int n,tt,s=0;    double x1,y1,x2,y2;    while(~scanf("%d",&n)&&n)    {        s++;        tt=1;        for(int i=1;i<=n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            line[tt].x=x1;            line[tt].y1=y1;            line[tt].y2=y2;            line[tt].f=1;            y[tt]=y1;            tt++;            line[tt].x=x2;            line[tt].y1=y1;            line[tt].y2=y2;            line[tt].f=-1;            y[tt]=y2;            tt++;        }        sort(line+1,line+tt,cmp);        sort(y+1,y+tt);        int len=unique(y+1,y+tt)-(y+1);        build(1,1,len);        update(1,line[1]);        double res=0;        for(int i=2;i<tt;i++)        {            res+=t[1].cnt*(line[i].x-line[i-1].x);            update(1,line[i]);        }        printf("Test case #%d\nTotal explored area: %.2f\n\n",s,res);    }    return 0;}


0 0
原创粉丝点击