HDU 3642 Get The Treasury 线段树+扫描线

反向标记是错的,要对矩形进行拆分

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

typedef long long LL;
using namespace std;
#define lson rt << 1,l,mid
#define rson rt << 1 | 1,mid + 1,r
const int maxn = 5e4 + 10;

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

LL cnt[maxn << 2], len[maxn << 2], n;
vector<Seg> seg;

void pushup(int rt, int l, int r) {
	if (cnt[rt] > 0) len[rt] = r - l + 1;
	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 main() {
	int x1, y1, x2, y2, x3, y3, x4, y4, maxval = -1;
	while (scanf("%I64d", &n) ,n != 0) {
		seg.clear();
		memset(cnt, 0, sizeof(cnt));
		memset(len, 0, sizeof(len));
		for (int i = 1; i <= n; i++) {
			scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
			seg.push_back(Seg(x1, y1, y2, 1));
			seg.push_back(Seg(x3, y1, y2, -1));

			seg.push_back(Seg(x4, y1, y2, 1));
			seg.push_back(Seg(x2, y1, y2, -1));

			seg.push_back(Seg(x3, y1, y3, 1));
			seg.push_back(Seg(x4, y1, y3, -1));

			seg.push_back(Seg(x3, y4, y2, 1));
			seg.push_back(Seg(x4, y4, y2, -1));
			maxval = max(maxval, max(max(y1, y2), max(y3, y4)));
		}
		sort(seg.begin(), seg.end());
		int msize = seg.size();
		LL ret = 0;
		for (int i = 0; i < msize - 1; i++) {
			//printf("now is %d %d %d\n", seg[i].l, seg[i].r, seg[i].cover);
			if (seg[i].l < seg[i].r) {
				update(1, 0, maxval, seg[i].l, seg[i].r - 1, seg[i].cover);
			}
			ret += (seg[i + 1].x - seg[i].x) * len[1];
			//printf("%d add %d\n", i, (seg[i + 1].x - seg[i].x) * len[1]);
		}
		printf("%I64d\n", ret);
	}
	return 0;
}

  

HDU 3642 Get The Treasury 线段树+扫描线

时间: 2024-08-01 17:04:46

HDU 3642 Get The Treasury 线段树+扫描线的相关文章

hdu 3642 Get The Treasury (三维的扫描线)

题目大意: 给出N个立方体. 求一个三维空间中被包围三次的空间的体积之和. 思路分析: 发现Z的范围很小.那么我们可以枚举Z轴,然后对 x y做扫描线. 而且不用枚举所有的Z ,只需要将Z离散化之后枚举. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define maxn 2222 #define debug puts("fuck!&q

HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

Problem A : Counting Squares From:HDU, 1264 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in t

hdu 1255 覆盖的面积(线段树&amp;扫描线&amp;重复面积)

覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3375    Accepted Submission(s): 1645 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数

hdu 1255 覆盖的面积 线段树扫描线求重叠面积

稀里糊涂打完了没想到1A了,心情还是很舒畅的,c和c++的四舍五入还是四舍六入遇积进位遇偶舍,我感觉很混乱啊,这道题我输出的答案是7.62,也就是遇偶舍了,可是我就手动处理一下进位问题,发现0.005 系统自动进位0.01了,尼玛啊,我一下子就混乱了,不是遇偶舍么,0也是偶数啊,怎么就进位了呢.最后我就不手动处理进位问题了,直接0.2lf%,虽然我输出的结果是7.62,但是提交也过了 这道题和poj1151,hdu1542差不多,扫描线详细讲解http://blog.csdn.net/young

HDU 4419 Colourful Rectangle --离散化+线段树扫描线

题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何维护每种颜色的长度着实让我伤透了脑筋.后来看了一位朋友的题解,才幡然醒悟. 开始想到了用二进制表示颜色,R用001表示,G用010表示,B用100表示.那么就可以用十进制1~7表示7种不同颜色了. 维护 cov[rt][1~3] 表示此区间内3种原色各有多少个, Len[rt][i]表示每种颜色的长

HDU 1255 覆盖的面积 (线段树+扫描线+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化一下,数据大小就缩小了,那么之后只需要线段树单点更新就好了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <algor

杭电 HDU ACM 1225 Atlantis (线段树 扫描线 离散化 最基本)

acm第一发扫描线问题,其实算法原理很好理解 ,但是具体实现起来还是遇到诸多问题,我尝试参考了网上两份对于解决 线段树表示区间问题的方法, 第一种是每个结点的真实值,比如对于更新离散化后的1 ~ 4区间,我们在线段树中更新的是1 ~ 3,这样单个结点也可以表示一个最小单位区间. 第二种那就是在建树的时候改变通常策略,1 ~ 10 为总区间,两个孩子为1 ~ 5 ,5 ~ 10. 核心难点:当我们每次找到需要更新的区间,首先应该更新cover值,然后判断此时是被覆盖了,还是解除覆盖了,如果刚好被覆

HDU 1255 覆盖的面积 线段树+扫描线

同 POJ1151 这次是两次 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <stack> #include <queue> #include <cmath> #include <vector

hdu 5091 Beam Cannon(线段树+扫描线+离散化)

Beam Cannon Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 457    Accepted Submission(s): 175 Problem Description Recently, the γ galaxies broke out Star Wars. Each planet is warring for resou