zoukankan      html  css  js  c++  java
  • (中等) HDU 1542 Atlantis,扫描线。

      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.

      题目就是求矩形并的面积,具体请看  线段树 (扫描线)  这篇文章。

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    
    #define lson L,M,lc
    #define rson M+1,R,rc
    #define lc po*2
    #define rc po*2+1
    
    using namespace std;
    
    struct BIAN
    {
        double x,y1,y2;
        int state;
    };
    
    BIAN bian[210];
    double hash[210];
    double BIT[210*4];
    int COL[210*4];
    int COU;
    
    bool cmp(BIAN a,BIAN b)
    {
        return a.x<b.x;
    }
    
    int find(double x)
    {
        int L=1,R=COU;
        int M;
    
        while(R>L)
        {
            M=(L+R)/2;
    
            if(fabs(hash[M]-x)<0.000001)
                return M;
    
            if(hash[M]<x)
                L=M+1;
            else
                R=M-1;
        }
    
        return L;
    }
    
    void pushUP(int L,int R,int po)
    {
        if(COL[po])
            BIT[po]=hash[R+1]-hash[L];
        else if(L==R)
            BIT[po]=0;
        else
            BIT[po]=BIT[lc]+BIT[rc];
    }
    
    void update(int ul,int ur,int ut,int L,int R,int po)
    {
        if(ul<=L&&ur>=R)
        {
            COL[po]+=ut;
    
            if(COL[po]>0)
                BIT[po]=hash[R+1]-hash[L];
            else if(L==R)
                BIT[po]=0;
            else
                pushUP(L,R,po);
    
            return;
        }
    
        int M=(L+R)/2;
    
        if(ul<=M)
            update(ul,ur,ut,lson);
        if(ur>M)
            update(ul,ur,ut,rson);
    
        pushUP(L,R,po);
    }
    
    int main()
    {
        int cas=0;
        int N;
        double x1,x2,y1,y2;
        double ans;
    
        for(cin>>N;N;cin>>N)
        {
            ans=0;
            COU=1;
            memset(hash,0,sizeof(hash));
            memset(COL,0,sizeof(COL));
            memset(BIT,0,sizeof(BIT));
    
            for(int i=1;i<=N;++i)
            {
                scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
    
                bian[i*2-1].x=x1;
                bian[i*2-1].y1=y1;
                bian[i*2-1].y2=y2;
                bian[i*2-1].state=1;
    
                bian[i*2].x=x2;
                bian[i*2].y1=y1;
                bian[i*2].y2=y2;
                bian[i*2].state=-1;
    
                hash[COU++]=y1;
                hash[COU++]=y2;
            }
    
            sort(hash+1,hash+COU);
            sort(bian+1,bian+2*N+1,cmp);
    
            int k=2;
            for(int i=2;i<COU;++i)
                if(hash[i]!=hash[i-1])
                    hash[k++]=hash[i];
            COU=k-1;
    
            for(int i=1;i<=2*N;++i)
            {
                ans+=BIT[1]*(bian[i].x-bian[i-1].x);
    
                update(find(bian[i].y1),find(bian[i].y2)-1,bian[i].state,1,COU-1,1);
            }
    
            ans+=BIT[1];
            printf("Test case #%d
    Total explored area: %.2f
    
    ",++cas,ans);
        }
    
        return 0;
    }
    View Code

    代码改了一下的:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    
    #define lc po*2
    #define rc po*2+1
    #define lson L,M,lc
    #define rson M+1,R,rc
    #define ji i*2-1
    #define ou i*2
    
    using namespace std;
    
    struct BIAN
    {
        double x,y1,y2;
        short state;
    };
    
    const int maxn=205;
    
    BIAN bian[maxn];
    double BIT[maxn*4];
    int COL[maxn*4];
    double hash[maxn];
    int COU;
    
    int find(double x)
    {
        int L=1,R=COU,M;
    
        while(R>L)
        {
            M=(L+R)/2;
    
            if(fabs(x-hash[M])<0.0000001)
                return M;
    
            if(hash[M]<x)
                L=M+1;
            else
                R=M-1;
        }
        
        return L;
    }
    
    void callUP(int L,int R,int po)
    {
        if(COL[po])
            BIT[po]=hash[R+1]-hash[L];
        else if(L==R)
            BIT[po]=0;
        else
            BIT[po]=BIT[lc]+BIT[rc];
    }
    
    void pushUP(int L,int R,int po)
    {
        int temp=min(COL[lc],COL[rc]);
        
        COL[po]+=temp;
        COL[lc]-=temp;
        COL[rc]-=temp;
    
        callUP(L,(L+R)/2,lc);
        callUP((L+R)/2+1,R,rc);
    
        callUP(L,R,po);
    }
    
    void pushDown(int L,int R,int po)
    {
        if(COL[po])
        {
            COL[lc]+=COL[po];
            COL[rc]+=COL[po];
    
            callUP(L,(L+R)/2,lc);
            callUP((L+R)/2+1,R,rc);
    
            callUP(L,R,po);
    
            COL[po]=0;
        }
    }
    
    void update(int ul,int ur,int ut,int L,int R,int po)
    {
        if(ul<=L&&ur>=R)
        {
            COL[po]+=ut;
            pushUP(L,R,po);
    
            return;
        }
    
        pushDown(L,R,po);
    
        int M=(L+R)/2;
    
        if(ul<=M)
            update(ul,ur,ut,lson);
        if(ur>M)
            update(ul,ur,ut,rson);
    
        pushUP(L,R,po);
    }
    
    bool cmp(BIAN a,BIAN b)
    {
        return a.x<b.x;
    }
    
    int main()
    {
        int N;
        int cas=1;
        double ans;
        double x1,x2,y1,y2;
    
        ios::sync_with_stdio(false);
        cout.setf(ios::fixed);
        cout.precision(2);
    
        for(cin>>N;N;cin>>N)
        {
            memset(BIT,0,sizeof(BIT));
            memset(COL,0,sizeof(COL));
    
            for(int i=1;i<=N;++i)
            {
                cin>>x1>>y1>>x2>>y2;
    
                hash[ji]=y1;
                hash[ou]=y2;
    
                bian[ji].state=1;
                bian[ou].state=-1;
    
                bian[ji].x=x1;
                bian[ou].x=x2;
    
                bian[ji].y1=bian[ou].y1=y1;
                bian[ji].y2=bian[ou].y2=y2;
            }
    
            sort(bian+1,bian+2*N+1,cmp);
            sort(hash+1,hash+2*N+1);
    
            COU=2;
            for(int i=2;i<=2*N;++i)
                if(hash[i]!=hash[i-1])
                    hash[COU++]=hash[i];
            --COU;
    
            ans=0;
    
            for(int i=1;i<=2*N;++i)
            {
                ans+=BIT[1]*(bian[i].x-bian[i-1].x);
            
                update(find(bian[i].y1),find(bian[i].y2)-1,bian[i].state,1,COU,1);
            }
    
            cout<<"Test case #"<<cas++<<endl;
            cout<<"Total explored area: "<<ans<<endl<<endl;
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    scrapy-图片-文件爬取
    四、scrapy五大核心组件
    三、scrapy全站数据爬取
    二、scrapy的高性能持久化存储操作
    一、scrapy基本使用
    无头浏览器+规避检测
    必应美图-异步爬虫-asyncio协程
    站长之家简历爬取源码
    梨视频最热视频爬取源码
    sql语句查询最近七天 三十天 数据
  • 原文地址:https://www.cnblogs.com/whywhy/p/4214350.html
Copyright © 2011-2022 走看看