poj 1151 Atlantis (线段树+扫描线)

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

2

10 10 20 20

15 15 25 25.5

0

Sample Output

Test case #1

Total explored area: 180.00

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 using namespace std;
  5 struct p1
  6 {
  7     double x,y1,y2;
  8     int v;
  9 };p1 str[2010];
 10 bool com(p1 a,p1 b)
 11 {
 12     return a.x<b.x;
 13 }
 14 double y[2010];
 15 struct p2
 16 {
 17     int l,r,c;
 18     double s,ll,rr;
 19 };p2 tree[4100];
 20 void build(int l,int r,int p)
 21 {
 22     tree[p].l=l;tree[p].r=r;
 23     tree[p].s=0;
 24     tree[p].c=0;
 25     tree[p].ll=y[l];
 26     tree[p].rr=y[r];
 27     if (l+1==r) return ;
 28     int m=(l+r)/2;
 29     build(l,m,p*2);
 30     build(m,r,p*2+1);
 31 }
 32 void pushup(int p)
 33 {
 34     if (tree[p].c>0)
 35     {
 36         tree[p].s=tree[p].rr-tree[p].ll;
 37         return ;
 38     }
 39     if (tree[p].l+1==tree[p].r) tree[p].s=0;
 40     else tree[p].s=tree[p*2].s+tree[p*2+1].s;
 41 }
 42 void un(int p,p1 e)
 43 {
 44     if (e.y1==tree[p].ll&&e.y2==tree[p].rr)
 45     {
 46         tree[p].c+=e.v;
 47         pushup(p);
 48         return ;
 49     }
 50     if (e.y2<=tree[p*2].rr) un(p*2,e);
 51     else if (e.y1>=tree[p*2+1].ll) un(p*2+1,e);
 52     else
 53     {
 54         p1 ee=e;
 55         ee.y2=tree[p*2].rr;
 56         un(p*2,ee);
 57         ee=e;
 58         ee.y1=tree[p*2+1].ll;
 59         un(p*2+1,ee);
 60     }
 61     pushup(p);
 62 }
 63 int main()
 64 {
 65     int n,i,j,k=1,t;
 66     double x1,x2,y1,y2,ans;
 67     while (~scanf("%d",&n))
 68     {
 69         t=1;
 70         if (n==0) break;
 71         for (i=1;i<=n;i++)
 72         {
 73             scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
 74             str[t].x=x1;
 75             str[t].y1=y1;
 76             str[t].y2=y2;
 77             str[t].v=1;
 78             y[t]=y1;
 79             t++;
 80             str[t].x=x2;
 81             str[t].y1=y1;
 82             str[t].y2=y2;
 83             str[t].v=-1;
 84             y[t]=y2;
 85             t++;
 86         }
 87         sort(str+1,str+t,com);
 88         sort(y+1,y+t);
 89         build(1,t-1,1);
 90          ans=0.0;
 91         un(1,str[1]);
 92         for (i=2;i<t;i++)
 93         {
 94             ans+=tree[1].s*(str[i].x-str[i-1].x);
 95             un(1,str[i]);
 96         }
 97         printf("Test case #%d\n",k);
 98         printf("Total explored area: %.2lf\n\n",ans);
 99         k++;
100     }
101 }
时间: 2024-08-10 21:12:08

poj 1151 Atlantis (线段树+扫描线)的相关文章

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

一维离散化, 扫描线扫另一维, 用线段树维护 POJ建议交C++...G++貌似double要用%f ? 反正同一份代码C++AC,G++WA ------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 109; struct Line { double p,

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

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18061   Accepted: 6873 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

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K 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 is

POJ 1151 Atlantis 线段树+离散化

题目链接:http://poj.org/problem?id=1151  http://acm.hdu.edu.cn/showproblem.php?pid=1542 题目大意:给你几个矩形的坐标,求他们的面积的并. 解题思路:可以参考http://www.cnblogs.com/kuangbin/archive/2011/08/16/2140544.html,实际上就是一个方向直接算出,另一个方向使用线段树维护已经覆盖的底边的长度.具体操作不算复杂:假想一条扫描下从下往上开始进行扫描,初始时候

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

题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1542 Atlantis (线段树+扫描线)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18559    Accepted Submission(s): 7523 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

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

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i