• [HDU1542]Atlantis(扫描线+线段树)


    Atlantis

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 16436    Accepted Submission(s): 6706


    Problem 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 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
     
    Recommend
    linle
     
    Statistic | Submit | Discuss | Note

    注意只要给一维离散化,且离散化的那一维必然是左闭右开的。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define ls (x<<1)
     5 #define rs (ls|1)
     6 #define lson ls,L,mid
     7 #define rson rs,mid+1,R
     8 #define rep(i,l,r) for (int i=l; i<=r; i++)
     9 using namespace std;
    10 
    11 const int N=2010;
    12 int n,cnt,T,tot,cov[N<<2];
    13 double x1,y1,x2,y2,ans,b[N],len[N<<2];
    14 struct P{ double x,y1,y2; int k; }p[N];
    15 
    16 bool cmp(P &a,P &b){ return a.x<b.x; }
    17 
    18 void upd(int x,int L,int R){
    19     if (cov[x]) len[x]=b[R+1]-b[L];
    20     else if (L==R) len[x]=0; else len[x]=len[ls]+len[rs];
    21 }
    22 
    23 void mdf(int x,int L,int R,int l,int r,int k){
    24     if (L==l && r==R){ cov[x]+=k; upd(x,L,R); return; }
    25     int mid=(L+R)>>1;
    26     if (r<=mid) mdf(lson,l,r,k);
    27     else if (l>mid) mdf(rson,l,r,k);
    28         else mdf(lson,l,mid,k),mdf(rson,mid+1,r,k);
    29     upd(x,L,R);
    30 }
    31 
    32 int main(){
    33     while (scanf("%d",&n),n){
    34         T++; cnt=tot=ans=0;
    35         memset(len,0,sizeof(len)); memset(cov,0,sizeof(cov));
    36         rep(i,1,n){
    37             scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
    38             p[++cnt]=(P){x1,y1,y2,1}; p[++cnt]=(P){x2,y1,y2,-1};
    39             b[++tot]=y1; b[++tot]=y2;
    40         }
    41         sort(b+1,b+tot+1); tot=unique(b+1,b+tot+1)-b-1;
    42         sort(p+1,p+cnt+1,cmp);
    43         rep(i,1,cnt-1)
    44             mdf(1,1,tot,lower_bound(b+1,b+tot+1,p[i].y1)-b,lower_bound(b+1,b+tot+1,p[i].y2)-b-1,p[i].k),ans+=len[1]*(p[i+1].x-p[i].x);
    45         printf("Test case #%d
    Total explored area: %.2lf
    
    ",T,ans);
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    函数之返回值
    函数之初识函数
    三元运算符
    枚举enumerate
    模块
    迭代器
    斐波那契
    leetcode155 最小栈
    leetcode94 二叉树的中序遍历
    leetcode20 有效的括号
  • 原文地址:https://www.cnblogs.com/HocRiser/p/8823355.html
Copyright © 2020-2023  润新知