HDU 4056 Draw a Mess(并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4056

 

题意:有N * M个格子,现在有矩形,菱形,圆形,倒等腰三角形四种图形可以覆盖格子,覆盖颜色有九种,后覆盖的图形颜色会代替之前该格的颜色,问最终每种颜色覆盖了多少格子

 

思路:将所给出的图形倒着顺序覆盖,这样就不需要考虑之后的重复覆盖了,只需要将之前被覆盖的格子处理出来,考虑对每行分别处理,然后从该行最右端进行判断,倘若当前点没被覆盖,那就覆盖该格,然后将这一段新覆盖的线段用并查集标记到线段最左端,这样之后再扫这段时就可以直接判断该跳到哪里了,均摊复杂度为O(N * M)

 

 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>
#include <string>

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

const int MAXN = 51000;
const int MAXM = 50000;
const int INF = 0x3f3f3f3f;

typedef long long ll;

int ans[10];

struct Union
{
	int fa[MAXN], vis[MAXN];

	void init(int n)
	{
		for (int i = 0; i <= n; i++)
			fa[i] = i, vis[i] = 0;
	}

	int Find(int x)
	{
		return x == fa[x] ? x : fa[x] = Find(fa[x]);
	}

	void unite(int l, int r, int c)
	{
		int fl = Find(l);
		while (r >= l)
		{
			int fr = Find(r);
			if (!vis[fr])
			{
				vis[fr] = 1;
				ans[c]++;
			}
			if (fl != fr) fa[fr] = fl;
			r = fr - 1;
		}
	}
} uf;

struct query
{
	char s[20];
	int x, y, r, c, l, w;
} op[MAXN];

int main()
{
	int n, m, q;
	while (~scanf("%d%d%d", &n, &m, &q))
	{
		memset(ans, 0, sizeof(ans));

		for (int i = 1; i <= q; i++)
		{
			scanf("%s", op[i].s);
			if (op[i].s[0] == 'C')
				scanf("%d%d%d%d", &op[i].x, &op[i].y, &op[i].r, &op[i].c);
			else if (op[i].s[0] == 'D')
				scanf("%d%d%d%d", &op[i].x, &op[i].y, &op[i].r, &op[i].c);
			else if (op[i].s[0] == 'R')
				scanf("%d%d%d%d%d", &op[i].x, &op[i].y, &op[i].l, &op[i].w, &op[i].c);
			else
				scanf("%d%d%d%d", &op[i].x, &op[i].y, &op[i].w, &op[i].c);
		}

		for (int i = 0; i < n; i++)
		{
			uf.init(m);

			int l, r, col;
			for (int j = q; j >= 1; j--)
			{
				col = op[j].c;

				if (op[j].s[0] == 'C')
				{
					if ((i - op[j].x) * (i - op[j].x) > op[j].r * op[j].r) continue;
					int w = sqrt((long double)(op[j].r * op[j].r - (i - op[j].x) * (i - op[j].x)));
					l = op[j].y - w;
					r = op[j].y + w;
				}
				else if (op[j].s[0] == 'D')
				{
					if (i < op[j].x - op[j].r || i > op[j].x + op[j].r) continue;
					l = op[j].y - (op[j].r - abs(i - op[j].x));
					r = op[j].y + (op[j].r - abs(i - op[j].x));
				}
				else if (op[j].s[0] == 'R')
				{
					if (i < op[j].x || i >= op[j].x + op[j].l) continue;
					l = op[j].y;
					r = op[j].y + op[j].w - 1;
				}
				else
				{
					if (i < op[j].x || i > op[j].x + (op[j].w + 1) / 2 - 1) continue;
					int w = op[j].w - 2 * (i - op[j].x);
					l = op[j].y - w / 2;
					r = op[j].y + w / 2;
				}

				l = max(0, l), r = min(r, m - 1);
				uf.unite(l, r, col);
			}
		}

		for (int i = 1; i <= 9; i++)
			printf("%d%c", ans[i], i == 9 ? '\n' : ' ');
	}
	return 0;
}

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值