UVA1493 - Draw a Mess(并查集)
UVA1493 - Draw a Mess(并查集)
题目大意:一个N * M 的矩阵,每次你在上面将某个范围上色,不论上面有什么颜色都会被最新的颜色覆盖,颜色是1-9,初始的颜色是0.最后输出这个矩形中。每一个颜色有多少个。某个范围这个分为了四种,圆,矩形,菱形,还有正三角形(倒着的)。注意这题的边界,不能超出这个矩形,非常easy越界。
解题思路:由于题的矩阵范围是200 * 50000,然后操作又是50000,这样三个相乘,即使给60s也是不够的。由于行的数目比較少。假设每次都能将这一行哪个没处理过直接拿出来。而不用一个一个推断就快非常多了,这样一来就相当于每一个格子仅仅要遍历一次。所以这里就每行用一个并查集。标记这这个位置以及后面的位置能够上色的起始位置。
然后颜色更新问题仅仅要将操作反着过来上色就能够处理了。
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int M = 50005;
const int N = 205;
int f[N][M];
int G[N][M];
int color[10];
int n, m, q;
char str[N];
struct OP {
char type;
int x, y, l, r, c;
}op[M];
int getParent (int x, int y) {
return y == f[x][y] ?
y : f[x][y] = getParent (x, f[x][y]);
}
void init() {
for (int i = q - 1; i >= 0; i--) {
scanf ("%s", str);
if (str[0] == 'D')
scanf ("%d%d%d%d", &op[i].x, &op[i].y, &op[i].l, &op[i].c);
else if (str[0] == 'T')
scanf ("%d%d%d%d", &op[i].x, &op[i].y, &op[i].l, &op[i].c);
else if (str[0] == 'R')
scanf ("%d%d%d%d%d", &op[i].x, &op[i].y, &op[i].l, &op[i].r, &op[i].c);
else
scanf ("%d%d%d%d", &op[i].x, &op[i].y, &op[i].l, &op[i].c);
op[i].type = str[0];
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
f[i][j] = j;
memset (color, 0, sizeof (color));
}
void circle (int x, int y, int r, int c) {
int L, R, s;
for (int i = -r; i <= r; i++) {
s = sqrt(r * r - i * i);
if (i + x >= n || i + x < 0)
continue;
L = max(y - s, 0);
L = max (getParent (i + x, L), L);
R = min (s + y, m - 1);
for (int j = L; j <= R; j++) {
if (j == getParent (i + x, j)) {
color[c]++;
f[i + x][j] = R + 1;
} else
j = getParent(i + x, j) - 1;
}
}
}
void diamond (int x, int y, int r, int c) {
int L, R;
for (int i = -r; i <= r; i++) {
if (i + x >= n || i + x < 0)
continue;
R = min (r - abs(i) + y, m - 1);
L = max (abs(i) - r + y, 0);
L = max (L, getParent (i + x, L));
for (int j = L; j <= R; j++) {
if (j == getParent (i + x, j)) {
color[c]++;
f[i + x][j] = R + 1;
} else
j = getParent (i + x, j) - 1;
}
}
}
void rectangle (int x, int y, int l, int w, int c) {
int L, R;
for (int i = x; i < min(x + l, n); i++) {
L = max (y, getParent (i, y));
R = min (y + w - 1, m - 1);
for (int j = L; j <= R; j++) {
if (j == getParent (i, j)) {
color[c]++;
f[i][j] = R + 1;
} else
j = getParent (i, j) - 1;
}
}
}
void triangle (int x, int y, int w, int c) {
int L, R, h;
h = (w + 1) / 2;
for (int i = 0; i < h; i++) {
if (i + x >= n)
break;
L = max (y - h + i + 1, 0);
L = max (getParent(i + x, L), L);
R = min (y + h - i - 1, m - 1);
for (int j = L; j <= R; j++) {
if (j == getParent (i + x, j)) {
color[c]++;
f[i + x][j] = R + 1;
} else
j = getParent (i + x, j) - 1;
}
}
}
void solve () {
for (int i = 0; i < q; i++) {
if (op[i].type == 'D')
diamond (op[i].x, op[i].y, op[i].l, op[i].c);
else if (op[i].type == 'C')
circle (op[i].x , op[i].y, op[i].l, op[i].c);
else if (op[i].type == 'T')
triangle (op[i].x, op[i].y, op[i].l, op[i].c);
else
rectangle (op[i].x, op[i].y, op[i].l, op[i].r, op[i].c);
}
}
int main () {
char str[N];
while (scanf ("%d%d%d", &n, &m, &q) != EOF) {
init ();
solve();
for (int i = 1; i < 9; i++)
printf ("%d ", color[i]);
printf ("%d\n", color[9]);
}
return 0;
}
UVA1493 - Draw a Mess(并查集)的更多相关文章
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- Draw a Mess (并查集)
It's graduated season, every students should leave something on the wall, so....they draw a lot of g ...
- UVA 1493 Draw a Mess(并查集+set)
这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
- 并查集(涂色问题) HDOJ 4056 Draw a Mess
题目传送门 题意:给出一个200 * 50000的像素点矩阵,执行50000次操作,每次把一个矩形/圆形/菱形/三角形内的像素点涂成指定颜色,问最后每种颜色的数量. 分析:乍一看,很像用线段树成段更新 ...
- 【HDOJ】4056 Draw a Mess
这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...
- POJ 2912 - Rochambeau - [暴力枚举+带权并查集]
题目链接:http://poj.org/problem?id=2912 Time Limit: 5000MS Memory Limit: 65536K Description N children a ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- POJ2912 Rochambeau [扩展域并查集]
题目传送门 Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4463 Accepted: 1545 ...
- POJ2912:Rochambeau(带权并查集)
Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5208 Accepted: 1778 题目链接:h ...
随机推荐
- 64.Minimum Path Sum---dp
题目链接:https://leetcode.com/problems/minimum-path-sum/description/ 题目大意:从左上到右下的路径中,找出路径和最小的路径(与62,63题相 ...
- xcode没有ios7的模拟器
xcode7 目前只支持 ios8盒和iOS9的模拟器如果是Yosemite系统,下载xcode7和xcode6.4,两个版本可以共存,然后再下载iOS7默契你如果是EI Caption系统,网上说E ...
- nginx配置tomcat集群
显示nginx的核心配置 #user nobody;worker_processes 1; events { worker_connections 1024; #并发连接数} http { in ...
- Yii2使用驼峰命名的形式访问控制器
yii2在使用的时候,访问控制器的时候,如果控制器的名称是驼峰命名法,那访问的url中要改成横线的形式.例如: public function actionRoomUpdate() { // }//访 ...
- DotNetOpenAuth实践之Windows签名制作
系列目录: DotNetOpenAuth实践系列(源码在这里) 在上篇中我们搭建了一个简单的认证服务器,里面使用到了Windows签名证书,这一篇则是教大家如何制作Windows签名证书,下面进入正题 ...
- 前端JS框架系列之requireJS基础学习
1 背景 伴随着项目功能的不断扩充,客户体验的不断完善,实现交互逻辑的JS代码变得越来越多.起初,为了管理JS代码,我们把JS从页面中解放出来独立成文件,接着又把相似的交互代码提取到公共的JS页面中. ...
- 【LOJ】#2349. 「JOI 2017/2018 决赛」团子制作
题解 有意思的一个dp,我们对G计数,发现如果不在同一条对角线上的G肯定不会互相影响,所以我们对于每一条对角线dp dp的方式是枚举这个G以什么方式放,横着还是竖着,还是不放 代码 #include ...
- Windows 10家庭版远程桌面连接错误
由于需要操作远程服务器,经常使用“远程桌面连接”工具,一直很正常.今天突然提示 出现身份验证错误,可能是由于 CredSSP加密Oracle修正.什么情况!! 根据提示的地址https://go.mi ...
- mysql字符编码相关
mysql> show variables like '%character%'; +--------------------------+--------------------------- ...
- 七 oracle 表查询二
1.使用逻辑操作符号问题:查询工资高于500或者是岗位为manager的雇员,同时还要满足他们的姓名首字母为大写的J?select * from emp where (sal > 500 or ...