zoukankan      html  css  js  c++  java
  • Atlantis

    Atlantis

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


    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   |   We have carefully selected several similar problems for you:  1828 1255 1166 1540 1754 
     

     离散化+线段树+扫描线

    #include <bits/stdc++.h>
    #define maxn 10005
    struct Line
    {
        double x,y_down,y_up;
        int flag;
        bool operator <(const Line &a)
        {
            return x<a.x;
        }
    }line[maxn];
    struct Tree
    {
        double y_down,y_up;
        double x;
        int cover;//用以表示加进线段树的线段次数
        bool flag;
    }tree[maxn*100];
    int n;
    double xx1,yy1,xx2,yy2;
    int cnt=0;
    double y[2*maxn];
    void build(int l,int r,int rt)
    {
        tree[rt].x=-1;
        tree[rt].cover=0;
        tree[rt].y_down=y[l];
        tree[rt].y_up=y[r];
        tree[rt].flag=false;
        if(l+1==r)
        {
            tree[rt].flag=true;
            return;
        }
        int mid=(l+r)>>1;
        build(l,mid,rt*2);
        build(mid,r,rt*2+1);
    }
    double insert_tree(int rt,double x,double l,double r,int flag)//flag表示是左边还是右边
    {
        if(r<=tree[rt].y_down||l>=tree[rt].y_up)
        {
            return 0;
        }
        if(tree[rt].flag)
        {
            if(tree[rt].cover>0)
            {
                double temp_x=tree[rt].x;
                double ans=(x-temp_x)*(tree[rt].y_up-tree[rt].y_down);
                tree[rt].x=x;
                tree[rt].cover+=flag;
                return ans;
            }
            else
            {
                tree[rt].cover+=flag;
                tree[rt].x=x;
                return 0;
            }
        }
        double ans1,ans2;
        ans1=insert_tree(rt*2,x,l,r,flag);
        ans2=insert_tree(rt*2+1,x,l,r,flag);
        return ans1+ans2;
    }
    int main()
    {
        int n,i;
        int cas=0;
        while(std::cin>>n&&n)
        {
            cnt=1;
            for(int i=1;i<=n;i++)
            {
                scanf("%lf%lf%lf%lf",&xx1,&yy1,&xx2,&yy2);
                y[cnt]=yy1;
                line[cnt].x=xx1;
                line[cnt].y_down=yy1;
                line[cnt].y_up=yy2;
                line[cnt].flag=1;
                cnt++;
                y[cnt]=yy2;
                line[cnt].x=xx2;
                line[cnt].y_down=yy1;
                line[cnt].y_up=yy2;
                line[cnt].flag=-1;
                cnt++;
            }
            std::sort(y+1,y+cnt);
            std::sort(line+1,line+cnt);
            build(1,cnt-1,1);
            double ans=0;
            for(int i=1;i<cnt;i++)
            {    //std::cout<<"gg"<<std::endl;
                ans+=insert_tree(1,line[i].x,line[i].y_down,line[i].y_up,line[i].flag);
            }
             printf("Test case #%d
    Total explored area: %.2f
    
    ", ++cas, ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    java技术用ssh从linux服务器下载数据
    linux 常见操作命令
    IP地址归属地查询
    【转】Java检测字符串是否有乱码
    maven install 跳过test方法
    echarts实现动态传入数据刷新【可执行】
    echarts报错Cannot read property 'features' of undefined
    【java web】Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    程序员,我们都是夜归人【转】
    程序员你为什么这么忙?【转】
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9451266.html
Copyright © 2011-2022 走看看