HDU 1542+HDU 1255(进阶) 线段树扫描线法解决矩形覆盖求面积

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. 

InputThe 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.OutputFor 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 
题意:题意:给你n个矩形的左下角坐标和右上角坐标,求矩形相交的面积(相叠的区域面积只能求一次)。

思路是参考以下几位大牛的

https://blog.csdn.net/u013480600/article/details/22548393

https://blog.csdn.net/tomorrowtodie/article/details/52048323

这一题有以下几个点要注意:

(1)区间是浮点类型,因此需要离散化坐标才能建树

(2)明确区间[l,r]代表的意思是线段[l,r+1]的长度

(3)pushup函数的调用位置以及里面语句的相对位置

(4)叶子结点的处理方法

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
#define lson id << 1
#define rson id << 1 | 1
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int N = 10007;
const int maxn = 100000 + 5;
struct node{//线段树的结构体
    int l,r,cnt;
    double len;
}a[N<<2];
struct node1{
    double l,r,h;
    int f;
    bool operator < (const node1 &a) const{
    return h<a.h;
    }
}num[N];
double point[N];
void build(int id,int l,int r){
    a[id].l=l;a[id].r=r;
    a[id].cnt=0;a[id].len=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
}
void pushup(int id){//这题的叶子结点[a,a]代表的是区间[a,a+1]的值,同理[L,R]表示的是[L,R+1]的值
    if(a[id].cnt>0) a[id].len=point[a[id].r+1]-point[a[id].l];//因为由上面那一行可得,区间长度就是右端点+1-左端点
    else if(a[id].l==a[id].r) a[id].len=0;//千万别漏掉这一行,否则如果是扫描结束那么就直接进入下面语句数组开小的话会越界
                                          //如果是下一次调用的话需要清除以前的保留值
    else a[id].len=a[lson].len+a[rson].len;//当为0的时候就说明这一部分已经被扫描结束了,那么就需要将其左右孩子加起来
}
void update(int id,int l,int r,int val){
    if(a[id].l==l&&a[id].r==r){
        a[id].cnt+=val;
        pushup(id);
        return ;
    }
    int mid=(a[id].l+a[id].r)>>1;
    if(r<=mid) update(lson,l,r,val);
    else if(l>mid) update(rson,l,r,val);
    else{
        update(lson,l,mid,val);
        update(rson,mid+1,r,val);
    }
    pushup(id);
}
int main()
{
     #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T=1;
    int n;
    while(scanf("%d",&n)!=EOF,n){
        int sum=0;
        rep(i,1,n){double x1,y1,x2,y2;
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        point[sum]=x1;point[sum+1]=x2;
        num[sum].l=num[sum+1].l=x1;
        num[sum].r=num[sum+1].r=x2;
        num[sum].h=y1,num[sum+1].h=y2;
        num[sum].f=1;num[sum+1].f=-1;
        sum+=2;
        }
        sort(num,num+sum);
        sort(point,point+sum);
        int k=1;
        for(int i=1;i<sum;i++)//去重
        if(point[i]!=point[i-1])
            point[k++]=point[i];
        build(1,0,k-1);//离散化建树,树的区间是[0,k-1];
        double ans=0;
        rep(i,0,sum-2){
            int L=lower_bound(point,point+k,num[i].l)-point;
            int R=lower_bound(point,point+k,num[i].r)-point-1;
            update(1,L,R,num[i].f);
            ans+=(num[i+1].h-num[i].h)*a[1].len;
        }
        printf("Test case #%d\n",T++);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}

还有一道进阶版的

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 

题意:给定n个矩形,求重叠两次以上的面积。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
#define lson id << 1
#define rson id << 1 | 1
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int N = 10007;
const int maxn = 100000 + 5;
struct node{
    int l,r,cnt;
    double len1,len2;//这里要注意是double类型的
}a[N<<2];
double x[N];
struct node1{
    double l,r,h;
    int f;
    bool operator < (const node1 &a) const{
        return h<a.h;
    }
}num[N];
void build(int id,int l,int r){
    a[id].l=l;a[id].r=r;
    a[id].cnt=a[id].len1=a[id].len2=0;
    if(l==r)  return ;
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
}
void pushup(int id){
    if(a[id].cnt)  a[id].len1=x[a[id].r+1]-x[a[id].l];
    else if(a[id].l==a[id].r) a[id].len1=0;
    else a[id].len1=a[lson].len1+a[rson].len1;

    if(a[id].cnt>1) a[id].len2=x[a[id].r+1]-x[a[id].l];
    //cnt>1 : 说明该区间被覆盖两次或以上,那么长度就可以直接计算,就是该区间的长度
    else if(a[id].l==a[id].r) a[id].len2=0;
    else if(a[id].cnt==1) a[id].len2=a[lson].len1+a[rson].len1;
//    cnt=1确切的意义是什么,应该是,可以确定,这个区间被完全覆盖了1次,而有没有被完全覆盖两次或以上则不知道无法确定,
//    那么怎么怎么办了,只要加上t[lch].s + t[rch].s
//    即,看看左右孩子区间被覆盖了一次或以上的长度,那么叠加在双亲上就是双亲被覆盖两次或以上的长度
    else a[id].len2=a[lson].len2+a[rson].len2;
}
void update(int id,int l,int r,int val){
    if(a[id].l==l&&a[id].r==r){
        a[id].cnt+=val;
        pushup(id);
        return ;
    }
    int mid=(a[id].l+a[id].r)>>1;
    if(r<=mid) update(lson,l,r,val);
    else if(l>mid) update(rson,l,r,val);
    else {
        update(lson,l,mid,val);
        update(rson,mid+1,r,val);
    }
    pushup(id);
}
int main()
{
     #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int t;
    scanf("%d",&t);
    while(t--){int n,tot=0;
        scanf("%d",&n);
        rep(i,1,n){ double x1,x2,y1,y2;
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        x[tot]=x1,x[tot+1]=x2;
        num[tot].l=num[tot+1].l=x1;
        num[tot].r=num[tot+1].r=x2;
        num[tot].h=y1;num[tot+1].h=y2;
        num[tot].f=1; num[tot+1].f=-1;
        tot+=2;
        }
        sort(x,x+tot);
        sort(num,num+tot);
        int k=1;
        rep(i,1,tot-1)
        if(x[i]!=x[i-1]) x[k++]=x[i];
        build(1,0,k-1);

        double ans=0;
        rep(i,0,tot-2){
        int L=lower_bound(x,x+k,num[i].l)-x;
        int R=lower_bound(x,x+k,num[i].r)-x-1;
        update(1,L,R,num[i].f);
        ans+=(num[i+1].h-num[i].h)*a[1].len2;
        }
        printf("%.2f\n",ans);
    }
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值