POJ 1151: 扫描线

该博客介绍了POJ 1151问题的解决方案,涉及到扫描线算法在计算矩形面积并中的应用。文章首先说明了输入输出格式,然后讲解了扫描线的概念,通过将矩形区域划分成多个小矩形简化问题。作者提到了一种基于排序和区间修改的暴力解法,并进一步使用线段树进行了优化,降低了时间复杂度至O(nlogn)。

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


——扫描线
原题传送门

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.

Data

Input
The input consists of several test cases.
Each test case starts with a line containing a single integer n of available maps.
The n following lines describe one map each.
Each of these lines contains four numbers x1;y1;x2;y2, 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 
	//This is a blank line

1 <= n <= 100 0 <= x1 < x2 <= 100000 0 <= y1 < y2 <= 100000

前言

POJ是一个神奇的OJ.
按道理说, d o u b l e double double类型的输出应该是 % l f \%lf %lf.
但是这样提交会WA!
后来参考奆老博客发现这个输出竟然改成 % f \%f %f就可以了!
在此声明.

思路

一道计算矩形面积并的裸题.
何为面积并?
举个例子,这样分布的矩形:
在这里插入图片描述
面积并就是这个部分:
在这里插入图片描述
如果没有扫描线,这个图形的面积还是很难计算的.
那么,什么是扫描线?

扫描线

扫描线,其实就是扫描的线,就像条形码.
形象一点,这个图形被扫描线分割成这些部分:
在这里插入图片描述
这个图形被切割的每一个部分都是一个矩形.
这样子,这个图形的总面积就可以拆分成每个图形的面积.
那具体如何求每一个矩形的面积?
由于读入的矩形比较容易操作,所以我们回到原题解释.

矩形面积并

这道题如果不是矩形面积并,我们如何解决?
当然是直接加了
我们可以考虑这样的思路:

  • 遇到矩形左边界,我们加上它
  • 遇到矩形右边界,我们计算边界长乘距离左边界的长度

其实在本题思路就是如此,只不过需要修改:

  1. 对于每一个矩形 ( x 1 , y 1 , x 2 , y 2 ) (x_1,y_1,x_2,y_2) (x1,y1,x2,y2)(与题意相符),我们设立两个四元组: ( x 1 , y 1 , y 2 , 1 ) (x_1,y_1,y_2,1) (x1,y1,y2,1) ( x 2 , y 1 , y 2 , − 1 ) (x_2,y_1,y_2,-1) (x2,y1,y2,1).

  2. 我们将这些四元组按照 x x x递增排序.

  3. 设立一个层数数组 c [ ] c[ ] c[],表示这个区间被覆盖的次数.

  4. 遍历每一个四元组,对于每一个四元组:
    如果第四个关键字为 1 1 1,就将 c [ y 1 , y 2 ] c[y_1,y_2] c[y1,y2]中的所有元素 + 1 +1 +1.
    如果第四个关键字为 − 1 -1 1,就将 c [ y 1 , y 2 ] c[y_1,y_2] c[y1,y2]中的所有元素 − 1 -1 1.

  5. 处理后再次遍历 c [ ] c[] c[],对于所有 c [ i ] &gt; 0 c[i]&gt;0 c[i]>0,我们将 c [ i ] c[i] c[i]所代表的区间长度统计到答案.

这就是用扫描线处理本题的大致思路.

注意点

本题除了需要完成这些点,还有几点需要注意.

  1. 本题的矩形上下边界是一个"点",所以对于每一个区间 c [ i ] c[i] c[i],可以用它表示点 i i i和点 i + 1 i+1 i+1的中间区间,这样对区间 [ l , r ] [l,r] [l,r]修改时就应改为对 c [ l , r − 1 ] c[l,r-1] c[l,r1]进行修改.
  2. 本题的点不一定是整数点,这样我们就需要将这些点的 y y y值映射到一个 h [ ] h[] h[]数组去,这时候需要进行离散化.

另外,本题由于 d o u b l e double double可能会存在运算后的精度差,导致WA,所以建议以 y y y值排序,从下往上扫heiheihei.
只要是掌握了扫描线的精髓,这样的变化应该不难实现.

BF

这样的直接区间修改算法时间复杂度是 O ( n 2 ) O(n^2) O(n2).
但是对于 n ≤ 100 n\le100 n100,仍然绰绰有余.
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=105;
struct Line
{
	double l,r,y;
	int add;
}a[2*N];
int n,c[2*N];
double h[2*N];
bool cmp(const Line &a,const Line &b)
{
	return a.y<b.y;
}
int main()
{
	double x1,y1,x2,y2;
	int tcase=0;
	while (scanf("%d",&n))
	{
		if (n==0) break;
		memset(h,0,sizeof(h));
		memset(a,0,sizeof(a));
		memset(c,0,sizeof(c));
		for (int i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			int p=2*i-1,q=2*i;
			a[p].y=y1,a[p].l=x1,a[p].r=x2,a[p].add=1;
			a[q].y=y2,a[q].l=x1,a[q].r=x2,a[q].add=-1;
			h[p]=x1,h[q]=x2;
		}
		sort(a+1,a+2*n+1,cmp);sort(h+1,h+2*n+1);
		int m=unique(h+1,h+2*n+1)-(h+1);
		double ans=0;
		for (int i=1;i<2*n;i++)
		{
			int p=lower_bound(h+1,h+m+1,a[i].l)-h,q=lower_bound(h+1,h+m+1,a[i].r)-h;
			double k=a[i+1].y-a[i].y,len=0.;
			for (int j=p;j<q;j++) c[j]+=a[i].add;
			for (int j=1;j<m;j++)
				if (c[j]>0)
					len+=h[j+1]-h[j];
			ans+=k*len;
		}
		printf("Test case #%d\nTotal explored area: %.2f\n\n",++tcase,ans);
	}
	return 0;
} 

线段树优化

本题对区间修改的 O ( n ) O(n) O(n)复杂度,仍存在可优化之处.
线段树就是一种可支持区间修改的数据结构.
并且更好的是,由于本题是矩形面积并.
被加上的四元组 ( x , y 1 , y 2 , 1 ) (x,y_1,y_2,1) (x,y1,y2,1)迟早会被抵消.
所以不必运用懒标记,改为做一些特殊的处理.
我们用两个标记 l e n , c n t len,cnt len,cnt来记录区间 [ l , r ] [l,r] [l,r]

  • c n t cnt cnt表示这个区间被覆盖的次数.
  • l e n len len表示这个区间被覆盖的长度.

那么可以想到如下的"更新节点"的方法:

  • 如果 c n t cnt cnt>0(对应 c [ i ] &gt; 0 c[i]&gt;0 c[i]>0),那么 l e n len len就等于这个区间的长度(即 h [ r + 1 ] − h [ l ] h[r+1]-h[l] h[r+1]h[l])
  • 如果 c n t = 0 cnt=0 cnt=0,那么 l e n len len就等于左右孩子的 l e n len len之和.
  • 不存在 c n t &lt; 0 cnt&lt;0 cnt<0 的情况.

求总覆盖的长度返回根节点的 l e n len len即可.
代码如下:

void up(int root)
{
    if (tree[root].cnt>0) tree[root].len=h[tree[root].r+1]-h[tree[root].l];
    else tree[root].len=tree[lc].len+tree[rc].len;
}

这样经过线段树的优化,总体复杂度就可以达到 O ( n l o g n ) O(nlogn) O(nlogn).

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define lc root<<1
#define rc root<<1|1
using namespace std;
const int N=205;
struct Line
{
	double l,r,y;
	int add;
}a[2*N];
struct node
{
    int l,r;
    int cnt;
    double len;
}tree[5*N];
int n,c[N];
double h[N];
bool cmp(const Line &a,const Line &b)
{
	return a.y<b.y;
}
void up(int root)
{
    if (tree[root].cnt>0) tree[root].len=h[tree[root].r+1]-h[tree[root].l];
    else tree[root].len=tree[lc].len+tree[rc].len;
}
void build(int root,int L,int R)
{
    tree[root].l=L,tree[root].r=R;
    if (L==R)
    {
        tree[root].cnt=tree[root].len=0;
        return;
    }
    int mid=(L+R)>>1;
    build (lc,L,mid);
    build (rc,mid+1,R);
    up(root);
}
void update(int root,int l,int r,int x)
{
    int L=tree[root].l,R=tree[root].r;
    if (l==L&&r==R)
    {
        tree[root].cnt+=x;
        up(root);
        return;
    }
    int mid=(L+R)>>1;
    if (r<=mid) update(lc,l,r,x);
    else if (l>mid) update(rc,l,r,x);
    else 
    {
        update(lc,l,mid,x);
        update(rc,mid+1,r,x);
    }
    up(root);
}
int main()
{
	double x1,y1,x2,y2;
	int tcase=0;
	while (scanf("%d",&n))
	{
		if (n==0) break;
		memset(h,0,sizeof(h));
		memset(a,0,sizeof(a));
		memset(c,0,sizeof(c));
        memset(tree,0,sizeof(tree));
		for (int i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			int p=2*i-1,q=2*i;
			a[p].y=y1,a[p].l=x1,a[p].r=x2,a[p].add=1;
			a[q].y=y2,a[q].l=x1,a[q].r=x2,a[q].add=-1;
			h[p]=x1,h[q]=x2;
		}
		sort(a+1,a+2*n+1,cmp);sort(h+1,h+2*n+1);
		int m=unique(h+1,h+2*n+1)-(h+1);
		double ans=0;
        build (1,1,2*n);
		for (int i=1;i<2*n;i++)
		{
			int p=lower_bound(h+1,h+m+1,a[i].l)-h,q=lower_bound(h+1,h+m+1,a[i].r)-h;
			double k=a[i+1].y-a[i].y;
            update(1,p,q-1,a[i].add);
			ans+=k*tree[1].len;
		}
		printf("Test case #%d\nTotal explored area: %.2f\n\n",++tcase,ans);
	}
	return 0;
} 

感谢奆老关注 qwq ?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值