题目链接:uva 1493 - Draw a Mess

题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域。

解题思路:用并查集维护每一个位置相应下一个能够上色的位置。然后将上色倒转过来处理,就攻克了颜色覆盖的问题。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> using namespace std;
const int maxr = 205;
const int maxc = 50005; int N, M, Q, cnt[15];
int f[maxr][maxc]; struct Order {
int sign, star, end;
int x, y, r, w, c;
void set (int sign, int x, int y, int c, int r, int w = 0) {
this->sign = sign;
this->x = x;
this->y = y;
this->c = c;
this->r = r;
this->w = w;
del_star();
} void del_star () {
if (sign < 2) {
star = max(x - r, 0);
end = min(x + r, N - 1);
} else if (sign == 2) {
r = (r + 1) / 2 - 1;
star = x;
end = min(x + r, N-1);
}
} }op[maxc]; int getfar (int* far, int x) {
return x == far[x] ? x : far[x] = getfar(far, far[x]);
} int style (char ch) {
if (ch == 'C')
return 0;
else if (ch == 'D')
return 1;
else if (ch == 'T')
return 2;
else
return 3;
} void init () {
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i <= M; i++) {
for (int j = 0; j < N; j++)
f[j][i] = i;
} char s[20];
int x, y, r, c, w;
for (int i = 1; i <= Q; i++) {
scanf("%s", s);
if (s[0] != 'R') {
scanf("%d%d%d%d", &x, &y, &r, &c);
op[i].set(style(s[0]), x, y, c, r);
} else {
scanf("%d%d%d%d%d", &x, &y, &r, &w, &c);
op[i].set(style(s[0]), x, y, c, r, w);
}
}
} inline int get_R (int r, int x, int i, int sign) {
if (sign == 0)
return (int)sqrt(1.0 * r * r - 1.0 * (x - i) * (x - i));
else if (sign == 1)
return r - abs(x - i);
else if (sign == 2)
return r - (i - x);
return 0;
} int count (int x, int y, int r, int star, int end, int sign) {
int ret = 0;
for (int i = star; i <= end; i++) {
int R = get_R(r, x, i, sign); int mv = max(y - R, 0);
while (mv = getfar(f[i], mv), abs(mv - y) <= R && mv < M) {
ret++;
f[i][mv] = mv+1;
}
}
return ret;
} int count_rec (int x, int y, int r, int l) {
int ret = 0;
for (int i = x; i <= x + r - 1 && i < N; i++) {
int mv = y;
while (mv = getfar(f[i], mv), abs(mv - y) < l && mv < M) {
ret++;
f[i][mv] = mv+1;
}
}
return ret;
} void solve () { for (int i = Q; i; i--) {
int& col = cnt[op[i].c];
if (op[i].sign == 3)
col += count_rec(op[i].x, op[i].y, op[i].r, op[i].w);
else
col += count(op[i].x, op[i].y, op[i].r, op[i].star, op[i].end, op[i].sign);
} for (int i = 1; i <= 9; i++)
printf("%d%c", cnt[i], i == 9 ? '\n' : ' ');
} int main () {
while (scanf("%d%d%d", &N, &M, &Q) == 3) {
init();
solve();
}
return 0;
}

uva 1493 - Draw a Mess(并查集)的更多相关文章

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

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

  2. UVA1493 - Draw a Mess(并查集)

    UVA1493 - Draw a Mess(并查集) 题目链接 题目大意:一个N * M 的矩阵,每次你在上面将某个范围上色,不论上面有什么颜色都会被最新的颜色覆盖,颜色是1-9,初始的颜色是0.最后 ...

  3. Draw a Mess (并查集)

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

  4. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  5. UVA 12232 - Exclusive-OR(带权并查集)

    UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...

  6. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  7. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  8. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  9. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

随机推荐

  1. 妹子(girls)

    妹子(girls) 题目描述 万人迷皮皮轩收到了很多妹子的礼物,由于皮皮轩觉得每个妹子都不错,所以将她们礼物的包装盒都好好保存,但长此以往皮皮轩的房间里都堆不下了,所以只能考虑将一些包装盒放进其他包装 ...

  2. hdu 4501 多维0-1背包

    小明系列故事——买年货 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  3. .NET中如何有效的使用Cache

    原文发布时间为:2009-09-30 -- 来源于本人的百度文章 [由搬家工具导入]   Cache 即高速缓存 ,我想很多人对他的第一印象一定像我一样,感觉他一定能提高系统得性能和运行速度。   C ...

  4. 使用git快捷方便的保存代码

    大家都在使用git保存和备份代码,下面我们就来学习下吧. 一.本地安装和配置git 1.安装git pacman -S git //如果没有问题的话就可以安装成功了 2.验证 git --versio ...

  5. json-gson 解析泛型及解析null

    参考://public static class paraJson {public class paraJson{ static Gson gson = new GsonBuilder().regis ...

  6. Notepad++ 自动补全,括号自动完成插件,主题和字体设置

    Notepad++ 自动补全成对符号http://rabbit52.com/2012/devel/notepad-autocomplete-brackets QuickText 和 Zen Codin ...

  7. Java IO 学习(二)select/poll/epoll

    如上文所说,select/poll/epoll本质上都是同步阻塞的,但是由于实现了IO多路复用,在处理聊天室这种需要处理大量长连接但是每个连接上数据事件较少的场景时,相比最原始的为每个连接新开一个线程 ...

  8. 51nod 1050 循环数组最大子段和【环形DP/最大子段和/正难则反】

    1050 循环数组最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该 ...

  9. HDU 1025 Constructing Roads In JGShining's Kingdom[动态规划/nlogn求最长非递减子序列]

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  10. Codeforces 739B Alyona and a tree(树上路径倍增及差分)

    题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...