zoukankan      html  css  js  c++  java
  • 算法复习——扫描线(hdu1542)

    题目:

    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

    题解:

      扫描线的模板题目···具体见http://blog.csdn.net/lwt36/article/details/48908031,%%%%%%%

      唯一需要注意的是线段树的l到r区间其实代表的是离散化后l到r+1这一段区间·····,因为最小的单位区间表示的是一条线段···而不是一个点

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<cstring>
    #include<string>
    #include<algorithm>
    using namespace std;
    const int N=505;
    struct node
    {
      double l,r,h;
      int op;
    }line[N];
    int n,m,tot,cnt[N*4],T;
    double ans,sum[N*4],b[N];
    inline bool cmp(node a,node b)
    {
      return a.h<b.h;
    }
    inline void update(int k,int l,int r)
    {
      if(cnt[k])  sum[k]=b[r+1]-b[l];
      else if(l==r)  sum[k]=0;
      else sum[k]=sum[k*2]+sum[k*2+1];
    }
    inline void modify(int k,int l,int r,int x,int y,int w)
    {
      if(l>=x&&r<=y)
      {
        cnt[k]+=w;update(k,l,r);
        return;
      }
      int mid=(l+r)/2;
      if(x<=mid)  modify(k*2,l,mid,x,y,w);
      if(y>mid)  modify(k*2+1,mid+1,r,x,y,w);
      update(k,l,r);
    }
    int main()
    {
      //freopen("a.in","r",stdin);
      while(true)
      {
        scanf("%d",&n);
        if(n==0)  break;T++;
        double X1,X2,Y1,Y2;tot=0;
        for(int i=1;i<=n;i++)
        {   
          scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2); 
          line[++tot].l=X1,line[tot].r=X2,line[tot].h=Y1;line[tot].op=1;b[tot]=X1;
          line[++tot].l=X1,line[tot].r=X2,line[tot].h=Y2;line[tot].op=-1;b[tot]=X2;
        }
        sort(line+1,line+tot+1,cmp);  
        sort(b+1,b+tot+1);
        m=unique(b+1,b+tot+1)-b-1;ans=0;
        memset(cnt,0,sizeof(cnt));
        memset(sum,0,sizeof(sum));
        for(int i=1;i<tot;i++)
        {
          int Le=lower_bound(b+1,b+m+1,line[i].l)-b;
          int Ri=lower_bound(b+1,b+m+1,line[i].r)-b;
          if(Le<Ri)  modify(1,1,m,Le,Ri-1,line[i].op);
          ans+=sum[1]*(line[i+1].h-line[i].h);
        }
        printf("Test case #%d
    Total explored area: %.2f
    
    ",T,ans);
      }
      return 0;
    }
     
  • 相关阅读:
    微信公众号菜单demo
    Hosts 广告
    thinkphp用swiftmailer发邮件demo
    微信小程序小结(4) -- 分包加载及小程序间跳转
    微信小程序小结(5) -- 常用语法
    常用SQL语句及在node中使用MySQL
    JavaScript -- tips
    CSS3 -- FlexBox(弹性盒子)
    gulp使用文档
    yarn快速使用及实践建议
  • 原文地址:https://www.cnblogs.com/AseanA/p/7593317.html
Copyright © 2011-2022 走看看