POJ(1151)——Atlantis(线段树 扫描线)

来源:互联网 发布:网络攻击防范 编辑:程序博客网 时间:2024/04/29 05:41

 Atlantis
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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 maxin 1000using namespace std;struct node//建立线段树{    double y1,y2,jl;//记录每一条平行于y轴边的起点终点和两点之间的距离    int ceng;//记录该线段是否被遍历过};node tree[800];struct smx//记录每一条平行于y轴边的起点和终点{    double x;//该边对应的x值    double yx,yh;    int f;//标记该边是一个矩形的起始边还是终止边};smx sm[400];double yy[400];int cmp(smx a,smx b){    return a.x < b.x;}void jianshu(int l,int r,int rt){    tree[rt].ceng = tree[rt].jl = 0;    tree[rt].y1 = yy[l];    tree[rt].y2 = yy[r];    if((l + 1) == r)    {        return ;    }    int mid = (l + r)>>1;    jianshu(l,mid,rt<<1);    jianshu(mid,r,rt<<1|1);}void que(smx S,int l,int r,int rt){    if(tree[rt].y1 == S.yx && tree[rt].y2 == S.yh)    {        tree[rt].ceng += S.f;//标记该区间已被更新过        if(tree[rt].ceng>0)//代表该区间还要用到,且已经被更新过            tree[rt].jl = tree[rt].y2 - tree[rt].y1;        else if(r - l == 1)//代表叶子节点            tree[rt].jl = 0;        else//说明该节点没有被更新过,或是更新过后已经扫描过其所在矩形,没有用了            tree[rt].jl = tree[rt<<1].jl + tree[rt<<1|1].jl;        return ;    }    int mid = (l + r)>>1;    if(S.yh <= yy[mid])//y对应的后面的值(接下来要遍历的区间)全部在左区间上        que(S,l,mid,rt<<1);    else if(S.yx >= yy[mid])//y对应的前面的值(接下来要遍历的区间)全部在又区间上        que(S,mid,r,rt<<1|1);    else//既有左边的又有右边的    {        smx sw = S;        sw.yh = yy[mid];//用一个临时变量开始遍历左边那一部分        que(sw,l,mid,rt<<1);        sw = S;        sw.yx = yy[mid];//用一个临时变量开始遍历右边那一部分        que(sw,mid,r,rt<<1|1);    }    if(tree[rt].ceng>0)        tree[rt].jl = tree[rt].y2 - tree[rt].y1;    else if(r - l == 1)        tree[rt].jl = 0;    else        tree[rt].jl = tree[rt<<1].jl + tree[rt<<1|1].jl;}int main(){    int n,m,i,j,k;    int t = 1;    while(~scanf("%d",&n))    {        if(n == 0)            break;        double a,b,c,d;        int m = 1;        j = 1;        for(i = 0; i < n; i++)        {            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);            sm[m].x = a;//记录矩形的起始边            sm[m].yx = b;            sm[m].f = 1;            sm[m].yh = d;            yy[m++] = b;//将每条平行于x轴的边映射到y轴上,记录            sm[m].x = c;//记录矩形的终止边            sm[m].yx = b;            sm[m].f = -1;            sm[m].yh = d;            yy[m++] = d;        }        sort(yy+1,yy+m);//对y轴上的映射点从小到大排序        jianshu(1,m-1,1);//建立线段树        sort(sm+1,sm+m,cmp);//对映射到x轴上的x值进行从小到大排序        double ans = 0;        que(sm[0],1,m-1,1);        for(i = 1; i < m; i++)        {            ans += (sm[i].x - sm[i-1].x) * tree[1].jl;            que(sm[i],1,m-1,1);        }        printf("Test case #%d\n",t++);        printf("Total explored area: %.2lf\n\n",ans);//注意每组输入之间有一个空行    }    return 0;}


0 0