【HDU 3642 求长方体的体积并

思路:很简单就不赘述了,更新父节点的函数注意一下就OK

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;
#define MAX 3005
#define ls rt<<1
#define rs ls|1
#define m (l+r)>>1
int sum1[MAX << 2];
int sum2[MAX << 2];
int sum3[MAX << 2];
int col[MAX << 2];
int posx[MAX<<1];
int posz[MAX << 1];
struct pos
{
	int l, r, h,z1,z2, s;
	pos(){}
	pos(int _l, int _r, int _z1,int _z2, int _h, int _s)
	{
		l = _l;
		r = _r;
		h = _h;
		z1 = _z1;
		z2 = _z2;
		s = _s;
	}
	bool operator<(pos b)
	{
		return h < b.h;
	}
}p[MAX << 1],temp[MAX<<1];
void uprt(int l,int r,int rt)
{
	if (col[rt] == 1)
	{
		sum3[rt] = sum2[ls] + sum2[rs] + sum3[ls] + sum3[rs];
		sum2[rt] = sum1[ls] + sum1[rs];
		sum1[rt] = posx[r + 1] - posx[l] - sum3[rt] - sum2[rt];
		return;
	}
	if (col[rt] == 2)
	{
		sum3[rt] = sum1[ls]+sum1[rs]+sum2[ls] + sum2[rs] + sum3[ls] + sum3[rs];
		sum2[rt] = posx[r + 1] - posx[l] - sum3[rt];
		sum1[rt] = 0;
		return;
	}
	if (col[rt] == 3)//一开始写成==就错了,这个区间覆盖3次以上也是这样的处理,或者不处理,例如,在最下面加上if==0
	{
		sum3[rt] = posx[r + 1] - posx[l];
		sum2[rt] = 0;
		sum1[rt] = 0;
		return;
	}
	if (col[rt] == 0)
	{
		sum3[rt] = sum3[ls] + sum3[rs];
		sum2[rt] = sum2[ls] + sum2[rs];
		sum1[rt] = sum1[ls] + sum1[rs];
		return;
	}
}

void updata(int L, int R, int c, int l, int r, int rt)
{
	if (L <= l&&r <= R)
	{
		col[rt] += c;
		uprt(l, r, rt);
		return;
	}
	int mid = m;
	if (L <= mid)
		updata(L, R, c, l, mid, ls);
	if (mid < R)
		updata(L, R, c, mid + 1, r, rs);
	uprt(l, r, rt);
}
int main()
{
	int t;
	cin >> t;
	int icase = 1;
	while (t--)
	{
		int n;
		int x1, x2, y1, y2, z1, z2;
		cin >> n;
		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
			posx[cnt] = x1;
			p[cnt] = pos(x1, x2, z1,z2, y1, 1);
			posz[++cnt] = z1;
			posx[cnt] = x2;
			p[cnt] = pos(x1, x2, z1,z2, y2, -1);
			posz[++cnt] = z2;
		}
		sort(posx, posx + cnt);
		sort(posz+1, posz + cnt+1);
		sort(p, p + cnt);
		int cntx = unique(posx, posx + cnt) - posx;
		int cntz = unique(posz+1, posz + cnt+1) - posz-1;
		long long ans = 0;
		for (int i = 2; i <=cntz; i++)
		{
			int cntp = 0;
			long long disz = posz[i] - posz[i - 1];
			for (int j = 0; j < cnt;j++)
			if (p[j].z1<=posz[i - 1]&&p[j].z2>=posz[i])//扫描高,考验扫描线的理解了
				temp[cntp++] = p[j];

			memset(sum1, 0, sizeof(sum1));
			memset(sum2, 0, sizeof(sum2));
			memset(sum3, 0, sizeof(sum3));
			memset(col, 0, sizeof(col));
			
			for (int j = 0; j < cntp - 1; j++)
			{
				int curl = lower_bound(posx, posx + cntx, temp[j].l) - posx;
				int curr = lower_bound(posx, posx + cntx, temp[j].r) - posx-1;
					updata(curl, curr, temp[j].s, 0, cntx - 1, 1);
				ans += ((long long)sum3[1]) * (temp[j + 1].h - temp[j].h)*disz;
			}
		}
		printf("Case %d: %lld\n", icase++,ans);
	}
	return 0;
}


你可能感兴趣的:(HDU,线段树,线段树,扫描线)