247. 亚特兰蒂斯

题目链接

247. 亚特兰蒂斯

有几个古希腊书籍中包含了对传说中的亚特兰蒂斯岛的描述。

其中一些甚至包括岛屿部分地图。

但不幸的是,这些地图描述了亚特兰蒂斯的不同区域。

您的朋友 Bill 必须知道地图的总面积。

你自告奋勇写了一个计算这个总面积的程序。

输入格式

输入包含多组测试用例。

对于每组测试用例,第一行包含整数 \(n\),表示总的地图数量。

接下来 \(n\) 行,描绘了每张地图,每行包含四个数字 \(x_1,y_1,x_2,y_2\)(不一定是整数),\((x_1,y_1)\)\((x_2,y_2)\) 分别是地图的左上角位置和右下角位置。

注意,坐标轴 \(x\) 轴从上向下延伸,\(y\) 轴从左向右延伸。

当输入用例 \(n=0\) 时,表示输入终止,该用例无需处理。

输出格式

每组测试用例输出两行。

第一行输出 Test case #k,其中 \(k\) 是测试用例的编号,从 \(1\) 开始。

第二行输出 Total explored area: a,其中 \(a\) 是总地图面积(即此测试用例中所有矩形的面积并,注意如果一片区域被多个地图包含,则在计算总面积时只计算一次),精确到小数点后两位数。

在每个测试用例后输出一个空行。

数据范围

\(1≤n≤10000,\)
\(0≤x_1<x_2≤100000,\)
\(0≤y_1<y_2≤100000\)
注意,本题 \(n\) 的范围上限加强至 \(10000\)

输入样例:

2
10 10 20 20
15 15 25 25.5
0

输出样例:

Test case #1
Total explored area: 180.00 

样例解释

样例所示地图覆盖区域如下图所示,两个矩形区域所覆盖的总面积,即为样例的解。

image

解题思路

线段树,扫描线,离散化

跟普通扫描线不同的是,这里正方形的个数更多,得用线段树来优化区间合并:扫描线设定为一条竖线,设定正方形左边权值为 \(1\),右边为 \(-1\),线段树维护纵坐标上的权值:对于其上的点,当且仅当有覆盖时其权值仅为 \(1\),另外线段树维护的是点的信息,这里点代表单位为 \(1\)的线段的左端点,所以在更新时区间右端点应该减一,另外这里的坐标为实数,应该离散化
另外对于不用打懒标记的情况,询问的时候是直接询问的根节点信息,用不到懒标记,另外对于更新节点时:

  • 对于加操作,肯定是不影响答案的
  • 对于减操作,
    • 当减完后覆盖次数依然不为 \(0\) 的话答案不变
    • \(0\) 时,可利用其左右儿子的信息获得答案,因为左右儿子没有修改是正确的,能够更新获得父亲节点的正确信息

另外,对于一些题目如果不打懒标记的话可能还不好做,像区间求和,所以,打懒标记还得看实际情况

  • 时间复杂度:\(O(nlogn)\)

代码

// Problem: 亚特兰蒂斯
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/249/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1e4+5;
int n;
vector<double> ys;
struct A
{
	double x,y1,y2;
	int k;
	bool operator<(A &o)const
	{
		return x<o.x;
	}
}a[N<<1];
struct segTr
{
	int l,r,cnt;
	double len;
}tr[8*N];
void build(int u,int l,int r)
{
	tr[u]={l,r,0,0};
	if(l!=r)
	{
		int mid=l+r>>1;
		build(u<<1,l,mid),build(u<<1|1,mid+1,r);
	}
}
void pushup(int u)
{
	if(tr[u].cnt)tr[u].len=ys[tr[u].r+1]-ys[tr[u].l];
	else if(tr[u].l!=tr[u].r)tr[u].len=tr[u<<1].len+tr[u<<1|1].len;
	else
		tr[u].len=0;
}
void change(int u,int l,int r,int k)
{
	if(l<=tr[u].l&&tr[u].r<=r)
	{
		tr[u].cnt+=k;
		pushup(u);
		return ;
	}
	int mid=tr[u].l+tr[u].r>>1;
	if(l<=mid)change(u<<1,l,r,k);
	if(r>mid)change(u<<1|1,l,r,k);
	pushup(u);
}
int find(double x)
{
	return lower_bound(ys.begin(),ys.end(),x)-ys.begin();
}
int main()
{
	int T=1;
	while(scanf("%d",&n),n)
	{
		ys.clear();
		double x1,x2,y1,y2;
		for(int i=1,j=0;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			a[j++]={x1,y1,y2,1};
			a[j++]={x2,y1,y2,-1};
			ys.pb(y1);
			ys.pb(y2);
		}
		double res=0;
		sort(ys.begin(),ys.end());
		ys.erase(unique(ys.begin(),ys.end()),ys.end());
		sort(a,a+2*n);
		build(1,0,ys.size()-2);
		for(int i=0;i<2*n;i++)
		{
			if(i)res+=tr[1].len*(a[i].x-a[i-1].x);
			change(1,find(a[i].y1),find(a[i].y2)-1,a[i].k);
		}
		printf("Test case #%d\n",T++);
		printf("Total explored area: %.2lf\n\n",res);
	}
}
posted @ 2022-02-24 17:56  zyy2001  阅读(59)  评论(0编辑  收藏  举报