zoukankan      html  css  js  c++  java
  • HDU1542 扫描线+离散化

    Atlantis

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


    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
     

     题意:

    x轴向右,y轴向下的坐标平面内,求矩形并的面积。坐标值非整数(0~100000).

    代码:

    //坐标值是double,把用到的每个坐标离散化。然后再扫描线。
    //注意:int rig=Bsearch(nodes[i].r,m,mp)-1;和sum[rt]=mp[r+1]-mp[l];
    //离散化之后a~a+1是有长度的。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=205;
    int cnt[maxn*4];
    double sum[maxn*4],mp[maxn*4];
    struct node
    {
        double l,r,h;
        int d;
        node(){}
        node(double a,double b,double c,int d):l(a),r(b),h(c),d(d){}
        bool operator < (node &p){
            if(h==p.h) return d>p.d;
            return h<p.h;
        }
    }nodes[maxn*4];
    int Bsearch(double a,int b,double *c)
    {
        int l=0,r=b-1,mid;
        while(l<=r){
            mid=(l+r)>>1;
            if(c[mid]==a) return mid;
            else if(c[mid]>a) r=mid-1;
            else l=mid+1;
        }
        return -1;
    }
    void Pushup(int l,int r,int rt)
    {
        if(cnt[rt])
            sum[rt]=mp[r+1]-mp[l];
        else if(l==r) sum[rt]=0;
        else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    void Update(int ql,int qr,int c,int l,int r,int rt)
    {
        if(ql<=l&&qr>=r){
            cnt[rt]+=c;
            Pushup(l,r,rt);
            return;
        }
        int m=(l+r)>>1;
        if(ql<=m) Update(ql,qr,c,l,m,rt<<1);
        if(qr>m) Update(ql,qr,c,m+1,r,rt<<1|1);
        Pushup(l,r,rt);
    }
    int main()
    {
        int n,cas=0;
        while(scanf("%d",&n)&&n){
            double x1,x2,y1,y2;
            int m=0,nu=0;
            for(int i=0;i<n;i++){
                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
                nodes[nu++]=node(x1,x2,y1,1);
                nodes[nu++]=node(x1,x2,y2,-1);
                mp[m++]=x1;mp[m++]=x2;
            }
            sort(mp,mp+m);
            m=unique(mp,mp+m)-mp;
            sort(nodes,nodes+nu);
            memset(sum,0,sizeof(sum));
            memset(cnt,0,sizeof(cnt));
            double ans=0;
            for(int i=0;i<nu-1;i++){
                int lef=Bsearch(nodes[i].l,m,mp);
                int rig=Bsearch(nodes[i].r,m,mp)-1;
                if(lef<=rig)
                Update(lef,rig,nodes[i].d,0,m-1,1);
                ans+=sum[1]*(nodes[i+1].h-nodes[i].h);
            }
            printf("Test case #%d
    ",++cas);
            printf("Total explored area: %.2lf
    
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    再谈低代码开发平台(200727)【转】
    JS 动态加载脚本的4种方法【转】
    mdadm Centos7 软RAID0安装配置
    Program to print all palindromes in a given range打印范围内的回文
    Python | Reverse Slicing of given string倒排并切割字符串
    Reverse string in Python (5 different ways)倒排字符串
    安装pip3
    yum命令的使用与createrepo自建仓库教程
    你了解Redis的持久化吗?
    Elasticsearch面试题汇总与解析
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6609687.html
Copyright © 2011-2022 走看看