HDU 1542 Atlanta

Source: Internet
Author: User
Atlanta tistime limit: 1000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 1542
64-bit integer Io format: % i64d Java class name: Main there are several except ent Greek texts that contain descriptions of the fabled island Atlanta. some of these texts even include maps of parts of the island. but unfortunately, these maps describe different regions of Atlanta. your friend Bill has to know the total area for which maps exist. you (unwisely) volunteered to write a program that calculates this quantity.
Inputthe 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. outputfor each test case, your program shocould 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 occupied ed area: a", where A is the total occupied ed 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 
Sourcemid-Central European Regional Contest 2000 solution: Line Segment tree + scanned line
  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 Atlanta

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.