• POJ·1151 Atlantis·线段树求矩形面积并


    题目在这: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 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

     
     
    还是比较裸的线段树求矩形面积并,与上一题基本相同,不同的是这题坐标是小数,需要离散。
    离散的时候学习了一个函数:unique函数,功能是将数组相邻的重复元素放到最后面,返回最后面的地址。
      用的时候需要对数组进行排序,长度 = unique(name,name+len) - name - 1
    codes:
     1 #include<set>
     2 #include<queue>
     3 #include<vector>
     4 #include<cstdio>
     5 #include<cstdlib>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 const int N = 110;
    11 #define Ch1 (i<<1)
    12 #define Ch2 (Ch1|1)
    13 #define mid(i) T[i].mid
    14 #define len(i) T[i].r - T[i].l
    15 #define rlen(i) T[i].rr - T[i].rl
    16 #define For(i,n) for(int i=1;i<=n;i++)
    17 #define Rep(i,l,r) for(int i=l;i<=r;i++)
    18 
    19 struct tnode{
    20     int l,r,mid,cover;
    21     double rl,rr,len;
    22 }T[N<<8];
    23 
    24 struct lines{
    25     double l,r,h;
    26     int kind;//1表示上底边 
    27 }L[N*2];
    28 int n,Lim,tot,kth;
    29 double x1,y1,x2,y2,cor[N*2],ans;
    30 
    31 bool cmp(lines A,lines B){
    32     return A.h<B.h;
    33 }
    34 
    35 void init(){
    36     tot = 0;ans = 0;
    37     For(i,n){
    38         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
    39         L[++tot].l = x1; L[tot].r = x2; L[tot].h = y1; L[tot].kind = 0; cor[tot] = x1;
    40         L[++tot].l = x1; L[tot].r = x2; L[tot].h = y2; L[tot].kind = 1; cor[tot] = x2;
    41     }
    42     sort(L+1,L+tot+1,cmp);
    43     sort(cor+1,cor+tot+1);
    44     Lim = unique(cor+1,cor+tot+1) - cor - 1;
    45 }
    46 
    47 void Build(int l,int r,int i){
    48     T[i].l = l; T[i].r = r; T[i].mid = (l+r)>>1;
    49     T[i].rl = cor[l]; T[i].rr = cor[r];
    50     if(l==r-1) return;
    51     Build(l,mid(i),Ch1); Build(mid(i),r,Ch2);
    52 }
    53 
    54 void Modify(int i,double l,double r,int delta){
    55     if(l==T[i].rl&&T[i].rr==r){
    56         T[i].cover+=delta;
    57         if(T[i].cover>0)        T[i].len = rlen(i);else 
    58         if(T[i].l+1==T[i].r)    T[i].len = 0;      else                        
    59                                 T[i].len = T[Ch1].len + T[Ch2].len;
    60         return;
    61     }
    62     double Mid = cor[mid(i)];
    63     if(r<=Mid)  Modify(Ch1,l,r,delta);else
    64     if(l>=Mid)  Modify(Ch2,l,r,delta);else
    65     Modify(Ch1,l,Mid,delta),Modify(Ch2,Mid,r,delta);
    66     if(T[i].cover>0)            T[i].len = rlen(i);
    67     else                        T[i].len = T[Ch1].len + T[Ch2].len; 
    68 } 
    69 
    70 int main(){
    71     scanf("%d",&n);
    72     while(n){
    73         init();kth++; 
    74         Build(1,Lim,1);L[0].h = L[1].h;
    75         For(i,tot){
    76             if(!L[i].kind){
    77                 ans+=T[1].len * (L[i].h - L[i-1].h);
    78                 Modify(1,L[i].l,L[i].r,1); 
    79             }else{
    80                 ans+=T[1].len * (L[i].h - L[i-1].h);
    81                 Modify(1,L[i].l,L[i].r,-1);
    82             }
    83         }
    84         printf("Test case #%d
    ",kth);
    85         printf("Total explored area: %.2f
    ",ans);
    86         printf("
    ");
    87         scanf("%d",&n);
    88         if(!n) return 0;
    89     }
    90     return 0;
    91 }

     

  • 相关阅读:
    【GIT-精讲】从零玩转Git-基础理论
    【fmjava】 面试题突击训练-Java基础语法篇01
    【笔记】springSecurity-OAuth2.0-授权模式演示
    【难受】SpirngCloud-Alibaba-nacos跨服务器访问接口的问题
    Python编程题汇总(持续更新中……)
    Python编程题14--随机分配礼物
    Python编程题13--判断两个升序列表,其中一个是另外一个的子集
    Python编程题12--列表中比前面元素都大,比后面元素都小的数
    Python编程题11--找出100以内的质数
    Python编程题10--找出和为N的两个数
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3815481.html
Copyright © 2020-2023  润新知