zoukankan      html  css  js  c++  java
  • 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

    2
    10 10 20 20
    15 15 25 25.5
    0

    Sample Output

    Test case #1
    Total explored area: 180.00 

    Source

     
    解题:线段树+扫描线
     
      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
    ",cs++);
    100         printf("Total explored area: %.2f
    
    ",ans);
    101     }
    102     return 0;
    103 }
    View Code

    写线段这种玩意,还是得用这种[1,2)这种叶子的线段树,不然,蛋疼

      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 &p) const{
     28         return h < p.h;
     29     }
     30 };
     31 struct node{
     32     int lt,rt,cnt;
     33     double len;
     34 };
     35 node tree[maxn<<2];
     36 Line line[maxn<<1];
     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 + 1 == rt) return;
     44     int mid = (lt + rt)>>1;
     45     build(lt,mid,v<<1);
     46     build(mid,rt,v<<1|1);
     47 }
     48 void up(int v){
     49     if(tree[v].cnt){
     50         tree[v].len = lisan[tree[v].rt] - lisan[tree[v].lt];
     51         return;
     52     }else if(tree[v].lt + 1 == tree[v].rt){
     53         tree[v].len = 0;
     54         return;
     55     }
     56     tree[v].len = tree[v<<1].len + tree[v<<1|1].len;
     57 }
     58 void update(int lt,int rt,int d,int v){
     59     if(tree[v].lt >= lt && tree[v].rt <= rt){
     60         tree[v].cnt += d;
     61         up(v);
     62         return;
     63     }
     64     if(lt < tree[v<<1].rt) update(lt,rt,d,v<<1);
     65     if(rt > tree[v<<1|1].lt) update(lt,rt,d,v<<1|1);
     66     up(v);
     67 }
     68 int main() {
     69     int n,cs = 1,tot,cnt;
     70     double x1,y1,x2,y2;
     71     while(scanf("%d",&n),n){
     72         for(int i = tot = 0; i < n; ++i){
     73             scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
     74             line[tot] = Line(x1,x2,y1,1);
     75             lisan[tot++] = x1;
     76             line[tot] = Line(x1,x2,y2,-1);
     77             lisan[tot++] = x2;
     78         }
     79         sort(line,line+tot);
     80         sort(lisan,lisan+tot);
     81         for(int i = cnt = 1; i < tot; ++i){
     82             if(lisan[i] == lisan[cnt-1]) continue;
     83             lisan[cnt++] = lisan[i];
     84         }
     85         build(0,cnt-1,1);
     86         double ans = 0;
     87         for(int i = 0; i + 1 < tot; ++i){
     88             int lt = lower_bound(lisan,lisan+cnt,line[i].l)-lisan;
     89             int rt = lower_bound(lisan,lisan+cnt,line[i].r)-lisan;
     90             update(lt,rt,line[i].delta,1);
     91             ans += tree[1].len * (line[i+1].h - line[i].h);
     92         }
     93         printf("Test case #%d
    ",cs++);
     94         printf("Total explored area: %.2f
    
    ",ans);
     95     }
     96     return 0;
     97 }
     98 /*
     99 2
    100 10 10 20 20
    101 15 15 25 25.5
    102 */
    View Code
  • 相关阅读:
    stm32自带的flash分布图
    leetcode21
    使用redis缓存数据需要注意的问题以及个人的一些思考和理解
    Chapter 2 Open Book——2
    spring管理事务需要注意的
    如何避免在简单业务逻辑上面的细节上面出错
    leetcode387
    黑天鹅-简记
    java方法中只有值传递,没有引用传递
    Chapter 2 Open Book——1
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4046696.html
Copyright © 2011-2022 走看看