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

    Atlantis
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 22662   Accepted: 8478

    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

     
    题意:求几个矩形覆盖的总面积
    线段树+扫描线
    离散化x坐标,扫描线自下而上扫
    标记矩形下边界为1,上边界为-1,线段树维护当前这根线的长度
    线段树的每个节点代表的是这个节点右边一小段的长度
    所以修改区间[l,r]是修改区间[l,r-1]
    每次的修改up上去,计算使用1号点的sum,
    这样可以避免标记的下传
    解释一下up函数:
    如果这个区间有标记,即区间完全被矩形边界覆盖,无论标记有多少,他的长度只能是区间长度
    如果这个区间没有标记,即区间部分被矩形边界覆盖,就由子节点合并
    叶子节点没有子节点,所以特判
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int n,cnt;
    int opl,opr,w;
    double x1[101],x2[101],y1[101],y2[101],hash[201],ans;
    struct LINE
    {
        int xl,xr,f;
        double h;
    }line[201];
    struct node
    {
        int l,r,f;
        double sum;
    }tr[201*4];
    bool cmp(LINE p,LINE q)
    {
        return p.h<q.h;
    }
    void build(int k,int l,int r)
    {
        tr[k].l=l; tr[k].r=r;
        if(l==r) return;
        int mid=l+r>>1;
        build(k<<1,l,mid);
        build(k<<1|1,mid+1,r);
    }
    void up(int k)
    {
        if(tr[k].f) tr[k].sum=hash[tr[k].r+1]-hash[tr[k].l];
        else if(tr[k].l==tr[k].r) tr[k].sum=0;
        else tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
    }
    void change(int k)
    {
        if(tr[k].l>=opl&&tr[k].r<=opr)
        {
            tr[k].f+=w;
            up(k);
            return;
        }
        int mid=tr[k].l+tr[k].r>>1;
        if(opl<=mid) change(k<<1);
        if(opr>mid) change(k<<1|1);
        up(k);
    }
    int main()
    {
        int T=0;
        while(scanf("%d",&n)!=EOF)
        {
            if(!n) return 0;
            T++; cnt=0; ans=0;
            for(int i=1;i<=n;i++) 
            {
                scanf("%lf%lf%lf%lf",&x1[i],&y1[i],&x2[i],&y2[i]);
                hash[++cnt]=x1[i]; hash[++cnt]=x2[i];
            }
            sort(hash+1,hash+cnt+1);
            cnt=unique(hash+1,hash+cnt+1)-(hash+1);
            for(int i=1;i<=n;i++)
            {
                line[i*2-1].xl=lower_bound(hash+1,hash+cnt+1,x1[i])-hash;
                line[i*2].xl=line[i*2-1].xl;
                line[i*2-1].xr=lower_bound(hash+1,hash+cnt+1,x2[i])-hash;
                line[i*2].xr=line[i*2-1].xr;
                line[i*2-1].h=y1[i]; 
                line[i*2].h=y2[i];
                line[i*2-1].f=1;
                line[i*2].f=-1;
            }
            sort(line+1,line+2*n+1,cmp);
            build(1,1,cnt);
            for(int i=1;i<=2*n;i++)
            {
                opl=line[i].xl; opr=line[i].xr-1;
                w=line[i].f;
                change(1);
                ans+=tr[1].sum*(line[i+1].h-line[i].h);
            }
            printf("Test case #%d
    Total explored area: %.2lf
    
    ",T,ans);
        }
    }
  • 相关阅读:
    dede图片横向滚动
    dede各种运用[转]
    PROFIBUS-DP现场总线的结构及应用
    51单片机的中断优先级及中断嵌套
    WPF里面制作圆角文本框
    【转】什么叫51单片机最小系统
    【转】(C#)OPC客户端源码
    路漫漫其修远兮,吾要上下左右前后而求索
    二叉树创建为什么用二级指针
    无向图的邻接表创建
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6828162.html
Copyright © 2011-2022 走看看