zoukankan      html  css  js  c++  java
  • POJ 1151 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 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 

    思路:
    扫描线。

    从左向右扫,记左边界为(x1,y1,y2,1) 右边界 (x2, y1, y2, -1)
    将 2*n 条线排序,2*n 个纵坐标离散化之后
    记 数组c 保存第i个区间被覆盖的次数,若被覆盖数>0,则 ans += (x[i]-x[i-1])*(y2-y1);

    用线段树保存数组c,同时优化y1,y2

    代码如下:
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <functional>
    #include <iostream>
    
    using namespace std;
    const int maxn=220;
    
    struct Line{
        double x,l,r;
        int k;
        inline bool operator<(const Line &rhs) const {
            return x<rhs.x;
        }
    }line[maxn];
    
    struct node{
        double x,l,r,len;
        bool fg;
        int cnt;
    }tr[maxn<<2];
    
    double val[maxn];
    void pushup(int i){    tr[i].len=tr[i<<1].len+tr[i<<1|1].len;}
    void build(int l,int r,int i){
        tr[i].l=val[l],tr[i].r=val[r];
        tr[i].x=0;
        tr[i].cnt=0;
        tr[i].fg=false;
        tr[i].len=tr[i].r-tr[i].l;
        if(l+1==r) {
            tr[i].fg=true;
            return;
        }
        int mid=l+r>>1;
        build(l,mid,i<<1);
        build(mid,r,i<<1|1);
        pushup(i);
    }
    double update(double x,double l,double r,int k,int i){
        if(r<=tr[i].l || l>=tr[i].r) return 0;
        if(tr[i].fg){
            if(tr[i].cnt){
                double ans=(x-tr[i].x)*(tr[i].r-tr[i].l);
                tr[i].x=x;
                tr[i].cnt+=k;
                return ans;
            } else {
                tr[i].cnt+=k;
                tr[i].x=x;
                return 0;
            }
        }
        return update(x,l,r,k,i<<1)+update(x,l,r,k,i<<1|1);
    }
    
    int main(){
        int cas=1,n;
        while(scanf("%d",&n)==1,n){
            int cnt=0;
            for (int i=1; i<=n; i++){
                double x,xx,y,yy;
                scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
                val[++cnt]=y;
                line[cnt]=Line{x,y,yy,1};
                val[++cnt]=yy;
                line[cnt]=Line{xx,y,yy,-1};
            }
            sort(val+1,val+1+cnt);
            sort(line+1,line+1+cnt);
            build(1,cnt,1);
            double ans=0;
            for (int i=1; i<=cnt; i++)
                ans+=update(line[i].x,line[i].l,line[i].r,line[i].k,1);
            printf("Test case #%d
    Total explored area: %.2f
    
    ",cas++,ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    js 防止页面后退的方法
    asp.net 设置网页过期
    C#子类调用基类构造备忘
    asp.net 自定义控件 嵌入资源文件 备忘
    CSS实现高度和宽度自适应
    C# 更新SQL Server数据库备注信息从另一数据库
    asp.net mvc4 学习笔记一(基本原理)
    CommittableTransaction和TransactionScope
    Delphi7 错误: Access violation at address ****** in module 'ntdll.dll'. Read of address ******.
    Delphi XE10 IdFtp 错误:No FTP list parsers have been registered
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9845692.html
Copyright © 2011-2022 走看看