HDU 3642 Get The Treasury(体积并+覆盖三次+线段树+扫描线)

这题就是给出了好多个长方体, 求出那些被覆盖了 3次及以上的体积


我们注意到z的范围很小,而且就给了1000个长方体

那么可以把z坐标离散化

然后在相邻的z坐标空间内,就变成了求面积覆盖

记录三个变量,代表一次覆盖,两次覆盖,三次覆盖及以上

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define lch(x) x<<1  
#define rch(x) x<<1|1  

const int maxn = 2222;
int cnt[maxn << 2];//边重复的次数
int sum[maxn << 2];//1次及以上
int sum2[maxn << 2];//2次及以上
int sum3[maxn << 2];
int X[maxn],Z[maxn];
struct Seg {
	int h, l, r;
	int s;//1为入边,-1为出边
	Seg() {}
	Seg(int a, int b, int c, int d) : l(a), r(b), h(c), s(d) {}
	bool operator < (const Seg &cmp) const {
		return h < cmp.h;
	}
}ss[maxn];
struct Spot
{
	int lx, rx, ly, ry, lz, rz;
}p[maxn];
void PushUp(int rt, int l, int r) {
	
	if (cnt[rt] >= 3) sum[rt] = sum2[rt] = 0, sum3[rt] = X[r + 1] - X[l];
	else if (cnt[rt] == 2)
	{
		if (l == r) sum2[rt] = X[r + 1] - X[l], sum3[rt] = 0, sum[rt] = 0;
		else
		{
			sum3[rt] = sum[lch(rt)] + sum[rch(rt)] + sum2[lch(rt)] + sum2[rch(rt)] + sum3[lch(rt)] + sum3[rch(rt)];
			sum2[rt] = X[r + 1] - X[l] - sum3[rt];
			sum[rt] = 0;
		}
	}
	else if (cnt[rt] == 1)
	{
		if (l == r) sum[rt] = X[r + 1] - X[l], sum2[rt] = 0, sum3[rt] = 0;
		else
		{
			sum3[rt] = sum2[lch(rt)] + sum2[rch(rt)] + sum3[lch(rt)] + sum3[rch(rt)];
			sum2[rt] = sum[lch(rt)] + sum[rch(rt)];
			sum[rt] = X[r + 1] - X[l] - sum2[rt] - sum3[rt];
		}
	}
	else
	{
		if (l == r) sum[rt] = sum2[rt] = sum3[rt] = 0;
		else
		{
			sum3[rt] = sum3[lch(rt)] + sum3[rch(rt)];
			sum2[rt] = sum2[lch(rt)] + sum2[rch(rt)];
			sum[rt] = sum[lch(rt)] + sum[rch(rt)];
		}
	}
}
void update(int L, int R, int c, int l, int r, int rt) {
	if (L <= l && r <= R) {
		cnt[rt] += c;
		PushUp(rt, l, r);
		return;
	}
	int m = (l + r) >> 1;
	if (L <= m) update(L, R, c, lson);
	if (m < R) update(L, R, c, rson);
	PushUp(rt, l, r);
}
int main() {
	int t,cas = 0;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		int m = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d%d%d%d%d", &p[i].lx, &p[i].ly, &p[i].lz, &p[i].rx, &p[i].ry, &p[i].rz);
			Z[m++] = p[i].lz;
			Z[m++] = p[i].rz;
		}//离散化
		sort(Z, Z + m);
		int cntz = unique(Z, Z + m) - Z;
		long long ans = 0;
		for (int i = 0; i < cntz - 1; i++)
		{
			memset(cnt, 0, sizeof(cnt));
			memset(sum, 0, sizeof(sum));
			memset(sum2, 0, sizeof(sum2));
			memset(sum3, 0, sizeof(sum3));
			m = 0;
			for (int j = 1; j <= n; j++)
				if (p[j].lz <= Z[i] && p[j].rz > Z[i])
				{
					X[m] = p[j].lx;
					ss[m++] = Seg(p[j].lx, p[j].rx, p[j].ly, 1);
					X[m] = p[j].rx;
					ss[m++] = Seg(p[j].lx, p[j].rx, p[j].ry, -1);
				}
				sort(X, X + m);
				sort(ss, ss + m);
				int cntx = unique(X, X + m) - X;
				for (int j = 0; j < m - 1; j++)
				{
					int l = lower_bound(X, X + cntx, ss[j].l) - X;
					int r = lower_bound(X, X + cntx, ss[j].r) - X - 1;
					if (l <= r) update(l, r, ss[j].s, 0, cntx - 1, 1);
					ans += (long long)sum3[1] * (long long)(ss[j + 1].h - ss[j].h) * (long long)(Z[i + 1] - Z[i]);
				}
			}
			printf("Case %d: %lld\n", ++cas, ans);
		}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值