首页 > 代码库 > HDU 1542 Atlantis

HDU 1542 Atlantis

Atlantis

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1542
64-bit integer IO format: %I64d      Java class name: Main
 
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
 
解题:线段树+扫描线
 
  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cmath>  5 #include <algorithm>  6 #include <climits>  7 #include <vector>  8 #include <queue>  9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 200; 18 struct Line{ 19     double l,r,h; 20     int delta; 21     Line(double x = 0,double y = 0,double z = 0,int d = 0){ 22         l = x; 23         r = y; 24         h = z; 25         delta = d; 26     } 27     bool operator<(const Line &tmp) const{ 28         return h < tmp.h; 29     } 30 }; 31 Line line[maxn<<1]; 32 struct node{ 33     int lt,rt,cnt; 34     double len; 35 }; 36 node tree[maxn<<2]; 37 double lisan[maxn]; 38 void build(int lt,int rt,int v){ 39     tree[v].lt = lt; 40     tree[v].rt = rt; 41     tree[v].cnt = 0; 42     tree[v].len = 0; 43     if(lt == rt) return; 44     int mid = (lt + rt)>>1; 45     build(lt,mid,v<<1); 46     build(mid+1,rt,v<<1|1); 47 } 48 void pushup(int v){ 49     if(tree[v].cnt){ 50         tree[v].len = lisan[tree[v].rt+1] - lisan[tree[v].lt]; 51         return; 52     } 53     if(tree[v].lt == tree[v].rt){ 54         tree[v].len = 0; 55         return; 56     } 57     tree[v].len = tree[v<<1].len + tree[v<<1|1].len; 58 } 59 void update(int lt,int rt,double delta,int v){ 60     if(tree[v].lt == lt && tree[v].rt == rt){ 61         tree[v].cnt += delta; 62         pushup(v); 63         return; 64     } 65     int mid = (tree[v].lt + tree[v].rt)>>1; 66     if(rt <= mid) update(lt,rt,delta,v<<1); 67     else if(lt > mid) update(lt,rt,delta,v<<1|1); 68     else{ 69         update(lt,mid,delta,v<<1); 70         update(mid+1,rt,delta,v<<1|1); 71     } 72     pushup(v); 73 } 74 int main() { 75     int n,tot,cnt,cs = 1; 76     double x1,y1,x2,y2; 77     while(scanf("%d",&n),n){ 78         for(int i = tot = 0; i < n; ++i){ 79             scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); 80             line[tot] = Line(x1,x2,y1,1); 81             lisan[tot++] = x1; 82             line[tot] = Line(x1,x2,y2,-1); 83             lisan[tot++] = x2; 84         } 85         sort(line,line+tot); 86         sort(lisan,lisan+tot); 87         for(int i = cnt = 1; i < tot; ++i){ 88             if(lisan[i] == lisan[cnt-1]) continue; 89             lisan[cnt++] = lisan[i]; 90         } 91         build(0,cnt-1,1); 92         double ans = 0; 93         for(int i = 0; i+1 < tot; ++i){ 94             int lt = lower_bound(lisan,lisan+cnt,line[i].l)-lisan; 95             int rt = lower_bound(lisan,lisan+cnt,line[i].r)-lisan; 96             update(lt,rt-1,line[i].delta,1); 97             ans += tree[1].len*(line[i+1].h - line[i].h); 98         } 99         printf("Test case #%d\n",cs++);100         printf("Total explored area: %.2f\n\n",ans);101     }102     return 0;103 }
View Code

 

HDU 1542 Atlantis