Atlantis(POJ1151 )(扫描线+线段树+离散化)

本文介绍了一种计算多个地图重叠区域总面积的方法,通过解析古希腊文献中关于亚特兰蒂斯不同区域的地图,利用算法计算所有地图覆盖的总区域面积。文章提供了两种算法实现,一种是O(n^2)的简单实现,另一种是更高效的O(nlogn)线段树算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 29779 Accepted: 10606
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

在这里插入图片描述
在这里插入图片描述
o(n*n)做法:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define N 150
using namespace std;
struct node {
    double v;
    int pos,e;
}a[N<<1],b[N<<1];
int f[N<<1],c[N<<1][2];
bool cmp(const node &a,const node &b){
    return a.v<b.v;
}
int main(){
    int n;
    int ca=0;
    double x1,x2,y1,y2;
    while(cin>>n,n){
    
    for(int i=1;i<=n;i++){
        cin>>x1>>y1>>x2>>y2;
        a[i*2-1].v=x1,a[i*2-1].pos=i,a[i*2-1].e=1;
        a[i*2].v=x2,a[i*2].pos=i,a[i*2].e=-1;
        b[i*2-1].v=y1,b[i*2-1].pos=i,b[i*2-1].e=0;
        b[i*2].v=y2,b[i*2].pos=i,b[i*2].e=1;
    }
    sort(a+1,a+2*n+1,cmp);
    sort(b+1,b+2*n+1,cmp);
    for(int i=1;i<=2*n;i++){
        c[b[i].pos][b[i].e]=i;
    }
    memset(f,0,sizeof(f));
    double ans=0;
    for(int i=1;i<2*n;i++){
        double len=0;
        int j=a[i].pos;
        int x=c[j][0],y=c[j][1],z=a[i].e;
        for(int k=x;k<y;k++){
            f[k]+=z;//标记某段是否被覆盖
           
        }
        for(int k=1;k<2*n;k++)if(f[k])len+=b[k+1].v-b[k].v; //计算扫描线被覆盖的长度。
        ans+=len*(a[i+1].v-a[i].v); 
        
    }
    printf("Test case #%d\nTotal explored area: %.2f \n",++ca,ans);
    printf("\n");   
    }   
}

线段树O(nlogn)

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define N 150
using namespace std;
struct node {
   
    double x,y1,y2;
    int  k;
}a[N<<1];
int tot;
double  raw[N<<1];
int val(double x){
    return lower_bound(raw+1,raw+tot+1,x)-raw;
}
struct SegmentTree{
     int l,r;
    double len;
    int cnt;
}t[N<<3];
void build(int p,int l,int r){
    t[p].l=l,t[p].r=r;
    if(l==r){
        t[p].len=0;
        t[p].cnt=0;
        return ;
    }
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    t[p].len=t[p*2].len+t[p*2+1].len;
}
void change(int p,int l,int r ,int k){
    if(l<=t[p].l&&r>=t[p].r&&t[p].l==t[p].r){
        t[p].cnt+=k;
            
        
        if(t[p].cnt>0){
            t[p].len=raw[t[p].r+1]-raw[t[p].l];
         
        }
        else t[p].len=0;
        return ;
    }
    int mid=(t[p].l+t[p].r)/2;
    if(l<=mid) change(p*2,l,r,k);
    if(r>mid) change(p*2+1,l,r,k);
    t[p].len=t[p*2].len+t[p*2+1].len;
    t[p].cnt=t[p*2].cnt+t[p*2+1].cnt;
}
bool cmp(const node &a,const node&b){
    return a.x<b.x;
}
int main(){
    int n;
    int ca=0;
    while(cin>>n,n){
        double ans=0;
        double x1,x2,y1,y2;
        for(int i=1;i<=n;i++){
        cin>>x1>>y1>>x2>>y2;
            a[i*2-1]={x1,y1,y2,1};
            a[i*2]={x2,y1,y2,-1};
            raw[i*2-1]=y1;
            raw[i*2]=y2;
        }
    
        sort(a+1,a+2*n+1,cmp);
        sort(raw+1,raw+2*n+1);
        
        tot=unique(raw+1,raw+2*n+1)-raw-1;
         build(1,1,tot-1);
         
        for(int i=1;i<2*n;i++){
          
            int l=val(a[i].y1),r=val(a[i].y2)-1;
            change(1,l,r,a[i].k);
            ans+=(a[i+1].x-a[i].x)*t[1].len;
           
        }
        printf("Test case #%d\nTotal explored area: %.2f \n\n",++ca,ans);
        
    }
    
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨航宇Harry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值