zoukankan      html  css  js  c++  java
  • HDU 1542 Atlantis(线段树面积并)

     描述
    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
    题意
    给你N个矩形,每个矩形的左下和右上的点,求面积并
    题解
    由于点不是很多,可以对x轴进行离散化,去重
    线段树面积并
    代码
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 const int N=2000;
     7 
     8 int col[N<<2];
     9 double sum[N<<2],x[N<<2];
    10 
    11 struct seg
    12 {
    13     double l,r,h;
    14     int s;
    15     seg(){}
    16     seg(double l,double r,double h,int s):l(l),r(r),h(h),s(s){}
    17     bool operator<(const seg &D)const{
    18         return h<D.h;
    19     }
    20 }s[N];
    21 void PushUp(int rt,int l,int r)
    22 {
    23     if(col[rt])sum[rt]=x[r+1]-x[l];
    24     else if(l==r)sum[rt]=0;
    25     else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    26 }
    27 void Update(int L,int R,int C,int l,int r,int rt)
    28 {
    29     if(L<=l&&r<=R)
    30     {
    31         col[rt]+=C;
    32         PushUp(rt,l,r);
    33         return;
    34     }
    35     int mid=(l+r)>>1;
    36     if(L<=mid)Update(L,R,C,l,mid,rt<<1);
    37     if(R>mid)Update(L,R,C,mid+1,r,rt<<1|1);
    38     PushUp(rt,l,r);
    39 }
    40 int main()
    41 {
    42     int n,o=1;
    43     double a,b,c,d;
    44     while(~scanf("%d",&n),n)
    45     {
    46         int cnt=0;
    47         for(int i=0;i<n;i++)
    48         {
    49             scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
    50             s[++cnt]=seg(a,c,b,1);
    51             x[cnt]=a;
    52             s[++cnt]=seg(a,c,d,-1);
    53             x[cnt]=c;
    54         }
    55         sort(x+1,x+1+cnt);
    56         sort(s+1,s+1+cnt);
    57         int k=1;
    58         for(int i=2;i<=cnt;i++)
    59             if(x[i]!=x[i-1])
    60                 x[++k]=x[i];
    61 
    62         memset(sum,0,sizeof sum);
    63         memset(col,0,sizeof col);
    64         double ans=0;
    65         for(int i=1;i<cnt;i++)
    66         {
    67             int l=lower_bound(x+1,x+1+k,s[i].l)-x;
    68             int r=lower_bound(x+1,x+1+k,s[i].r)-x-1;
    69             Update(l,r,s[i].s,1,k,1);
    70             ans+=sum[1]*(s[i+1].h-s[i].h);
    71         }
    72         printf("Test case #%d
    Total explored area: %.2f
    
    ",o++,ans);
    73     }
    74     return 0;
    75 }
     
  • 相关阅读:
    面试题(三)
    面试题(二)
    经典面试题(一)
    $.ajax()实现简单计算器
    [hdu5373 The shortest problem]模拟
    [hdu5371 Hotaru's problem]最大回文半径
    [hdu5372 Segment Game]树状数组
    [zoj3813]Alternating Sum 公式化简,线段树
    [hdu5348]图上找环,删环
    [hdu5360]贪心
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9291597.html
Copyright © 2011-2022 走看看