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(并查集)的更多相关文章

  1. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  2. Draw a Mess (并查集)

    It's graduated season, every students should leave something on the wall, so....they draw a lot of g ...

  3. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

  4. 并查集(涂色问题) HDOJ 4056 Draw a Mess

    题目传送门 题意:给出一个200 * 50000的像素点矩阵,执行50000次操作,每次把一个矩形/圆形/菱形/三角形内的像素点涂成指定颜色,问最后每种颜色的数量. 分析:乍一看,很像用线段树成段更新 ...

  5. 【HDOJ】4056 Draw a Mess

    这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...

  6. POJ 2912 - Rochambeau - [暴力枚举+带权并查集]

    题目链接:http://poj.org/problem?id=2912 Time Limit: 5000MS Memory Limit: 65536K Description N children a ...

  7. CodeForces Roads not only in Berland(并查集)

    H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  8. POJ2912 Rochambeau [扩展域并查集]

    题目传送门 Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4463   Accepted: 1545 ...

  9. POJ2912:Rochambeau(带权并查集)

    Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5208   Accepted: 1778 题目链接:h ...

随机推荐

  1. NuGet Package Explorer上传时报:failed to process request:'Method Not Allowed'错误解决办法

    相关日志:PUT /api/v2/package - 1000 -  NuGet+Package+Explorer/3.15.0.0+(Microsoft+Windows+NT+6.2.9200.0) ...

  2. 9.Python3标准库--数据压缩与归档

    ''' 尽管现代计算机系统的存储能力日益增长,但生成数据的增长是永无休止的. 无损(lossless)压缩算法以压缩或解压缩数据花费的时间来换取存储数据所需要的空间,以弥补存储能力的不足. Pytho ...

  3. 字典对象的 Pythonic 用法(上篇)

    字典对象在Python中作为最常用的数据结构之一,和数字.字符串.列表.元组并列为5大基本数据结构,字典中的元素通过键来存取,而非像列表一样通过偏移存取.笔者总结了字典的一些常用Pyhonic用法,这 ...

  4. Idea安装Scala插件(转)

    原文链接:http://blog.csdn.net/a2011480169/article/details/52712421 参考博客: 1.http://wwwlouxuemingcom.blog. ...

  5. java基础14 多态(及关键字:instanceof)

    面向对象的三大特征: 1.封装   (将一类属性封装起来,并提供set()和get()方法给其他对象设置和获取值.或者是将一个运算方法封装起来,其他对象需要此种做运算时,给此对象调用) 2.继承   ...

  6. Python股票信息抓取~

    本来想把股票的涨跌抓取出来,用汇通网的股票为例,就找了国际外汇为例. 页面里有xhr请求,并且每个xhr的url请求的 http://api.q.fx678.com/history.php?symbo ...

  7. 服务器或普通PC裸机安装 ESXI6.5

    ESXI :安装包 http://pan.baidu.com/s/1c2gM0Xq (包含注册机和其他套件,驱动打包工具) ESXI 6.5 在服务器安装比较方便,一般intel 的网卡都没多大问题, ...

  8. jQuery 最简化实现

    window.jQuery = (selector) => { let nodes = {}; if (typeof selector === 'string') { //是字符串 let te ...

  9. tornado中的cookie

    1. cookie与session的区别: Session:通过在服务器端记录用户信息从而来确认用户身份,保存在服务器上,每个用户会话都有一个对应的session Cookie:通过在客户端记录信息确 ...

  10. Socket 异步

    摘要: System.Net.Sockets.Sockte 类有一组增强功能,提供可供专用的高性能套接字应用程序使用的可选异步模式,SocketAsyncEventArgs 类就是这一组增强功能的一部 ...