HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并。

NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂。 具体见思路见注释=。=

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

#define lson rt<<1,l,mid
#define rson rt<<1|1,mid + 1,r
using namespace std;
const int maxn = 4004;

struct Seg {
	double l, r, x;
	int cover;
	Seg(double l, double r, double x, int cover) :l(l), r(r), x(x), cover(cover) {}
	//排序大小按照x轴的坐标大小
	bool operator < (const Seg &tmp) const {
		return x < tmp.x;
	}
};

int n, cnt[maxn << 2];
double len[maxn << 2];
vector<double> dy;
vector<Seg> seg;

int GetID(double Val) {
	//二分查找点的坐标,离散化
	return lower_bound(dy.begin(), dy.end(), Val) - dy.begin();
}

void pushup(int rt, int l, int r) {
	int lc = rt << 1, rc = rt << 1 | 1;
	//如果当前线段存在,更新坐标
	//这里r+1是因为长度为x的线段有x+1个点,所以最后一个点是r+1
	if (cnt[rt]) len[rt] = dy[r + 1] - dy[l];
	else if (r == l) len[rt] = 0;
	else len[rt] = len[lc] + len[rc];
}

void update(int rt, int l, int r, int ql, int qr, int Val) {
	int lc = rt << 1, rc = rt << 1 | 1, mid = (l + r) >> 1;
	if (ql <= l && qr >= r) {
		//累加当前线段的值
		cnt[rt] += Val;
		pushup(rt, l, r);
	}
	else {
		//标记不需要下放,因为当前线段的增减和子线段没有关系
		//而且每条线段必定增加一次,减少一次
		if (ql <= mid) update(lson, ql, qr, Val);
		if (qr > mid) update(rson, ql, qr, Val);
		pushup(rt, l, r);
	}
}

int main() {
	int kase = 1;
	while (scanf("%d", &n),n != 0) {
		//初始化线段树
		dy.clear(); seg.clear();
		memset(cnt, 0, sizeof(cnt));
		memset(len, 0, sizeof(len));
		//读入数据
		for (int i = 1; i <= n; i++) {
			double x1, x2, y1, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
			//将每个矩形抽象成两条线段
			seg.push_back(Seg(y1, y2, x1, 1));
			seg.push_back(Seg(y1, y2, x2, -1));
			//存储每个坐标的值
			dy.push_back(y1); dy.push_back(y2);
		}
		//将浮点坐标离散化
		sort(dy.begin(), dy.end());
		dy.erase(unique(dy.begin(), dy.end()), dy.end());
		//将线段排序
		sort(seg.begin(), seg.end());
		int msize = seg.size(), k = dy.size();
		double ret = 0;
		for (int i = 0; i < msize - 1; i++) {
			//这里的qr为什么要减一? 是因为我线段树每个节点存的是l,r之间的线段,而不是点集,把每个线段当成点来看的话,
			//每个[l,r]的点构成的线段编号应该是[l,r-1]
			int ql = GetID(seg[i].l), qr = GetID(seg[i].r) - 1;
			//更新值
			update(1, 0, k - 1, ql, qr, seg[i].cover);
			//累加当前扫描线扫过的面积
			ret += len[1] * (seg[i + 1].x - seg[i].x);
		}
		printf("Test case #%d\nTotal explored area: %.2lf\n\n", kase++, ret);
	}
	return 0;
}

  

HDU 1542 Atlantis 线段树+离散化+扫描线,布布扣,bubuko.com

时间: 2024-12-29 09:18:19

HDU 1542 Atlantis 线段树+离散化+扫描线的相关文章

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的,这样理论上是对的 要注意处理高度相同的线段,把底边优先处理(在代码里就是f标记为1的线段),因为若是一个矩形的底边和另一个矩形的上边重合,则这个轮廓肯定不能算 不过POJ和HDU的数据好像都比较弱,我没进行上面的细节处理也AC了,不过一个很简单的数据就会不对,所以还是要处理一下才是真正正确的代码 我

hdu 1542 Atlantis(线段树)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6899    Accepted Submission(s): 3022 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线段树维护两个值:cover和len,cover表示该线段区间目前被覆盖的线段数目,len表示当前已覆盖的线段长度(化为离散前的真值),每次加入一条线段,将其y_low,y_high之间的区间染上line[i].cover,再以tree[1].len乘以接下来的线段的x坐标减去当前x坐标,即计算了一部

HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1542 Atlantis (线段树+扫描线)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18559    Accepted Submission(s): 7523 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantics 线段树+离散化扫描

将 x 轴上的点进行离散化,扫描线沿着 y 轴向上扫描 每次添加一条边不断找到当前状态有效边的长度 , 根据这个长度和下一条边形成的高度差得到一块合法的矩形的面积 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 6 using namespace std; 7 8 const int MAX=200+10; 9 int flag[MAX&l

hdu 1542 Atlantis(段树&amp;amp;扫描线&amp;amp;面积和)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i