zoukankan      html  css  js  c++  java
  • POJ 1151 Atlantis

    Atlantis
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 11830   Accepted: 4588

    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

    //手贱的把flag写成了bool、然后给flag赋-1,然后就wa,然后一直在找错
    //离散化+扫描线,算面积的并

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define N 203
    #define lson l,m,k<<1
    #define rson m,r,k<<1|1
    using namespace std;
    double rcy[N];
    struct segment
    {
        int cover;
        double index;
    };
    segment st[N<<2];
    struct line
    {
        double x,y1,y2;
        int flag;
        bool operator<(const line&b)const
        {
            return x<b.x;
        }
    };
    line li[N];
    void build(int l,int r,int k)
    {
        if(r==l+1)
         {
          st[k].cover=0;  return ;
         }
         int m=(l+r)>>1;
         build(lson);
         build(rson);
    }
    double S;
    double index;
    int flag;
    void update(double &y1,double &y2,int l,int r,int k)
    {
        if(r==l+1)
        {
            if(st[k].cover)
             S+=(rcy[r]-rcy[l])*(index-st[k].index);
            st[k].cover+=flag;
            st[k].index=index;
            return ;
        }
         int m=(l+r)>>1;
         if(y1<rcy[m]) update(y1,y2,lson);
         if(y2>rcy[m]) update(y1,y2,rson);
    }
    int main()
    {
        int n;
        int i,j,k,t=1;
        double x1,x2,y1,y2;
        while(scanf("%d",&n),n)
        {
            for(j=i=0;i<n;i++)
            {
                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
                li[j].x=x1;li[j].flag=1;
                li[j].y1=y1;li[j].y2=y2;
                rcy[j]=y1;
                j++;
                rcy[j]=y2;
                li[j].x=x2;li[j].flag=-1;
                li[j].y1=y1;li[j].y2=y2;
                j++;
            }
           sort(rcy,rcy+j);
           sort(li,li+j);
           for(i=1,k=0;i<j;i++)
            if(rcy[i]!=rcy[k])
              rcy[++k]=rcy[i];
           build(0,k,1);S=0;
           for(i=0;i<j;i++)
           {
             flag=li[i].flag;
             index=li[i].x;
             update(li[i].y1,li[i].y2,0,k,1);
           }
           printf("Test case #%d\n",t++);
           printf("Total explored area: %.2lf\n\n",S);
        }
        return 0;
    }

    //学了下不用更新到底的方法
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define lson l,m,k<<1
    #define rson m,r,k<<1|1
    #define N 253
    using namespace std;
    struct node
    {
        double len;
        int cover;
    };
    struct line
    {
        double x,yup,ydown;
        int flag;
        bool operator <(const line&b)const
        {
            return x<b.x;
        }
    };
    node st[N<<2];
    line li[N];
    double rcy[N];
    void build(int l,int r,int k)
    {
      st[k].cover=st[k].len=0;
        if(r==l+1)//这里把1写成了l,检查了N久的错误、、、、
          return ;
       int m=(l+r)>>1;
       build(lson);
       build(rson);
    }
    void up(int &k,int &l,int &r)
    {
        if(st[k].cover)
        {
            st[k].len=rcy[r]-rcy[l];
        }else if(r==l+1)
                st[k].len=0;
              else
                st[k].len=st[k<<1].len+st[k<<1|1].len;
    }
    int flag;
    void update(double &dy,double &uy,int l,int r,int k)
    {
        if(dy<=rcy[l]&&uy>=rcy[r])
        {
            st[k].cover+=flag;
            up(k,l,r);
          return ;
        }
        int m=(l+r)>>1;
        if(dy<rcy[m]) update(dy,uy,lson);
        if(uy>rcy[m]) update(dy,uy,rson);
        up(k,l,r);
    }
    int main()
    {
        int n,i,j,k,t=1;
        double x1,x2,y1,y2;
        double s=0;
        while(scanf("%d",&n),n)
        {
           for(j=i=0;i<n;i++)
           {
               scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
               li[j].ydown=y1; li[j].yup=y2;
               li[j].x=x1;     li[j].flag=1;
               rcy[j]=y1; j++; rcy[j]=y2;
               li[j].ydown=y1; li[j].yup=y2;
               li[j].x=x2;    li[j++].flag=-1;
           }
           sort(li,li+j);
           sort(rcy,rcy+j);
           for(k=0,i=1;i<j;i++)
             if(rcy[i]!=rcy[k])
               rcy[++k]=rcy[i];
           build(0,k,1);s=0;
           j--;
           for(i=0;i<j;i++)
           {
             flag=li[i].flag;
             update(li[i].ydown,li[i].yup,0,k,1);
             s+=st[1].len*(li[i+1].x-li[i].x);
           }
           printf("Test case #%d\n",t++);
           printf("Total explored area: %.2lf\n\n",s);
        }
        return 0;
    }

  • 相关阅读:
    三元运算符嵌套问题
    tp框架利用redis存储session
    PHP date('W')遇到跨年问题
    AES加解密 集成 spring MVC
    python SMTP 发送邮件
    mysql数据库安装过程
    (转)Apache启动过程
    apache服务器全局配置详解
    Apache 的 httpd.conf 详解(很实用)
    安装apache遇到的问题总结
  • 原文地址:https://www.cnblogs.com/372465774y/p/2604761.html
Copyright © 2011-2022 走看看