POJ1151 Atlantis(扫描线、线段树)

Atlantis

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 x 1 ; y 1 ; x 2 ; y 2 ( 0 < = x 1 < x 2 < = 100000 ; 0 < = y 1 < y 2 < = 100000 ) x_1;y_1;x_2;y_2 (0 <= x_1 < x_2 <= 100000;0 <= y_1 < y_2 <= 100000) x1;y1;x2;y2(0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values ( x 1 ; y 1 ) (x_1; y_1) (x1;y1) and ( x 2 ; y 2 ) (x_2;y_2) (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 

方法一

y y y坐标离散化,再用扫描线。数据范围100,直接暴力维护区间。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=210;
struct L{double x,yh,yl;int v;}line[maxn];
int tree[maxn],tot,n,ca;
double c[maxn];

bool cmp(const L& a,const L& b){    return a.x<b.x; }

double ask(){
    double ret=0;
    for(int i=0;i<tot-1;i++)
        if(tree[i]>0)  ret+=c[i+1]-c[i];
    return ret;
}

void change(int l,int r,int v){
    for(int i=l;i<r;i++)   tree[i]+=v;
}

void solve(){
    double x1,y1,x2,y2,prevx=-1e8,ans=0;
    memset(tree,0,sizeof(tree));
    for(int i=0;i<n;i++){
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        line[i*2].x=x1,line[i*2].yh=y2,line[i*2].yl=y1,line[i*2].v=1;
        line[i*2+1].x=x2,line[i*2+1].yh=y2,line[i*2+1].yl=y1,line[i*2+1].v=-1;
        c[i*2]=y1,c[i*2+1]=y2;
    }
    sort(c,c+n*2);
    tot=unique(c,c+n*2)-c;
    sort(line,line+2*n,cmp);
    for(int i=0;i<2*n;i++){
        if(line[i].x!=prevx)    ans+=(line[i].x-prevx)*ask();
        int l=lower_bound(c,c+tot,line[i].yl)-c;
        int r=lower_bound(c,c+tot,line[i].yh)-c;
        change(l,r,line[i].v),prevx=line[i].x;
    }
    printf("Test case #%d\nTotal explored area: %.2f\n\n",++ca,ans);
}

int main(){
    while(scanf("%d",&n),n) solve();
}

方法二

线段树维护区间。由于矩形的两条边都是成对出现的,所以不需要下传延迟标记,对某段区间增加了1之后,总会再对该区间进行-1的操作。并且区间被覆盖的次数永远不可能是负数。

有了上面这些性质,线段树可以简化一些。

下面附上代码,必须要说明的是,这个代码在POJ上没有ac

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=210;
struct L{double x,yh,yl;int v;}line[maxn];
struct T{
    int l,r,cnt;double len;
    #define l(x) tree[x].l
    #define r(x) tree[x].r
    #define cnt(x) tree[x].cnt
    #define len(x) tree[x].len
}tree[maxn*4];
int tot,n,ca;
double c[maxn];

bool cmp(const L& a,const L& b){    return a.x<b.x; }

void build(int p,int l,int r){
    l(p)=l,r(p)=r,cnt(p)=len(p)=0;
    if(l==r)    return;
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
}

void update(int p){
    if(cnt(p))  len(p)=c[r(p)+1]-c[l(p)];
    else    len(p)=len(p<<1)+len(p<<1|1);
}

void change(int p,int l,int r,int v){
    if(l<=l(p)&&r>=r(p)){
        cnt(p)+=v;
        update(p);
        return;
    }
    int mid=(l(p)+r(p))/2;
    if(l<=mid)  change(p*2,l,r,v);
    if(r>=mid+1)    change(p<<1|1,l,r,v);
    update(p);
}

void solve(){
    double x1,y1,x2,y2,prevx=-1e8,ans=0;
    for(int i=0;i<n;i++){
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        line[i*2].x=x1,line[i*2].yh=y2,line[i*2].yl=y1,line[i*2].v=1;
        line[i*2+1].x=x2,line[i*2+1].yh=y2,line[i*2+1].yl=y1,line[i*2+1].v=-1;
        c[i*2]=y1,c[i*2+1]=y2;
    }
    sort(c,c+n*2);
    tot=unique(c,c+n*2)-c;
    build(1,0,tot-1);
    sort(line,line+2*n,cmp);
    for(int i=0;i<2*n;i++){
        if(line[i].x!=prevx)    ans+=(line[i].x-prevx)*len(1);
        int l=lower_bound(c,c+tot,line[i].yl)-c;
        int r=lower_bound(c,c+tot,line[i].yh)-c;
        change(1,l,r-1,line[i].v),prevx=line[i].x;
    }
    printf("Test case #%d\nTotal explored area: %.2f\n\n",++ca,ans);
}

int main(){
    while(scanf("%d",&n),n) solve();
}

至于为什么没有通过,这个我也不知该怎么解释。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

m0_51864047

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

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

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

打赏作者

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

抵扣说明:

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

余额充值