poj1151 Atlantis && cdoj 1600艾尔大停电 矩形面积并

 

题目:

Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23758   Accepted: 8834

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

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

Source

 
  思路:
    矩形面积并,复习下。
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <cstring>
 6 
 7 using namespace std;
 8 
 9 #define MP make_pair
10 #define PB push_back
11 typedef long long LL;
12 typedef pair<int,int> PII;
13 const double eps=1e-8;
14 const double pi=acos(-1.0);
15 const int K=3e2+7;
16 const int mod=1e9+7;
17 
18 struct node
19 {
20     int f;
21     double l,r,y;
22     bool operator < (const node &ta)const
23     {
24         return y<ta.y;
25     }
26 }seg[K*2];
27 int cover[K*8];
28 double sum[K*8],hs[K*2];
29 void push_up(int o,int l,int r)
30 {
31     if(cover[o])    sum[o]=hs[r+1]-hs[l];
32     else if(l==r)   sum[o]=0;
33     else sum[o]=sum[o<<1]+sum[o<<1|1];
34 }
35 void update(int o,int l,int r,int nl,int nr,int f)
36 {
37     if(l==nl&&r==nr)
38         cover[o]+=f,push_up(o,l,r);
39     else
40     {
41         int mid=l+r>>1;
42         if(nr<=mid) update(o<<1,l,mid,nl,nr,f);
43         else if(nl>mid) update(o<<1|1,mid+1,r,nl,nr,f);
44         else    update(o<<1,l,mid,nl,mid,f),update(o<<1|1,mid+1,r,mid+1,nr,f);
45         push_up(o,l,r);
46     }
47 }
48 int main(void)
49 {
50     int n,cs=1;
51     while(scanf("%d",&n)&&n)
52     {
53         int cnt=0;
54         double ans=0;
55         memset(cover,0,sizeof cover);
56         memset(sum,0,sizeof sum);
57         for(int i=1;i<=n;i++)
58         {
59             double lx,ly,rx,ry;
60             scanf("%lf%lf%lf%lf",&lx,&ly,&rx,&ry);
61             seg[cnt+1].l=seg[cnt+2].l=lx;
62             seg[cnt+1].r=seg[cnt+2].r=rx;
63             seg[cnt+1].y=ry,seg[cnt+2].y=ly;
64             seg[cnt+1].f=1,seg[cnt+2].f=-1;
65             hs[cnt+1]=lx,hs[cnt+2]=rx;
66             cnt+=2;
67         }
68         sort(seg+1,seg+1+n*2);
69         sort(hs+1,hs+1+2*n);
70         int sz=unique(hs+1,hs+1+n*2)-hs;
71         for(int i=1;i<2*n;i++)
72         {
73             int l=lower_bound(hs+1,hs+sz,seg[i].l)-hs;
74             int r=lower_bound(hs+1,hs+sz,seg[i].r)-hs;
75             update(1,1,sz,l,r-1,seg[i].f);
76             ans+=sum[1]*(seg[i+1].y-seg[i].y);
77         }
78         printf("Test case #%d\nTotal explored area: %.2f\n\n",cs++,ans);
79     }
80     return 0;
81 }

 

posted @ 2017-10-11 15:22  weeping  阅读(482)  评论(0编辑  收藏  举报