Atlantis HDU - 1542(扫描线+线段树基础题)

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 

思路:扫描线,具体原理请看前面的扫描线博客,这里主要分析与普通线段树的不同之处和两种写法。

在扫描线里,我们需要的是连续的区间,而不是离散的点。也就是说普通线段树(l,mid),(mid+1,r)这样的写法是无法完全包括我们的区间,因为它是离散型的。所以我们需要转换为(l,mid),(mid,r)。

普遍的,我们的线段树以及数据区间分布是这样的:

[1, a][a + 1, b][b + 1, c][c + 1, d][d + 1, e].......

但是如果只是简简单单的用这个来解决扫描线的问题会导致错误,为什么因为,他没有涉及到[a,a + 1],在扫描线中会出现[a,a + 1]中的数据,而常用的线段树的区间概念是无法解决这样的问题的,出现了所谓的区间缺失,怎样解决,下面的代码给出了解决方案,这里简单的提一下,就是利用[ , ),这个区间性质,左闭右开,即可解决区间缺失问题。

为了实现这样的区间,有两种线段树的写法:

1. 先直接建树,但是递归的时候满足(l,mid)(mid,r).

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
const int maxn=1e5+7;
#define mod 998244353
#define Lson l,m,rt<<1
#define Rson m,r,rt<<1|1
typedef long long ll;
using namespace std;
int n;
struct Edeg
{
    double l,r,h;
    int flag;
    Edeg()=default;
    Edeg(double a,double b,double c,int d):l(a),r(b),h(c),flag(d){}
}edge[maxn*3];
bool cmp(Edeg a, Edeg b)
{
    return a.h<b.h;
}
double x[maxn*2];
struct Tree
{
    int l,r,lazy;
    double len;
}tree[maxn<<2];

void Build(int l,int r,int rt)
{
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].lazy=0,tree[rt].len=0;
    if(l==r-1) return;//维护左闭右开
    int mid=(l+r)>>1;
    Build(l,mid,rt<<1);
    Build(mid,r,rt<<1|1);
}
void push_up(int rt)
{
    if(tree[rt].lazy>0)
    {
        tree[rt].len=(x[tree[rt].r]-x[tree[rt].l]);
    }
    else if(tree[rt].l==tree[rt].r-1) tree[rt].len=0;//
    else tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;
}
void updata(int L,int R,int v,int rt)
{
    if(L==tree[rt].l&&R==tree[rt].r)
    {
        tree[rt].lazy+=v;
        push_up(rt);
        return;
    }
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(L>=mid) updata(L,R,v,rt<<1|1);
    else if(R<=mid) updata(L,R,v,rt<<1);
    else
    {
        updata(L,mid,v,rt<<1);
        updata(mid,R,v,rt<<1|1);
    }
    push_up(rt);
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int kca=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        int k=0,m=0;
        for(int i=1;i<=n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            x[++k]=x1,x[++k]=x2;
            edge[++m]=Edeg(x1,x2,y1,1);
            edge[++m]=Edeg(x1,x2,y2,-1);
        }
        sort(edge+1,edge+m+1,cmp);
        sort(x+1,x+k+1);
        int tot=unique(x+1,x+k+1)-&x[1];
        Build(1,tot,1);
        double ans=0.f;
        for(int i=1;i<2*n;i++)
        {
            int l=lower_bound(x+1,x+tot+1,edge[i].l)-&x[0];
            int r=lower_bound(x+1,x+tot+1,edge[i].r)-&x[0];
            updata(l,r,edge[i].flag,1);
            ans+=(edge[i+1].h-edge[i].h)*tree[1].len;
        }
        printf("Test case #%d\n",++kca);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}

 

第二种是边更新边建树。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
const int maxn=1e5+7;
#define mod 998244353
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
int n;
struct Edeg
{
    double l,r,h;
    int flag;
    Edeg()=default;
    Edeg(double a,double b,double c,int d):l(a),r(b),h(c),flag(d){}
}edge[maxn*3];
bool cmp(Edeg a, Edeg b)
{
    return a.h<b.h;
}
double x[maxn*2];
struct Tree
{
    int l,r,lazy;
    double len;
}tree[maxn<<2];

void push_up(int rt,int l,int r)
{
    if(tree[rt].lazy)
    {
        tree[rt].len=(x[r+1]-x[l]);//区间
    }
    else if(l==r) tree[rt].len=0;
    else tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;
}
void updata(int L,int R,int v,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        tree[rt].lazy+=v;
        push_up(rt,l,r);
        return;
    }
    int m=(l+r)>>1;
    if(L<=m) updata(L,R,v,Lson);
    if(R>m) updata(L,R,v,Rson); 
    push_up(rt,l,r);
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int kca=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        int k=0,m=0;
        for(int i=1;i<=n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            x[++k]=x1,x[++k]=x2;
            edge[++m]=Edeg(x1,x2,y1,1);
            edge[++m]=Edeg(x1,x2,y2,-1);
        }
        for(int i=1;i<=m;i++)
        {
            tree[i].len=tree[i].lazy=0;
        }
        sort(edge+1,edge+m+1,cmp);
        sort(x+1,x+k+1);
        int tot=unique(x+1,x+k+1)-&x[1];
        double ans=0.f;
        for(int i=1;i<2*n;i++)
        {
            //cout<<edge[i].l<<" "<<edge[i].r<<endl;
            int l=lower_bound(x+1,x+tot+1,edge[i].l)-&x[0];
            int r=lower_bound(x+1,x+tot+1,edge[i].r)-&x[0]-1;//维护左闭右开区间
            //cout<<l<<" "<<r<<endl;
            updata(l,r,edge[i].flag,0,tot-1,1);//要从0到tot-1,因为0<=r-1<=tot-1
            ans+=(edge[i+1].h-edge[i].h)*tree[1].len;
        }
        printf("Test case #%d\n",++kca);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值