poj 1151Atlantis线段树+扫面线

来源:互联网 发布:会计初级软件下载 编辑:程序博客网 时间:2024/05/16 05:38
Atlantis
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17736 Accepted: 6754

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 <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define FOR(a,b) for(int i =  a ; i < b ; i++)#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define FOPENIN(IN) freopen(IN, "r", stdin)#define FOPENOUT(OUT) freopen(OUT, "w", stdout)const int N = 300;struct Node{    double x,y1,y2;    int flag ;}node[N];//把一段段平行于y轴的线段表示成数组 ,//x是线段的x坐标,y1,y2线段对应的下端点和上端点的坐标//一个矩形 ,左边的那条边f为1,右边的为-1,//用来记录重叠情况,可以根据这个来计算,nod节点中的cbool cmp(Node a,Node b){    return a.x  - b.x <0.0000001;}struct node{    int l,r;//线段树左右的整点    double ml;double mr;//分别对应左右真实的浮点数端点    int s;double len ;//s用来记录重叠情况,len计算实际长度}a[N*3];double y[N];//记录y坐标的数组void build(int i,int l,int r){    a[i].l = l ;    a[i].r = r;    a[i].ml = y[l];    a[i].mr = y[r];    a[i].s = 0 ;    a[i].len = 0;    if(a[i].l + 1 == a[i].r){return ;}    int mid =(l+r)>>1;    build(i<<1,l,mid);    build(i<<1|1,mid,r);}void callen(int i ){<span style="font-family: Arial, Helvetica, sans-serif; color: rgb(0, 128, 0);">//计算线</span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(0, 128, 0);">的长度</span>    if(a[i].s > 0 )        a[i].len = a[i].mr -a[i].ml;    else if(a[i].r - a[i].l == 1)        a[i].len = 0 ;    else        a[i].len = a[i<<1].len+a[i<<1|1].len;  }void update(int i,Node b){//加入线段b,从第一条开始,更新线段树    if(a[i].ml == b.y1 && a[i].mr == b.y2){        a[i].s += b.flag ;        callen(i);        return ;    }    if(b.y2 <= a[i<<1].mr )  update(i<<1,b);    else if(b.y1 >= a[i<<1|1].ml) update(i<<1|1,b);    else {        Node temp = b;        temp.y2 = a[i<<1].mr;        update(i<<1,temp);        temp =b ;        temp.y1 = a[i<<1|1].ml;        update(i<<1|1,temp);    }    callen(i);}int main(){    int n,k=1;    while(scanf("%d",&n),n){        int t= 1 ;        double x1,y1,x2,y2;        for(int i = 0 ; i < n ; i++){            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            node[t].x= x1;            node[t].y1 = y1;            node[t].y2 = y2;            node[t].flag = 1;//入边            y[t++] = y1;            node[t].x = x2;            node[t].y1 = y1;            node[t].y2 = y2;            node[t].flag = -1;            y[t++] = y2;        }        sort(node+1,node+t,cmp);        sort(y+1,y+t);        build(1,1,t-1);        update(1,node[1]);//先加入第一条线段        double sum = 0 ;        for(int i = 2 ; i < t ; i++){            double tt = a[1].len * (node[i].x - node[i-1].x);            sum+=tt;            update(1,node[i]);//从第二条开始加入        }        printf("Test case #%d\n",k++);        printf("Total explored area: %.2f\n\n",sum);    }    return 0;}


 

0 0
原创粉丝点击