zoukankan      html  css  js  c++  java
  • hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis

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


     

    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 1698 1540 1754 

    【题意】:

    给你n个矩形的左下角(x1,y1),右上角(x2,y2),让你求n个矩形面积的并

    【分析】:见这里

    【代码】:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define m(s) memset(s,0,sizeof s)
    #define lc k<<1
    #define rc k<<1|1
    using namespace std;
    const int N=1e5+10;
    struct node{
        double xl,xr,h;
        int id;
        bool operator < (const node &a)const{
            return h<a.h;
        }
    }b[N];
    int n,kase,tag[N<<2];
    double sum[N<<2],c[N];
    void updata(int k,int l,int r){
        if(tag[k]) sum[k]=c[r+1]-c[l];//当前区间被线段覆盖
        else if(l==r) sum[k]=0;//已经到了叶子结点且没有被覆盖
        else sum[k]=sum[lc]+sum[rc];// 没有完全被覆盖,但是还没到叶子结点,从其子区间中获得信息
    }
    void change(int k,int l,int r,int x,int y,int val){
        if(x<=l&&r<=y){
            tag[k]+=val;updata(k,l,r);
            return ;
        }
        int mid=l+r>>1;
        if(x<=mid) change(lc,l,mid,x,y,val);
        if(y>mid) change(rc,mid+1,r,x,y,val);
        updata(k,l,r);
    }
    int main(){
        while(~scanf("%d",&n)&&n){
            int cnt=0;double ans=0;
            double x1,x2,y1,y2;
            m(sum);m(tag);
            for(int i=1;i<=n;i++){
                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
                b[++cnt]=(node){x1,x2,y1,1};c[cnt]=x1;
                b[++cnt]=(node){x1,x2,y2,-1};c[cnt]=x2;
            }
            sort(b+1,b+cnt+1);
            sort(c+1,c+cnt+1);
            int t=unique(c+1,c+cnt+1)-(c+1);
            for(int i=1;i<cnt;i++){
                x1=lower_bound(c+1,c+t+1,b[i].xl)-c;
                x2=lower_bound(c+1,c+t+1,b[i].xr)-c-1;
                change(1,1,t,x1,x2,b[i].id);
                ans+=(b[i+1].h-b[i].h)*sum[1];
            }
            printf("Test case #%d
    ",++kase);  
            printf("Total explored area: %.2f
    
    ",ans);
        }
        return 0;
    }
    /*
    奉送一组数据
    输入:
    3
    10 10 20 20
    15 15 25 30
    22 8 28 17
    
    输出:
    Test case #1
    Total explored area: 273.00
    
    再送大家一道题:poj 1389
    整体思路一模一样,只是一个为整数,一个为实数,输入输出格式不一样。。
    */

    -------------------------------------

    -------------------------------------

    poj 1389

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define m(s) memset(s,0,sizeof s)
    #define lc k<<1
    #define rc k<<1|1
    using namespace std;
    const int N=1e5+10;
    struct node{
        int xl,xr,h;
        int id;
        bool operator < (const node &a)const{
            return h<a.h;
        }
    }b[N];
    int n,kase,tag[N<<2];
    int sum[N<<2],c[N];
    void updata(int k,int l,int r){
        if(tag[k]) sum[k]=c[r+1]-c[l];
        else if(l==r) sum[k]=0;
        else sum[k]=sum[lc]+sum[rc];
    }
    void change(int k,int l,int r,int x,int y,int val){
        if(x<=l&&r<=y){
            tag[k]+=val;updata(k,l,r);
            return ;
        }
        int mid=l+r>>1;
        if(x<=mid) change(lc,l,mid,x,y,val);
        if(y>mid) change(rc,mid+1,r,x,y,val);
        updata(k,l,r);
    }
    int main(){
        int x1,x2,y1,y2;
        while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2),~x1){
            int cnt=0,ans=0;m(sum);m(tag);
            b[++cnt]=(node){x1,x2,y1,1};c[cnt]=x1;
            b[++cnt]=(node){x1,x2,y2,-1};c[cnt]=x2;
            while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2),~x1){
                b[++cnt]=(node){x1,x2,y1,1};c[cnt]=x1;
                b[++cnt]=(node){x1,x2,y2,-1};c[cnt]=x2;
            }
            sort(b+1,b+cnt+1);
            sort(c+1,c+cnt+1);
            int t=unique(c+1,c+cnt+1)-(c+1);
            for(int i=1;i<cnt;i++){
                x1=lower_bound(c+1,c+t+1,b[i].xl)-c;
                x2=lower_bound(c+1,c+t+1,b[i].xr)-c-1;
                change(1,1,t,x1,x2,b[i].id);
                ans+=(b[i+1].h-b[i].h)*sum[1];
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Orleans的深入
    Orleans的入门教程
    .net core 微服务通讯组件Orleans的使用与配置
    AddTransient、AddSingleton、AddScoped的区别
    近期做架构师的总结
    大数据分析的深度与假象
    SQL反模式-1
    学习总结---INNODB 事务并发
    Tomcat的error-page掩盖action实例化的exception
    A SQL to insert continuous values
  • 原文地址:https://www.cnblogs.com/shenben/p/6278982.html
Copyright © 2011-2022 走看看