HDU 1828 Picture 线段树+扫描线

题意:给你一些矩形的左上角点的坐标和右下角点的坐标,求周长并

最显而易见的思路就是对于x轴和y轴做两次扫描线,对于负数的坐标进行离散化。每次增加的值是线段变化量的绝对值。具体写法和求面积并的差不多。

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

using namespace std;

#define lson rt << 1 , l , mid
#define rson rt << 1 | 1, mid + 1 , r

const int maxn = 10000 + 10;
int cnt[maxn << 2], len[maxn << 2], n;

struct Seg {
	int x, l, r, cover;
	Seg(int x, int l, int r, int cover) :x(x), l(l), r(r), cover(cover) {}
	bool operator < (const Seg &seg) const {
		if (x == seg.x) return cover > seg.cover;
		return x < seg.x;
	}
};

vector<Seg> seg;
vector<int> num;
int sx1[maxn], sy1[maxn], sx2[maxn], sy2[maxn];

void pushup(int rt, int l, int r) {
	if (cnt[rt]) len[rt] = num[r + 1] - num[l];
	else if (l == r) len[rt] = 0;
	else len[rt] = len[rt << 1] + len[rt << 1 | 1];
}

void update(int rt, int l, int r, int ql, int qr, int Val) {
	if (ql <= l && qr >= r) {
		cnt[rt] += Val; pushup(rt, l, r);
	}
	else {
		int mid = (l + r) >> 1;
		if (ql <= mid) update(lson, ql, qr, Val);
		if (qr > mid) update(rson, ql, qr, Val);
		pushup(rt, l, r);
	}
}

int GetID(int val) {
	return lower_bound(num.begin(), num.end(), val) - num.begin();
}

int solve(int px1[], int py1[], int px2[], int py2[]) {
	seg.clear(); num.clear();
	memset(cnt, 0, sizeof(cnt));
	memset(len, 0, sizeof(len));
	for (int i = 0; i < n; i++) {
		seg.push_back(Seg(px1[i], py1[i], py2[i], 1));
		seg.push_back(Seg(px2[i], py1[i], py2[i], -1));
		num.push_back(py1[i]);
		num.push_back(py2[i]);
	}
	sort(seg.begin(), seg.end());
	sort(num.begin(), num.end());
	num.erase(unique(num.begin(), num.end()), num.end());
	int ret = 0, msize = seg.size(), nowlen = 0, k = num.size();
	for (int i = 0; i < msize; i++) {
		int ql = GetID(seg[i].l), qr = GetID(seg[i].r) - 1;
		update(1, 0, k - 1, ql, qr, seg[i].cover);
		ret += abs(nowlen - len[1]);
		nowlen = len[1];
	}
	return ret;
}

int main() {
	while (scanf("%d", &n) != EOF) {
		for (int i = 0; i < n; i++) {
			scanf("%d%d%d%d", &sx1[i], &sy1[i], &sx2[i], &sy2[i]);
		}
		int ret = solve(sx1, sy1, sx2, sy2) + solve(sy1, sx1, sy2, sx2);
		printf("%d\n", ret);
	}
}

总觉得可以就做一次扫描线就可以完成统计,就是不知道应该怎么写。看了别人的blog之后发现自己还是太弱。

要统计的东西有这些,假设扫描的是y轴。

1. y轴上有多少条线段,并且如果端点重合的话就算一条。因为x轴上面有多少条线是和y轴上有多少条线段有关的,是2倍的关系。为此还要开两个数组来辅助标记,表示当前线段的左端点和右端点是否被覆盖。

2. y轴上点段的长度,这个和上面的做法统计的功能一样。

3. 当前线段被覆盖了多少次,这个也和上面的统计方法一样。

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

using namespace std;

const int maxn = 2e4 + 10;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1,mid + 1,r
struct Seg {
	int l, r, x, cover;
	Seg(int l, int r, int x, int cover) :l(l), r(r), x(x), cover(cover) {}
	bool operator < (const Seg &seg) const {
		if (x == seg.x) return cover > seg.cover;
		return x < seg.x;
	}
};

vector<Seg> seg;
int n;
bool lbound[maxn << 2], rbound[maxn << 2];
int numseg[maxn << 2], len[maxn << 2], cnt[maxn << 2];
int minval, maxval;

void pushup(int rt, int l, int r) {
	int lc = rt << 1, rc = rt << 1 | 1;
	if (cnt[rt]) {
		lbound[rt] = rbound[rt] = true;
		//因为每一条线段的长度都等于1,第l条线段起始点是l,第r条线段的结束点是r + 1,长度是r + 1 - l
		len[rt] = r - l + 1;
		//因为横向必定有两条边,所以为2
		numseg[rt] = 2;
	}
	else if (l == r) {
		len[rt] = numseg[rt] = lbound[rt] = rbound[rt] = 0;
	}
	else {
		len[rt] = len[lc] + len[rc];
		lbound[rt] = lbound[lc];
		rbound[rt] = rbound[rc];
		numseg[rt] = numseg[lc] + numseg[rc];
		//如果在交点重合
		if (rbound[lc] && lbound[rc]) {
			numseg[rt] -= 2;
		}
	}
}

void update(int rt, int l, int r, int ql, int qr, int Val) {
	if (ql <= l && qr >= r) {
		cnt[rt] += Val;
		pushup(rt, l, r);
	}
	else {
		int mid = (l + r) >> 1;
		if (ql <= mid) update(lson, ql, qr, Val);
		if (qr > mid) update(rson, ql, qr, Val);
		pushup(rt, l, r);
	}
}

int main() {
	while (scanf("%d", &n) != EOF) {
		seg.clear();
		minval = INT_MAX; maxval = -1;
		for (int i = 1; i <= n; i++) {
			int x1, y1, x2, y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			seg.push_back(Seg(y1, y2, x1, 1));
			seg.push_back(Seg(y1, y2, x2, -1));
			minval = min(minval, min(y1, y2));
			maxval = max(maxval, max(y1, y2));
		}
		sort(seg.begin(), seg.end());
		int msize = seg.size(), ret = 0, last = 0;
		for (int i = 0; i < msize; i++) {
			if (seg[i].l < seg[i].r) update(1, minval, maxval, seg[i].l, seg[i].r - 1, seg[i].cover);
			//横向的长度
			if (i < msize - 1) ret += numseg[1] * (seg[i + 1].x - seg[i].x);
			//纵向的长度
			ret += abs(last - len[1]);
			last = len[1];
		}
		printf("%d\n", ret);
	}
	return 0;
}

  

HDU 1828 Picture 线段树+扫描线

时间: 2025-01-02 14:42:48

HDU 1828 Picture 线段树+扫描线的相关文章

hdu 1828 Picture(线段树&amp;扫描线&amp;周长并)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2578    Accepted Submission(s): 1363 Problem Description A number of rectangular posters, photographs and other pictures of the same shap

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

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

hdu 1828 Picture(线段树轮廓线)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3075    Accepted Submission(s): 1616 Problem Description A number of rectangular posters, photographs and other pictures of the same shape

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

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 (线段树+扫描线)

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 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 3642(线段树+扫描线)

三维扫描线,枚举z寻找相交区间的立方体,然后直接扫描线求xy平面的相交三次及以上面积,乘以z区间求和就可以了 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int maxn=1e6+100; const int maxm=2000+500; int col[maxn<<2]; int

HDU 3265 Posters (线段树+扫描线)(面积并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩形,然后就是面积并,特别注意的是x1 == x3 || x2 == x4的时候,要特判一下,否则会RE. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algori