Atlantis(线段树+扫描线+离散化)

来源:互联网 发布:淘宝代购可以退货吗 编辑:程序博客网 时间:2024/06/04 23:19

Atlantis

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 4
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
 

Source
Mid-Central European Regional Contest 2000
 

Statistic | Submit | Back


第一次接触扫描线的题目,前面讲解过扫描线(http://blog.csdn.net/sinat_37668729/article/details/78233748),看了别人的代码。。。记得今年的网络赛中出现一个类似的题目

题意:

求不重合的矩形的总面积

思路:

就是运用扫描线,然后区间更新,求长度,然后再根据记录计算高度,最终得出面积,注意需要离散化y,对x进行排序


代码:

#include <iostream>  #include <cstdio> #include <cstring> #include <algorithm>  using namespace std; #define L(rt) (rt<<1)  #define R(rt) (rt<<1|1)#define INF 0x3f3f3f3f#define maxn 300  struct Node{      double x;double y1;double y2;      int flag;  //标记叶子结点}node[maxn]; bool cmp(Node a,Node b){      return a.x<b.x;  }double y[maxn];  //记录离散化后点对应的y值  struct node{      int l;int r;double ml;double mr;int s;double len;  }a[maxn*3];  void build(int i,int left,int right){      a[i].l=left;    a[i].r=right;    a[i].ml=y[left];    a[i].mr=y[right];     a[i].s=0;    a[i].len=0;    if(a[i].l+1==a[i].r){          return;    }    int mid=(left+right)>>1;      build(L(i),left,mid);      build(R(i),mid,right);//建树时注意这里不是mid+1,因为做相减的时候如果mid+1这么建回到值左孩子的右边与有孩子的左边无法进行运算 }  void callen(int i){        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[L(i)].len+a[R(i)].len;      }      return;  }void updata(int i,Node b){      if(a[i].ml==b.y1&&a[i].mr==b.y2){          a[i].s+=b.flag;        callen(i);        return ;      }    if(b.y2<=a[L(i)].mr) updata(L(i),b);      else if(b.y1>=a[R(i)].ml) updata(R(i),b);      else{          Node temp=b;          temp.y2=a[L(i)].mr;          updata(L(i),temp);          temp=b;        temp.y1=a[R(i)].ml;          updata(R(i),temp);      }      callen(i);      return ;  }int main(){      int n,ans,cas=1,te;      double x1,x2,y1,y2;      while(scanf("%d",&n),n){             ans=1;              for(int i=0;i<n;i++){              scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);              node[ans].x=x1;              node[ans].y1=y1;              node[ans].y2=y2;              node[ans].flag=1;//入边            y[ans++]=y1;            node[ans].x=x2;              node[ans].y1=y1;              node[ans].y2=y2;              node[ans].flag=-1;//出边            y[ans++]=y2;        }        sort(node+1,node+ans,cmp);          sort(y+1,y+ans);         build(1,1,ans-1);          updata(1,node[1]);         double sum=0;          for(int i=2;i<ans;i++){              sum+=a[1].len*(node[i].x-node[i-1].x);              updata(1,node[i]);        }         printf("Test case #%d\n",cas++);          printf("Total explored area: %.2lf\n\n",sum);      }      return 0;  }  


原创粉丝点击