DGZX1563 - Shaping Regions形成的区域

本题与《矩形面积》类似,只不过将点的个数变多,但点坐标的取值范围变小,并且每个点多了一个颜色属性而已。

#include <stdio.h>
#include <algorithm>
using namespace std;

const int maxN = 1010;
int n, m, a, b;
int x[2*maxN], y[2*maxN], cx[2*maxN], cy[2*maxN];
int *p[2*maxN];
int map[2*maxN][2*maxN];
int color[maxN], ans[2501];

void input();
void process();
void compress(int *, int *);
int cmp(const void *sa, const void *sb);
void print();

int main()
{
	input();
	compress(x,cx);
	compress(y,cy);
	process();
	print();
	return 0;
}

void input()
{
	scanf("%d%d%d", &a, &b, &n);
	int i;
	for (i=1; i<=n; i++)
	{
		scanf("%d%d%d%d%d", &x[i], &y[i], &x[i+n], &y[i+n], &color[i]);
	}
	// 底色为白色,可以认为是一个左下角(0, 0)右上角(a, b)颜色为 1 的矩形
	m = 2 * n + 1;
	x[0] = 0, y[0] = 0;
	x[m] = a, y[m] = b;
}

void process()
{
	for (int i=n; i>=0; i--)
	{
		int t = i + n;
		for (int j=x[i]; j<x[t]; j++)
		{
			for (int k=y[i]; k<y[t]; k++)
			{
				if (map[j][k]==0)
					map[j][k] = color[i];
			}
		}
	}
	for (int i=0; i<=m; i++)
	{
		for (int j=0; j<=m; j++)
		{
			ans[map[i][j]] += cx[i] * cy[j];
		}
	}
	ans[1] += ans[0];// 题目中 1 代表白色,而程序中默认初值为 0 代表白色。
}

void compress(int *x, int *cx)
{
	for (int i=0; i<=m; i++)
		p[i] = &x[i];
	qsort(p,m+1,sizeof(int*),cmp);
	int t = 0, pt = *p[0];
	*p[0] = t;
	for (int i=1; i<=m; i++)
	{
		if ((*p[i])!=pt)
		{
			t++;
			cx[t-1] = *p[i] - pt;
		}
		pt = *p[i];
		*p[i] = t;
	}
}

int cmp(const void *sa, const void *sb)
{
	int a = **(int**)sa;
	int b = **(int**)sb;
	if (a>b)
		return 1;
	else return -1;
}

void print()
{
	for (int i=1; i<2501; i++)
	{
		if (ans[i]!=0)
		{
			printf("%d %d\n", i, ans[i]);
		}
	}
}

 

posted @ 2014-02-23 16:43  莞中OI  阅读(248)  评论(0)    收藏  举报